TDDB84: Lecture 6 Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command
Creational Abstract Factory Singleton Builder Structural Composite Proxy Bridge Adapter Template method Behavioral Iterator Mediator Chain of responsibility Observer Observer LE3 LE4 LE5 LE6 Creational patterns Structural patterns LA1 LA2 LA3
Creational Abstract Factory Singleton Builder Structural Composite Proxy Bridge Adapter Template method Behavioral Iterator Mediator Chain of responsibility Observer Observer LE3 LE4 LE5 LE6 Creational patterns Structural patterns LA1 LA2 LA3
Adapter
Class Adapter Object Adapter
Multiple back-end objects Client Target do() Adapter -adaptee1 -adaptee2 do() Adaptee1 +perform() Adaptee2 +perform()
Multiple back-end methods Client Target do() Adapter -adaptee do() Adaptee1 +foo() +bar() adaptee.foo() adaptee.bar()
public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly(); } public class TurkeyAdapter implements Duck { Turkey turkey; } public TurkeyAdapter(Turkey turkey) { this.turkey = turkey; } public void quack() { turkey.gobble(); } public void fly() { for(int i=0; i < 5; i++) { turkey.fly(); } } public class DuckAdapter implements Turkey { Duck duck; Random rand; } public DuckAdapter(Duck duck) { this.duck = duck; rand = new Random(); } public void gobble() { duck.quack(); } public void fly() { if (rand.nextint(5) == 0) { duck.fly(); } }
Demo
Adapter: consequences
Adapter: consequences + Isolates interface changes to the adapter class
Adapter: consequences + Isolates interface changes to the adapter class - Class adapters require target interfaces or multiple inheritance in the language
Bridge
TV
TV Samsung LG
TV Samsung LG Logitech Harmony On() Off() On() Off()
TV Samsung LG Logitech Harmony On() Off() On() Off() One For All On() Off() On() Off()
TV Samsung LG Logitech Harmony On() Off() On() Off() Remote One For All On() Off() On() Off()
Message type
Message type Password recovery Signup
Message type Password recovery Signup E-mail Send() Send()
Message type Password recovery Signup E-mail Send() Send() SMS Send() Send()
Message type Password recovery Signup E-mail Send() Send() Transmission type SMS Send() Send()
Abstraction == That which we (should) care about
Bridge Strategy Intent Collaborations Decouple two class hierarchies (abstraction/ implementation) The Bridge forwards requests to the Implementor Allow for exchangeable algorithms The Context and Strategy collaborate, passing data between them
Bridge Adapter Intent Decouple two class hierarchies (abstraction/ implementation) Convert an existing class to fit a new interface Applicability In a new system In an existing system
Demo
Design principles Encapsulate what varies Program to an interface, not to an implementation Favor composition over inheritance Classes should be open for extension but closed for modification Don t call us, we ll call you Depend on abstractions, do not depend on concrete classes Classes should only have one reason to change
Bridge: consequences
Bridge: consequences + Lets two class hierarchies with common superclasses vary independently
Bridge: consequences + Lets two class hierarchies with common superclasses vary independently - If some implementation classes do not support an abstract concept, the abstraction breaks
Observer
Weather Station Humidity Display Average Temp Display
Weather Station Humidity Display Average Temp Display subscribe() subscribe()
Weather Station Humidity Display Average Temp Display subscribe() subscribe() publish() publish()
Weather Station Humidity Display Average Temp Display subscribe() subscribe() publish() publish() unsubscribe() publish()
Subject
Subject Concrete Observer
Demo
Mediator vs Observer
Design principles Encapsulate what varies Program to an interface, not to an implementation Favor composition over inheritance Classes should be open for extension but closed for modification Don t call us, we ll call you Depend on abstractions, do not depend on concrete classes Classes should only have one reason to change Strive for loosely-coupled design
Chain of Responsibility
SPAM Filter isspam(message)
SPAM Filter isspam(message) reject Message rejected
SPAM Filter isspam(message) accept Size Filter istoolarge(message) reject Message rejected
SPAM Filter isspam(message) accept Size Filter istoolarge(message) accept Sorting Filter sort(message) reject reject Message rejected
SPAM Filter isspam(message) accept Size Filter istoolarge(message) accept Sorting Filter sort(message) process reject reject Message arrived Message rejected
SPAM Filter isspam(message) accept Size Filter istoolarge(message) accept Sorting Filter sort(message) process reject reject Message arrived Message rejected
Examples Logging Input management in GUI:s
Demo
CoR: consequences
CoR: consequences + provides the Observer with more control over invocation of targets
CoR: consequences + provides the Observer with more control over invocation of targets - A handler does not know if it will receive a message, depending on the behavior of other handlers in the chain
Memento
Iterative Optimizer iteration current target value current solution Optimize() Abort() GetState() SetState() SolverMemento iteration current target value current solution Client - memento - optimizer Optimize() Abort() ResetOptimizer(SolverMemento)
Iterative Optimizer iteration current target value current solution Optimize() Abort() GetState() SetState() SolverMemento iteration current target value current solution Client - memento - optimizer Optimize() Abort() ResetOptimizer(SolverMemento)
Mementos in GUI:s - Undo/Redo
Mementos in GUI:s - Undo/Redo 1 gi User Name Ginnie Age 24
Mementos in GUI:s - Undo/Redo User Name Age gi Ginnie 24 1 User Name Age 2 gi jo Ginnie Johnny 24 37
Mementos in GUI:s - Undo/Redo User Name Age gi Ginnie 24 1 2 gi jo User Name Ginnie Johnny Age 24 37 3 Undo Redo Cut Copy Paste Paste special... Delete Select All
Mementos in GUI:s - Undo/Redo User Name Age gi Ginnie 24 1 2 gi jo User Name Ginnie Johnny Age 24 37 3 Undo Redo Cut Copy Paste Paste special... 4 gi User Name Ginnie Age 24 Delete Select All
Demo
Memento: consequences
Memento: consequences + Can externalize object state for later restoration within the lifetime of the object
Memento: consequences + Can externalize object state for later restoration within the lifetime of the object + Encapsulates access to the objects inner state
Memento: consequences - + Can externalize object state for later restoration within the lifetime of the object + Encapsulates access to the objects inner state Depending on implementation, access to private fields requires memento classes as inner/friend classes to each domain class
Command
Command Client Waiter Order Chef Order Place Order Cook
Remote control Joe s Ultimate Remote Control On Off On Off On Off On Off On Off On Off On Off Undo TDDB84 Design Patterns
Remote control Joe s Ultimate Remote Control Fan High() Low() Off() GetSpeed() On Off On Off On Off On Off On Off On Off On Off Undo TDDB84 Design Patterns
Remote control Joe s Ultimate Remote Control Fan High() Low() Off() GetSpeed() On Off On Off On Off On Off On Off On Off On Off Undo TV TurnOn() TurnOff() SetChannel() TDDB84 Design Patterns
Remote control Joe s Ultimate Remote Control Fan High() Low() Off() GetSpeed() On Off On Off On Off On Off On Off On Off On Off Undo On() Off() Dim() Lamp TV TurnOn() TurnOff() SetChannel() TDDB84 Design Patterns
Client Invoker -command perform() Command +execute() Receiver +perform() Concrete Command -receiver
Invoker
Client
Concrete Commands
Command
Receiver
Demo
Command: consequences
+ Allows extensions of commands Command: consequences
+ Allows extensions of commands Command: consequences + Decouples the execution from the specification of the command
Command: consequences + Allows extensions of commands + Decouples the execution from the specification of the command - Bad design if not needed!
Command: consequences + Allows extensions of commands + Decouples the execution from the specification of the command - - Bad design if not needed! May be confusing if it removes the receiver from responsibilities
Next lecture
Creational Prototype Structural Facade Flyweight Behavioral Command Interpreter Memento State Visitor LE7 LE8 LE9 Several different patterns GUI apps LA4 LA5
Creational Prototype Structural Facade Flyweight Behavioral Domain-specific languages Command Visitor Interpreter Fluent API:s as a demo of the Façadeprinciple Program to an interface, e.g. Memento State {1,2,3}.ShouldNot().Contain(EvenNumber) How language design can make design patterns disappear (open classes, multiple dispatch, eval) LE7 LE8 LE9 Several different patterns GUI apps LA4 LA5