Material and some slide content from: - GoF Design Patterns Book Design Patterns #1 Reid Holmes Lecture 11 - Tuesday October 19 2010.
GoF design patterns !"#$%&'()*$+,--&.*' /.&,-("*,0 1-.23-2.,0 4&5,6(".,0 #,3-".7$8&-5"9 :9,<-".$=$30,'' B*-&.<.&-&. 30,'' D&><0,-&$8&-5"9 :;'-.,3-$#,3-".7 /5,(*$"C$.&'<"*'(;(0(-7 :9,<-".=";?&3- 42(09&. 4.(9)& /">>,*9 +."-"-7<& B-&.,-". /"><"'(-& 1(*)0&-"* 8&9(,-". %&3".,-". ";?&3- #,3,9& 8&>&*-" #07@&()5- E;'&.6&. +."A7 1-,-& 1-.,-&)7 F('(-". REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Singleton ‣ Intent: “Ensure a class has only one instance” ‣ Motivation: ? ‣ Applicability: ‣ Situations when there must be only one copy of a class. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Singleton ‣ Structure: ‣ Participants: ‣ an instance operation that retrieves the instance. ‣ may be responsible for creating instance. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Singleton ‣ Collaborations ‣ All collaboration via instance operation. ‣ Consequences: ‣ Controlled access to instance. ‣ Reduced name space. ‣ Permits variable number of instances. ‣ More flexible than class operations. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Singleton ‣ Implementation: 1.? 2.? ‣ Related to: ‣ Can be used to create Abstract Factory, Builder, and Prototype. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Abstract factory ‣ Intent: “Provide an interface for creating families of related objects without specifying their concrete classes” ‣ Motivation: Consider a multi-platform UI toolkit. A WidgetFactory can provide an interface to make sure the right widget is instantiated for each platform. ‣ Applicability: ‣ When a system should be independent of how its products are created and represented. ‣ A system contains multiple families of products. ‣ ? REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Abstract factory ‣ Structure ‣ Participants: ‣ Abstract/Concrete Factory ‣ Abstract/Concrete Product ‣ Client REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Abstract factory ‣ Collaborations ‣ Usually only one Abstract Factory (singleton). ‣ Objects are created by concrete factories. ‣ Consequences: ‣ Isolates concrete classes from clients. ‣ ? ‣ Makes exchanging families easy. ‣ ? ‣ Makes adding products hard. ‣ ? REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Abstract factory ‣ Implementation: ‣ Create abstract factory interface. ‣ Use factory method to create descriptive names. ‣ Create concrete products/factories. ‣ ? ‣ Known uses: Frequently used in widget toolkits. ‣ Related to: Often implemented with Factory Method or Prototypes. Concrete factories are often Singletons. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Builder ‣ Intent: “Separate the construction of complex objects from its representation.” ‣ Motivation: Consider a text reader that must convert back and forth between RTF , ASCII, and TeX. The ordering / combinations are unbounded requiring a flexible composition mechanism. ‣ Applicability: ‣ ? ‣ When the construction process must allow for different representation. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Builder ‣ Structure ‣ Participants: ‣ Builder / Concrete Builder ‣ Director ‣ Product REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Builder ‣ Collaborations: ‣ Client creates director with desired builder. ‣ Director tells builder to build parts. ‣ Client retrieves parts from builder. ‣ Consequences: ‣ Vary internal representation. ‣ Isolates code from construction / representation. ‣ ? REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Builder ‣ Implementation: ‣ 1) Create flexible construction interface. Allow new parts to be appended to the existing whole. ‣ 2) Don’t bother with abstract products, they usually vary enough that it doesn’t help much. ‣ 3) Don’t use interface for Builder; empty methods allow ConcreteBuilders to choose what methods to implement. ‣ Known uses: Smalltalk parser & ClassBuilder. ‣ Related to: Similar to Abstract Factory. Frequently builds Composite objects. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Builder Example REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Proxy ‣ Intent: “Provide a placeholder to control access to another object.” ‣ Motivation: One reason to control access is cost: consider an object that is expensive to populate entirely but cheap to partially populate. (e.g., remote object, large file from disk, etc.) ‣ Applicability: (When a more versatile reference is needed.) ‣ Remote : ? ‣ Virtual : ? ‣ Protection : Protect access to objects. ‣ Smart reference : Performs additional actions. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Proxy ‣ Structure ‣ Participants: ‣ Proxy ‣ Subject / RealSubject REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Proxy ‣ Collaborations: ‣ Client interacts with proxy. ‣ Proxy forwards req. to RealSubject as required. ‣ Consequences: ‣ Remote : ? ‣ Virtual : ? ‣ Protection : Housekeeping / auth can occur. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Proxy ‣ Implementation: ‣ 1) Create common interface for Proxy/ RealSubject. ‣ 2) Reference Proxy in Client. ‣ 3) Proxy forwards requests as necessary. ‣ Known uses: Image manipulation / RPC. ‣ Related to: Similar to Adapter and Decorator. Decorators add responsibilities while proxies serve as mediators. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Proxy wrt Project REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Facade ‣ Intent: “Provide a unified, higher-level, interface to a whole module making it easier to use.” ‣ Motivation: ? ‣ Applicability: ‣ When you want a simple interface to a complex subsystem. ‣ ? ‣ You want to layer your subsystems. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Facade REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Facade ‣ Participants: ‣ Facade ‣ Subsystem classes ‣ Collaborations: ‣ Clients interact subsystem via Facade. ‣ Consequences: ‣ ? ‣ Promotes ? ‣ Doesn’t prevent access to subsystem classes. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Facade ‣ Implementation: ‣ 1) Analyze client / subsystem tangling. ‣ 2) Create interface. Abstract factories can also be used to add further decoupling. ‣ Known uses: Varied. ‣ Related to: Abstract Factory can be used with Facade to create subsystem objects. Facades are frequently Singletons. Abstracts functionality similar to Mediator but does not concentrate on communication. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Facade wrt Project REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Activity ‣ 5 mins: ‣ Right side: Develop a use for a facade pattern for your system. ‣ Left side: Develop a usage of a proxy pattern for your system. ‣ 10 mins (5 / group): ‣ Match up with team from other side of room. Explain your pattern and how it improves your system’s design. REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Recommend
More recommend