1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and...
-
Upload
roger-beasley -
Category
Documents
-
view
212 -
download
0
Transcript of 1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and...
1
Design PatternsDesign Patterns
Object-Oriented DesignObject-Oriented Design
2
Design Patterns
Reuse of design knowledge and experience
Common in many engineering disciplines
Avoids “reinventing the wheel” Design Patterns, Gamma, Helm,
Johnson, Vlissides (GoF) Architecture: Christopher Alexander
3
Elements of a Design Elements of a Design PatternPattern
name - simple, descriptive term
intent - brief description of aim
motivation - concrete example
applicability - where is it useful?
structure - “blueprint” diagram
participants - each class in pattern
4
Elements of a Design Elements of a Design Pattern (cont’d)Pattern (cont’d)
collaborations - relationships among participants
consequences - implications of use
implementation - general implementation
sample code - code for a particular application
known uses - applications where used
related patterns - to combine or substitute
problemareas
5
Example Pattern: CompositeExample Pattern: Composite
Structural pattern Represent part-whole relationships
in tree hierarchy Uniform treatment of individual
objects
6
Composite: Application
Piece PartType GetType()int GetIdentifier()int GetTime()float GetQuality()int GetNumberOfRejections()SetRejectStatus(bool)bool GetRejectStatus()
CompoundPart AddPart(Part)int GetNumberOfParts()Part * Decompose()PartType GetType()int GetIdentifier()int GetTime()float GetQuality()int GetNumberOfRejections()SetRejectStatus(bool)bool GetRejectStatus()
sub-parts *
7
Composite: Application
Part PartType GetType()int GetIdentifier()int GetTime()float GetQuality()int GetNumberOfRejections()SetRejectStatus(bool)bool GetRejectStatus()
CompoundPart AddPart(Part)int GetNumberOfParts()Part * Decompose()
sub-parts *
8
Composite Pattern Example: Simulation Components
SimComponent
PartType GetType()int GetIdentifier()int GetTime()float GetQuality()int GetNumberOfRejections()SetRejectStatus(bool)bool GetRejectStatus()
Composite
all of ops() ...AddPart(Part)int GetNumberOfParts()Part * Decompose()
Piece
PartType GetType() ...all of the ops()...
Inheritance
Contains 1 ormore Components
in a variablecalled sub-pieces
sub-pieces
9
Composite Pattern Example: A Graphic
Graphic
Draw()Add(Graphic)Remove(int)GetChild(int)
Picture
Draw()Add(Graphic)Remove(int)GetChild(int)
Text
Draw()
Rectangle
Draw()
Line
Draw()
Inheritance
Contains 1 ormore Graphicsin a variable
called graphics
graphics
for all g in graphicsg.Draw()Add/Remove Graphic
to/from list of graphics
10
Composite Pattern Example: A Graphic
MailBody
Disp()Add(Attch)Remove(int)GetChild(int)
Attachments
Disp()Add(Attch)Remove(int)GetChild(int)
Pic
Disp()
Image
Disp()
Text
Disp()attchs
for all m in attchsm.Disp()
Alternatively:Display a list of attachments
11
Composite:Composite:General StructureGeneral Structure
12
Observer
Behavioral pattern When changed, Subject notifies
Observers
13
Observer: Structure
14
Observer: Sequence Diagram
15
Observer: Application
Unit AddPart(Part *)GetNumberFinalParts()Part * GetFinalPart()SetTypesRequired(PartType *, int num)AddUnitObserver(UnitObserver *) NotifyObservers()
UnitObserver Update()Update(Unit *)
ProductRemovalObserver Update(Unit *)
Observer Update()
// called when unit completes a partfor all o in observers o.Update(this)
16
Creational Patterns
Abstract Factory Builder Factory Method Prototype Singleton
17
Structural Patterns
Adapter Bridge Composite Decorator Facade Flyweight Proxy
18
Behavioral Patterns
Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor