Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "Design Patterns CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 Design Patterns CSIS 3701: Advanced Object Oriented Programming

2 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)

3 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

4 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 –…–…

5 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.

6 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

7 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.

8 Façade Pattern

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

10 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.

11 Iterator Pattern

12 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.

13 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 object Member variables read

14 Memento Pattern

15 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.

16 Observer Pattern

17 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).

18 Command Design Pattern “Encapsulate a request as an object, thereby letting you parameterize clients with different requests” Implemeted by Event objects in Java ActionEvent public Object getSource(); public class Main public void actionPerformed( ActionEvent e) { … } JButton object

19 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.

20 Abstract Factory Pattern


Download ppt "Design Patterns CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google