200 likes | 369 Vues
Object-Oriented Design. Design Patterns. 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.
E N D
Object-Oriented Design Design Patterns
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
Elements of a Design Pattern • 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
Elements of a Design 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 problem areas
Example Pattern: Composite • Structural pattern • Represent part-whole relationships in tree hierarchy • Uniform treatment of individual objects
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 *
Composite: Application Part PartType GetType() int GetIdentifier() int GetTime() float GetQuality() int GetNumberOfRejections() SetRejectStatus(bool) bool GetRejectStatus() sub-parts * CompoundPart AddPart(Part) int GetNumberOfParts() Part * Decompose()
Composite Pattern Example: Simulation Components SimComponent PartType GetType() int GetIdentifier() int GetTime() float GetQuality() int GetNumberOfRejections() SetRejectStatus(bool) bool GetRejectStatus() Contains 1 or more Components in a variable called sub-pieces Inheritance Piece Composite sub-pieces PartType GetType() ... all of the ops() ... all of ops() ... AddPart(Part) int GetNumberOfParts() Part * Decompose()
Composite Pattern Example: A Graphic Graphic Draw() Add(Graphic) Remove(int) GetChild(int) Contains 1 or more Graphics in a variable called graphics Inheritance Line Rectangle Text Picture graphics Draw() Draw() Draw() Draw() Add(Graphic) Remove(int) GetChild(int) for all g in graphics g.Draw() Add/Remove Graphic to/from list of graphics
Composite Pattern Example: A Graphic MailBody Disp() Add(Attch) Remove(int) GetChild(int) Text Image Pic Attachments attchs Disp() Disp() Disp() Disp() Add(Attch) Remove(int) GetChild(int) for all m in attchs m.Disp() Alternatively: Display a list of attachments
Observer • Behavioral pattern • When changed, Subject notifies Observers
Observer Update() Observer: Application Unit AddPart(Part *) GetNumberFinalParts() Part * GetFinalPart() SetTypesRequired(PartType *, int num) AddUnitObserver(UnitObserver *) NotifyObservers() UnitObserver Update() Update(Unit *) ProductRemovalObserver Update(Unit *) // called when unit completes a part for all o in observers o.Update(this)
Creational Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton
Structural Patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy
Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor