Introduction to Software Engineering: Object Design I Reuse & Patterns John T. Bell Department of Computer Science University of Illinois, Chicago Based on materials from Bruegge & DuToit 3e, Chapter 8, and Design Patterns by Gamma et. Al. 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. 2 1
THE Classic Design Patterns Book 3 Documentation for all Patterns 4 2
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) 5 Creational Patterns 6 3
Structural Patterns 7 Behavioral Patterns 1 / 2 8 4
Behavioral Patterns 2 / 2 9 The Design Pattern Triangle Name (Classification) Description Implementation Given any one, provide the other two 10 5
Creational Patterns 13 Singleton Ensure Only 1 Instance AND provide universal access to it. 14 6
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( ). 15 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. 16 7
Abstract Factory class produces many things, all designed to work together 17 Builder creates a complex structure step-by-step, and delivers the result 18 8
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 19 Structural Patterns 20 9
Adapter is used to connect two pre-existing classes. Class Approach: Object Approach: 21 Bridge separates interface from implementation, for 2 new classes 22 10
Composite creates hierarchical trees, with grouping nodes and leaf nodes 23 A Composite can have different kinds of grouping nodes ( uses Flyweight ) 24 11
Proxy A local stand-in taking the place of a remote resource. Note: Client only knows about Subject, which is abstract Uses: Speedup, Caching, Security, Smart pointers, more. Note: Proxy and RealSubject are siblings, both descendants of Subject 25 Decorator combines functionality in a daisy-chain fashion 26 12
Behavioral Patterns 1 / 2 27 Command encapsulates actions. Useful for undo/redo histories & macros. 28 13
Iterator is frequently used with Composite, and other collections 29 Iterator Example 30 14
Mediator coordinates between classes that don t need to know each other 31 What Does Command Do? A. Delegate a request to a subordinate class. B. Encapsulate requests as objects, providing support for undoing, queueing or logging requests. 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 15
Behavioral Patterns 2 / 2 33 Memento remembers a state without violating privacy / info hiding 34 16
Observer Gets notifications when changes occur. 35 Observer Implements MVC 36 17
State subclasses finite states 37 Strategy allows different algorithms using a common interface ( e.g. sort ) 38 18
Template encapsulates an algorithm while deferring specific steps to children 39 Visitor with collections allows for flexibility in both nodes and actions Each Visitor has methods for each type of node it knows how to visit See close-up next slide Iterator applied here, to pass Visitor to each node in turn. Each node has a method to Accept a Visitor, which it implements by passing itself to the appropriate Visitor method for that node type. 40 19
Visitor Hierarchy Multiplexes Over Both Node Types and Action Types Different concrete visitor classes handle different tasks. E.g. printing, spell-checking, intersection testing, etc. Different methods within each class get called by different node types. 41 Visitor in practice The application uses Iterator to move from node to node, passing the Visitor to each Accept method 42 20
Name that Pattern A. Builder B. Composite C. Façade D. Proxy E. Tree 43 What Type of Pattern is Memento? A. Abstract B. Behavioral C. Creational D. Forgetful E. Structural 44 21
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. 45 22