ObjectOriented Design Patterns 1 Design Patterns 4 Reuse

Object-Oriented Design Patterns 1

Design Patterns 4 Reuse of design knowledge and experience 4 Common in many engineering disciplines 4 Avoids “reinventing the wheel” 4 Design Patterns, Gamma, Helm, Johnson, Vlissides (Go. F) 4 Architecture: Christopher Alexander 2

Elements of a Design Pattern 4 name - simple, descriptive term 4 intent - brief description of aim 4 motivation - concrete example 4 applicability - where is it useful? 4 structure - “blueprint” diagram 4 participants - each class in pattern 3

Elements of a Design Pattern (cont’d) 4 collaborations - relationships among participants 4 consequences - implications of use problem areas 4 implementation - general implementation 4 sample code - code for a particular application 4 known uses - applications where used 4 related patterns - to combine or substitute 4

Example Pattern: Composite 4 Structural pattern 4 Represent part-whole relationships in tree hierarchy 4 Uniform treatment of individual objects 5

Composite: Application Piece Part. Type Get. Type() int Get. Identifier() int Get. Time() float Get. Quality() int Get. Number. Of. Rejections() Set. Reject. Status(bool) bool Get. Reject. Status() sub-parts * Compound. Part Add. Part(Part) int Get. Number. Of. Parts() Part * Decompose() Part. Type Get. Type() int Get. Identifier() int Get. Time() float Get. Quality() int Get. Number. Of. Rejections() Set. Reject. Status(bool) bool Get. Reject. Status() 6

Composite: Application Part. Type Get. Type() int Get. Identifier() int Get. Time() float Get. Quality() int Get. Number. Of. Rejections() Set. Reject. Status(bool) bool Get. Reject. Status() sub-parts * Compound. Part Add. Part(Part) int Get. Number. Of. Parts() Part * Decompose() 7

Composite Pattern Example: Simulation Components Sim. Component Part. Type Get. Type() int Get. Identifier() int Get. Time() float Get. Quality() int Get. Number. Of. Rejections() Set. Reject. Status(bool) bool Get. Reject. Status() Contains 1 or more Components in a variable called sub-pieces Inheritance Piece Composite Part. Type Get. Type(). . . all of the ops(). . . all of ops(). . . Add. Part(Part) int Get. Number. Of. Parts() Part * Decompose() sub-pieces 8

Composite Pattern Example: A Graphic Inheritance Line Draw() Rectangle Draw() Add(Graphic) Remove(int) Get. Child(int) Text Picture Draw() Add(Graphic) Remove(int) Get. Child(int) Add/Remove Graphic to/from list of graphics Contains 1 or more Graphics in a variable called graphics for all g in graphics g. Draw() 9

Composite Pattern Example: A Graphic Mail. Body Disp() Add(Attch) Remove(int) Get. Child(int) Text Disp() Image Disp() Pic Attachments Disp() Add(Attch) Remove(int) Get. Child(int) Alternatively: Display a list of attachments attchs for all m in attchs m. Disp() 10

Composite: General Structure 11

Observer 4 Behavioral pattern 4 When changed, Subject notifies Observers 12

Observer: Structure 13

Observer: Sequence Diagram 14

Observer: Application Unit Add. Part(Part *) Get. Number. Final. Parts() Part * Get. Final. Part() Set. Types. Required(Part. Type *, int num) Add. Unit. Observer(Unit. Observer *) Notify. Observers() // called when unit completes a part for all o in observers o. Update(this) Observer Update() Unit. Observer Update() Update(Unit *) Product. Removal. Observer Update(Unit *) 15

Creational Patterns 4 Abstract Factory 4 Builder 4 Factory Method 4 Prototype 4 Singleton 16

Structural Patterns 4 Adapter 4 Bridge 4 Composite 4 Decorator 4 Facade 4 Flyweight 4 Proxy 17

Behavioral Patterns 4 Chain of Responsibility 4 Command 4 Interpreter 4 Iterator 4 Mediator 4 Memento 4 Observer 4 State 4 Strategy 4 Template Method 4 Visitor 18
- Slides: 18