![Page 1: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/1.jpg)
Lecture 12 - Thursday October 21 2010.
Material and some slide content from:- GoF Design Patterns Book
Design Patterns #2Reid Holmes
![Page 2: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/2.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
GoF design patternsDesign Patterns!"#$%&'()*$+,--&.*'
/.&,-("*,0 1-.23-2.,0 4&5,6(".,0
#,3-".7$8&-5"9
:;'-.,3-$#,3-".7
42(09&.
+."-"-7<&
1(*)0&-"*
:9,<-".$=$30,''
4.(9)&
/"><"'(-&
%&3".,-".
#,3,9&
:9,<-".=";?&3-
#07@&()5-
+."A7
B*-&.<.&-&.
/5,(*$"C$.&'<"*'(;(0(-7
/">>,*9
B-&.,-".
8&9(,-".
D&><0,-&$8&-5"9
8&>&*-"
E;'&.6&.
1-,-&
1-.,-&)7
F('(-".
30,''
";?&3-
![Page 3: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/3.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Composite‣ Intent: “Compose objects into whole-part
hierarchies, allowing ?
‣ Motivation: For situations where complex artifacts can be composed from multiple elements.
‣ Applicability:
‣ For representing whole-part relationships.
‣ For situations when you want your clients to ?
![Page 4: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/4.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Composite‣ Structure:
‣ Participants:‣ Component (lowest common denominator)‣ Leaf‣ Composite‣ Client
![Page 5: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/5.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Composite‣ Collaborations
‣ Clients interact with component interface; composite objects forward requests to leaves.
‣ Consequences:
‣ Allows applications to ?‣ Easy to add new components.‣ Negative: can make your app overly general.
Hard to restrict how leaves can be composed.
![Page 6: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/6.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Composite‣ Implementation:
1.Explicit parent references can be helpful2.Maximize the component interface3.Child ordering may matter 4.Caching for performance
‣ Related to:
‣ Often used with Decorator and Visitor patterns.
![Page 7: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/7.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Decorator‣ Intent: “Dynamically add additional responsibilities to
structures.”
‣ Motivation: Sometimes we want to add new responsibilities to individual objects, not the whole class. Can enclose existing objects with another object.
‣ Applicability: ‣ Add responsibilities dynamically and transparently.‣ Remove responsibilities dynamically.‣ ?
![Page 8: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/8.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Decorator‣ Structure
‣ Participants:‣ Component / concrete component‣ Decorator / concrete decorator
![Page 9: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/9.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Decorator (code ex)
![Page 10: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/10.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Decorator‣ Collaborations‣ Decorators forward requests to component object.
‣ Consequences:‣ More flexible.‣ Avoids ?‣ Warn: Decorator & component are not identical.‣ ?
‣ Negative: Many of little objects.
![Page 11: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/11.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Decorator‣ Implementation:‣ 1) Interface conformance. ‣ 2) Abstract decorator not needed if only one
decoration is required.‣ 2) Keep component classes lightweight. ‣ 3) Changing a skin instead of changing the guts.
‣ Related to: Decorators are a kind of single-node Composite. Decorators can change the skin, Strategy pattern can change the guts.
![Page 12: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/12.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Visitor‣ Intent: “Represent operations to be performed on
classes of elements.”
‣ Motivation: ?
‣ Applicability:‣ When you have a large object structure you want
to traverse.‣ ?‣ The objects rarely change but the operations may.
![Page 13: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/13.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Visitor‣ Structure
‣ Participants:
‣ Visitor / ConcreteVisitor
‣ Element / ConcreteElement
‣ ObjectStructure
![Page 14: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/14.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Visitor‣ Collaborations:
‣ Client creates the ConcreteVisitor that traverses the object structure.
‣ The visited object calls its corresponding visitor method on the ConcreteVisitor.
‣ Consequences: ‣ Adding new operations is easy.‣ Visitor gathers related operations‣ Adding ConcreteElement classes is hard.‣ Accumulating state.‣ Negative: ?
![Page 15: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/15.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Visitor‣ Implementation:
‣ 1) Double dispatch.
‣ 2) Who traverses structure?
‣ Related to: Good ad visiting Composite structures.
![Page 16: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/16.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Command‣ Intent: “Encapsulate requests enabling clients to
log / undo them as required.”
‣ Motivation: In situations where you need to be able to make requests to objects without knowing anything about the request itself or the receiver of the request, the command pattern enables you to pass requests as objects.
‣ Applicability: ‣ ?‣ ?‣ Support undo.‣ Model high-level operations on primitive
operations.
![Page 17: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/17.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Command‣ Structure
‣ Participants:‣ Command / ConcreteCommand‣ Client‣ Invoker‣ Receiver
![Page 18: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/18.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Command‣ Collaborations:‣ Client creates ConcreteCommand and specifies
receiver.‣ Invoker stores ConcreteCommand object.‣ Invoker requests execute on Command; stores
state for undoing prior to execute (if undoable).‣ Concrete invokes operations on its receiver to
perform request.‣ Consequences: ‣ ?‣ Commands are first-class objects.‣ Commands can be assembled into composite.‣ ?
![Page 19: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/19.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
Command‣ Implementation:
‣ 1) How smart should a command be?
‣ 2) Support undo/redo.
‣ 3) Avoiding error accumulation in the undo process.
‣ Related to: Composite commands can be created; the Memento pattern can store undo state. Commands often use Prototype when they need to be stored for undo/redo.
![Page 20: Design Patterns #2 - David R. Cheriton School of Computer ... · REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE Composite ‣ Implementation: 1.Explicit parent references can be](https://reader034.vdocuments.site/reader034/viewer/2022042220/5ec5f6a3b5b82a3f476e2a7c/html5/thumbnails/20.jpg)
REID HOLMES - SE2: SOFTWARE DESIGN & ARCHITECTURE
iRoadTrip demo‣ Functionality being demonstrated:
‣ Creating a trip.
‣ Joining a trip.
‣ Adding new users to a trip dynamically.
‣ Updating positions dynamically.
‣ Not implemented:
‣ Statistics / querying.
‣ Interaction.
‣ Leaving a trip / managing trips.