Download, print and study this document offline |
Page 1 CSE 403 Design Patterns (Part 2) Page 2 CSE 403 Design Patterns (Part 2) Outline Introduction to design patterns Creational patterns (constructing objects) Creational patterns (constructing objects) Structural patterns (controlling heap layout) Bh i l tt (ffti bjt ti ) ? ? Behavioral patterns (affecting object semantics) ? Slide 2 Page 3 CSE 403 Design Patterns (Part 2) Outline Introduction to design patterns Creational patterns (constructing objects) Creational patterns (constructing objects) Structural patterns (controlling heap layout) Bh i l tt (ffti bjt ti ) ? ? Behavioral patterns (affecting object semantics) ? Slide 2 Structural patterns: Wrappers The wrapper translates between incompatible interfaces Wrappers are a thin veneer over an encapsulated class Wrappers are a thin veneer over an encapsulated class modify the interface extend behavior restrict access The encapsulated class does most of the work p Pattern Functionality Interface Adapter same different Decorator different same Decorator different same Proxy same same Slide 3 Page 4 CSE 403 Design Patterns (Part 2) Outline Introduction to design patterns Creational patterns (constructing objects) Creational patterns (constructing objects) Structural patterns (controlling heap layout) Bh i l tt (ffti bjt ti ) ? ? Behavioral patterns (affecting object semantics) ? Slide 2 Structural patterns: Wrappers The wrapper translates between incompatible interfaces Wrappers are a thin veneer over an encapsulated class Wrappers are a thin veneer over an encapsulated class modify the interface extend behavior restrict access The encapsulated class does most of the work p Pattern Functionality Interface Adapter same different Decorator different same Decorator different same Proxy same same Slide 3 Adapter Change an interface without changing functionality rename a method convert units implement a method in terms of another Example: angles passed in radians vs. degrees Slide 4 Page 5 CSE 403 Design Patterns (Part 2) Outline Introduction to design patterns Creational patterns (constructing objects) Creational patterns (constructing objects) Structural patterns (controlling heap layout) Bh i l tt (ffti bjt ti ) ? ? Behavioral patterns (affecting object semantics) ? Slide 2 Structural patterns: Wrappers The wrapper translates between incompatible interfaces Wrappers are a thin veneer over an encapsulated class Wrappers are a thin veneer over an encapsulated class modify the interface extend behavior restrict access The encapsulated class does most of the work p Pattern Functionality Interface Adapter same different Decorator different same Decorator different same Proxy same same Slide 3 Adapter Change an interface without changing functionality rename a method convert units implement a method in terms of another Example: angles passed in radians vs. degrees Slide 4 Adapter example: scaling rectangles interface Rectangle { // grow or shrink this by the given factor void scale(float factor); ... float getWidth(); float area(); } class myClass { void myMethod(Rectangle r) { l(2) ... r.scale(2); ... } } Could we use this class instead? class NonScaleableRectangle { void setWidth(float width) { ... } (){} void setHeight(float height) { ... } ... } Slide 5Read More