370 likes | 489 Views
Chapter 26. Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?. Adaptor. Resolve incompatible interfaces i.e. use an existing interface that cannot be changed, a third party “Use a level of indirection to create a consistent interface.”.
E N D
Chapter 26 Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
Adaptor • Resolve incompatible interfaces • i.e. use an existing interface that cannot be changed, a third party • “Use a level of indirection to create a consistent interface.”
Fig. 26.1 Adaptor How is this different from Fig. 25.1?
Fig. 26.2 Using the adapter Compare to Fig. 25.10
GRASP vs. GoF • GRASP is at work in other patterns • Look for underlying principles, such as “Façade Object”
Fig. 26.3 • The Goals: • Protection at a variation point • Low Coupling
Factory Pattern Object Creation issues • Who creates the adapter or interface? • How do you know which adapter/interface to create? • How to maintain High Cohesion? Who should be responsible for creating complex objects?
Factory is a Common Solution • Pure Fabrication • Hide complex creation logic • Separate responsibilities into a cohesive helper • Memory management • Performance management
Singleton • Some class should only have one object instance at runtime • Usual implementation is a static method • Supports global visibility • Supports single access point If instance == null …. If not null, then use the existing instance object.
Strategy • Design for varying but related algorithms or policies • Design to change/ swap them out. • Make each a separate class, but with a common interface.
Strategy in Collaboration Context object passes a reference to itself (this) to the Strategy Object.
Fig. 26.11 “pricingStrategy” association is a method invocation
Fig. 26.13 How is this different from Adapter? Factory decides which adapter to create.
Composite • How to treat a group of objects the same as an atomic object • Use the same interface • Implement composite object to hold a list of atomic objects • Best discount by: • Time • Customer type • product
The Composite Pattern Outer Object Inner Objects
Fig. 26.17 Store-defined discount, when sale is created
Fig. 26.18 Customer-type discount, when customer ID is entered.
Passes the customer object, rather than the customer ID. Why? Fig. 26.19 How customer-type discount is applied. An alternative to Fig. 26.17.
Facade • Common unified interface • Allows for easy customization • Make a single point of contact to wrap the subsystem
Observer • Publish – Subscribe • Delegation Event • Standard stuff, but from the OOD POV. Update the GUI window when the sale total changes.
Fig. 26.22 How are the various methods invoked?
Fig. 26.23 SSD explains how the various methods are invoked. Follow the data flows.
Fig. 26.26 How is this different from Fig. 26.22?
Fig. 26.27 Publish-Subscribe example: alarm clock.
Summary • Why are patterns used? • What is the designers goal? • How do you follow the data flows? • How many core patterns do you think there are?