design patterns - university of illinois at chicagoi342/notes/12 - design patterns.pdfdesign...
TRANSCRIPT
11/8/2018
1
Design Patterns
John T. Bell
Department of Computer ScienceUniversity of Illinois, Chicago
Based on materials from Chapter 15 of “The Object-Oriented Thought Process” by Matt Weisfeld
and “Design Patterns” by Gamma et. Al.
2
Design Through Reuse
• Many design problems have been solved before.
• Libraries, Frameworks, COTS, are all approaches to reusing code.
• Architectures and Design Patterns are approaches to reusing concepts and ideas.
• Design Patterns are well-documented solutions to commonly encountered ( OO ) design problems. The classic source is by Gamma et. al.
11/8/2018
2
3
THE Classic Design Patterns Book( Available from UIC Library Safari Books )
4
Documentation for all Patterns
11/8/2018
3
5
Design Pattern Classification
Purpose
Creational Structural Behavioral
Scope Class Factory Method (107) Adapter (139) Interpreter (243)Template Method (325)
Object Abstract Factory (87)Builder (97)Prototype (117)Singleton (127)
Adapter (139)Bridge (151)Composite (163)Decorator (175)Facade (185)Proxy (207)
Chain of Responsibility (223)Command (233)Iterator (257)Mediator (273)Memento (283)Flyweight (195)Observer (293)State (305)Strategy (315)Visitor (331)
6
Creational Patterns
( clone( ) method in Java Object )
11/8/2018
4
7
Singleton – Ensure Only 1 InstanceAND provide universal access to it.
8
Factory Method creates a related object, type determined dynamically
Example: A “Lock” class could contain a “getKey( )” method.ComboLock creates ComboKey, BikeLock creates BikeKey, etc.
11/8/2018
5
9
Abstract Factory class produces many things, all designed to work together
10
Prototype makes copies of objects using overriding and polymorphism.
• Does not require knowing what type of object will be created when writing code.
• Implemented in Java as Object.clone( ).
11/8/2018
6
11
Structural Patterns
12
Bridge separates interface from implementation, for 2 new classes
11/8/2018
7
13
Design Choice:Overloading or Bridge Pattern ?
A = Character B = DecisionMakerA1/A2 = Player / NPC B1/B2 = UI/AImethodA = makeMove method = getMove
14
Adapter is used to connect two pre-existing classes.
Class Approach:
Object Approach:
11/8/2018
8
15
Proxy – A “local” stand-in taking the place of a “remote” resource.
Uses: Speedup, Caching, Security,Smart pointers, more.
Note: Client only knows about Subject, which is abstract
Note: Proxy and RealSubject are siblings, both descendants of Subject
16
Composite creates hierarchical trees, with grouping nodes and leaf nodes
11/8/2018
9
17
Java’s AWT Implements theComposite Design Pattern
Object
java.awt.Component
java.awt.Container
*
LabelButton
WindowPanel JComponent
18
Behavioral Patterns – 1 / 2
11/8/2018
10
19
Iterator is frequently used with Composite, and other collections
20
Iterator Example
11/8/2018
11
21
Mediator coordinates between classes that don’t need to know each other
22
Behavioral Patterns – 2 / 2
11/8/2018
12
23
Memento remembers a state without violating privacy / info hiding
24
Observer – Gets notifications when changes occur.
11/8/2018
13
25
Observer Implements the Model-View-Controller Architecture, MVC
• Model - Data modelling a system.• View - Observes the data.• Controller - Adjusts the data
and/or the view.
Model
and controllersViews
26
Java Swing implements the Observer pattern by adding EventListeners:
• EventListeners ( and descendants ) are Interfaces, implemented by many classes, including user-written classes.
• Listeners can be added to java.awt.Components and descendants thereof.
• Listeners may be low-level, e.g. mouseMotionListener, or high-level, e.g. ActionListener.
• Some listeners require multiple methods. Adapters are classes that provide dummy methods for these types of listeners. ( Override only the needed methods. )
11/8/2018
14
27
EventListeners are added to Components
Object
java.awt.Component
java.awt.Container
*
LabelButton
WindowPanel JComponent
EventListeners
28
State subclasses finite states
state =
: State : State
: State
Changing the state variable changes how the Handle( ) method operates
Consider using Singleton to ensure only one instance of each State type exists
11/8/2018
15
29
The Design Pattern Triangle
Name(Classification)
Description Implementation
Given any one, provide the other two
30
Name That Pattern
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
A. Abstract Factory
B. Factory
C. Factory Method
D. Family
E. Builder
11/8/2018
16
31
What Does Command Do ?
A. Encapsulate requests as objects, providing support for undoing, queueing or logging requests.
B. Delegate a request to a subordinate class.
C. Explore a daisy-chain of potential handlers to find the best handler to service a request.
D. Provide a surrogate or placeholder for another object to control access to it.
E. Provide a unified interface to a set of interfaces in a subsystem.
32
Name that Pattern
A. Composite
B. Tree
C. Builder
D. Façade
E. Proxy
11/8/2018
17
33
What Type of Pattern is Memento?
A. Abstract
B. Behavioral
C. Creational
D. Forgetful
E. Structural
34
Open-Ended
• Name and describe a design pattern that you have used in a project, either this semester or at some other time. Provide a UML diagram illustrating its use in your project.
• Which is your favorite design pattern that we have studied this semester? Explain what you like about it.
11/8/2018
18
35
Design Patterns in the Java API
• Iterator• Factory Method - Collection.iterator( )• Prototype - Object.clone( )• Composite - Container extends Component• Observer - Component.add______Listener( )• Decorator - ScrollPane
• Singleton - KeyboardScanner• Command - Move