design patterns csis 3701: advanced object oriented programming

20
Design Patterns CSIS 3701: Advanced Object Oriented Programming

Upload: bethanie-spencer

Post on 05-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Design Patterns

CSIS 3701: Advanced Object Oriented Programming

Page 2: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Design Patterns

• Standard “toolbox” of design ideas– Usually codify solutions to common problems within

system design

• Usually support the major data classes within a system by helping them communicate– Identify major data classes– Identify the design pattern classes to connect them– Not at overall architecture level

(but often used to implement common components within an architecture)

Page 3: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Design Patterns• Creational Patterns

– Abstract Factory– Builder– Factory Method– Prototype– Singleton

• Structural Patterns– Adapter– Bridge– Composite– Decorator – Façade– Flyweight– Proxy

• Behavioral Patterns– Chain of Responsibility– Command– Interpreter– Iterator– Mediator– Memento– Observer– State– Strategy – Template Method– Visitor

Page 4: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Design Patterns

• “Design Patterns: Elements of Reusable Object-Oriented Style” (Gamma, Helm, Johnson, Vlissides)

• Many are built directly into Java– Façade pattern interface– Template pattern abstract methods– Observer, Command patterns event handlers– …

Page 5: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Decorator Pattern

• “Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.”

• Example: The JScrollPane visual component. – Can be “wrapped” around any other

visual component (JTextArea, JPanel, etc.)

– Contains the other visual component.– Much more convenient than trying to

define scrolling in every different visual class.

Page 6: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Decorator Pattern

• JScrollPane example:JTextArea area = new JTextArea(10, 25);JScrollPane scroll = new JScrollPane(area);

JPanel p = new JPanel();getContentPane.add(p); p.add(scroll);

The JScrollPane contains the JTextArea

We then add the JScrollPane to the JPanel

Page 7: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Façade Pattern

• “Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes a subsystem easier to use.”

• Example:Every time a new name is added to a NameList, name should be used to add a new record to a database.

Instead of the developers of putting the SQL and JDBC code directly in the methods, a separate NameList class provides methods to create the SQL and JDBC.

Page 8: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Façade Pattern

Page 9: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Façade Pattern

• Often implemented as an interface– Interface for façade defined in advance– Specific classes later implement the façade

Page 10: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Iterator Pattern

• Provides way to access elements of an aggregate object sequentially without knowing representation

• Example: User classes would like to get students in a Roster without having to deal with array, linked list, or however Roster stores it.

Page 11: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Iterator Pattern

Page 12: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Memento Pattern

• Without violating encapsulation, capture and externalize an object’s internal state so than the object can be restored to this state later.

• Example:Students may change their mind after adding a course. You need to implement an “undo” button that undoes up to the last five changes made.

Page 13: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Memento Pattern

• Serialization in Java– Convert entire state of object to

single string which can be stored in either a file or a database.

file

objectMember variables

read

Page 14: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Memento Pattern

Page 15: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Observer Pattern

• Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

• Example:The registrar maintains a list of all open sections. When a section is opened or closed (due to students dropping or adding), that list must be updated.

Page 16: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Observer Pattern

Page 17: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Observer Design Pattern

• Implemented by Listeners in Java– Objects register with components (by calling their addActionListener methods).

– When events occur on components, those objects notified (by calling their actionPerformed methods).

Page 18: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Command Design Pattern

• “Encapsulate a request as an object, thereby letting you parameterize clients with different requests”

• Implemeted by Event objects in Java

ActionEventpublic Object getSource();

public class Main public void actionPerformed( ActionEvent e) { … }

JButton object

Page 19: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Abstract Factory Pattern

• Provide and Interface for creating families of related or dependent objects without specifying their concrete classes.

• Example:Java’s BorderFactory class contains static methods that return different types of borders for use by panels. Adding new types of borders just requires adding new methods, not new classes.

Page 20: Design Patterns CSIS 3701: Advanced Object Oriented Programming

Abstract Factory Pattern