More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How...
Transcript of More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How...
![Page 1: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/1.jpg)
More GoF Design Patterns: Composite, Façade, and ObserverCurt Clifton
Rose-Hulman Institute of Technology
![Page 2: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/2.jpg)
GoF Pattern TaxonomyBehavioral
Interpreter Template MethodChain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategy
Visitor
CreationalFactory Method
Abstract FactoryBuilderPrototypeSingleton
StructuralAdapter
BridgeCompositeDecoratorFaçadeFlyweightProxy
![Page 3: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/3.jpg)
Composite
How could we handle multiple, conflicting pricing policies?
Such as…
20% senior discount
Preferred customer discount, 15% off sales of $400
Manic Monday, $50 off purchases over $500
Buy 1 case of Darjeeling tea, get 15% off entire order
![Page 4: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/4.jpg)
Composite Pattern
Problem: How do we handle the situation where a group of objects can be combined but should still support the same polymorphic methods as any individual object in the group?
Solution: Define a composite object that implements the same interface as the individual objects.
Q1,2Example…
![Page 5: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/5.jpg)
![Page 6: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/6.jpg)
![Page 7: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/7.jpg)
Example Continued:Dynamic Use
![Page 8: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/8.jpg)
How do we build the Composite Strategy?
Three places in example where new pricing strategies can be added:
When new sale is created, add store discount policy
When customer is identified, add customer-specific policy
When a product is added to the sale, add product-specific policy
![Page 9: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/9.jpg)
Adding Store Discount PolicySingleton, Factory
Makes a Composite to begin with. Why?
![Page 10: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/10.jpg)
Adding Customer Specific Discount Policy
New system operation from alternative use
case flowRecall: What’s a
ref frame?
![Page 11: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/11.jpg)
Adding Customer Specific Discount Policy (continued)
Where did ps come from?
How does Factory know that ps is a composite? Q3
![Page 12: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/12.jpg)
Applying Composite
Q4
![Page 13: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/13.jpg)
Façade
NextGen POS needs pluggable business rules
Assume rules will be able to disallow certain actions, such as…
Purchases with gift certificates must include just one item
Change returned on gift certificate purchase must be as another gift certificate
Allow charitable donation purchases, but max. of $250 and only with manager logged-in
More general than just Façade Controllers
![Page 14: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/14.jpg)
Some Conceivable Implementations
Strategy pattern
Open-source rule interpreter
Commercial business rule engine
![Page 15: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/15.jpg)
Façade
Problem: How do we avoid coupling to a part of the system whose design is subject to substantial change?
Solution: Define a single point of contact to the variable part of the system—a façade object that wraps the subsystem.
Q5
![Page 16: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/16.jpg)
Sale methods would be designed to check in with the façade
![Page 17: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/17.jpg)
Observer
How do we refresh the GUI display when the domain layer changes without coupling the domain layer back to the UI layer?
Model-View Separation
![Page 18: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/18.jpg)
Observer(aka Publish-Subscribe)
Problem: Suppose some subscriber objects want to be informed about events or state changes for some publisher object. How do we achieve this while maintaining low coupling from the publisher to the subscribers?
Solution: Define an subscriber interface that the subscriber objects can implement. Subscribers register with the publisher object. The publisher sends notifications to all its subscribers.
![Page 19: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/19.jpg)
setTotal(Money newTotal)
…
…Sale
JFrame
SaleFrame( Sale sale )
…
…SaleFrame
Example: Update SaleFrame when Sale’s Total Changes
onPropertyEvent( source, name, value )
«interface»PropertyListener
onPropertyEvent( source, name, value )
addPropertyListener( PropertyListener lis )
sale.addPropertyListener(this);…
propertyListeners.add(lis);
propertyListeners *
publishPropertyEvent( name, value )
total = newTotal;publishPropertyEvent(“sale.total”, total);
for(PropertyListener pl : propertyListeners)! pl.onPropertyEvent(this, name, value);
if (name.equals(“sale.total”))! totalTextField.setText(value.toString());
![Page 20: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/20.jpg)
Example: Update SaleFrame when Sale’s Total Changes
![Page 21: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/21.jpg)
Example: Update SaleFrame when Sale’s Total Changes
setTotal(Money newTotal)
…
…Sale
onPropertyEvent( source, name, value )
«interface»PropertyListener
propertyListeners *
JFrame
SaleFrame( Sale sale )
…
…SaleFrame
onPropertyEvent( source, name, value )
publishPropertyEvent( name, value )addPropertyListener( PropertyListener lis )
{parameter}
Is UI coupled to domain layer?Is domain layer coupled to UI?
Q6,7
![Page 22: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/22.jpg)
Observer: Not just for GUIs watching domain layer…
GUI widget event handling
Example:JButton startButton = new JButton(“Start”);startButton.addActionListener(new Starter());
Publisher: startButton
Subscriber: Starter instance
Q8
![Page 23: More GoF Design Patterns: Composite, Façade, and Observer€¦ · Composite Pattern Problem: How do we handle the situation where a group of objects can be combined but should still](https://reader035.fdocuments.in/reader035/viewer/2022071103/5fdd16883289dd0f4b3aff89/html5/thumbnails/23.jpg)
GoF Pattern TaxonomyBehavioral
Interpreter Template MethodChain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor
CreationalFactory MethodAbstract FactoryBuilderPrototypeSingleton
StructuralAdapterBridgeCompositeDecoratorFaçadeFlyweightProxy
Q9