Design-Patterns (C++ Examples)
Design-Patterns (C++ Examples)
<<Interface>>
SomeApp Shape
Square Circle
<<creates>>
Fan
FanSwitch +turnOn
+turnOff
A bad way to extended Switch
It also violates the DIP
FanSwitch still inherits the dependency upon Light.
Light
+turnOn
+turnOff
ABSTRACT SERVER solution to the Table Lamp problem
It satisfies both the DIP and the OCP
But there is a potential violation of the Single-Responsibility
Principle (SRP: A class should only one reason to change)
We have bound together two things, Light and Switchable, that may
not change for the same reasons.
What if we purchased Light from a third party?
Object Oriented Analysis and Design 31
Adapter Pattern – Switch example
<<Interface>>
Switchable
Switch +turnOn
+turnOff
Solving the Table Lamp problem with the class form ADAPTER
Client
<<Interface>>
ServicesEnumeration RegiteredServices
Enumeration
Modem Problem
Problem:
Suppose that there were hundreds of modem clients all making happy
use of the Modem interface.
Now suppose that customer have given us a new kind of modem that
don’t dial - dedicated modem.
There are several new applications (Ded Users) that use these
dedicated modems and don’t bother to dial.
All the current modem clients to be able to use these dedicated
modems and needn’t to modify their applications.
Object Oriented Analysis and Design 41
Adapter Pattern – Modem example
<<Interface>>
Hayes Modem
Dialler
+dial
Robotics Modem +hangup Modem
<<Interface>> Clients
Ernie’s Modem Modem
+send Ded Users
Ideal solution to the +receive
Modem Problem
Dedicated Modem
Problem:
Unfortunately this requires us to make changes to all the
modem clients – something that our customer forbade.
<<Interface>> Modem
Hayes Modem
Modem Clients
+dial
Robotics Modem +hangup
+send
Ernie’s Modem +receive
Dedicated
Modem Ded
Dedicated Modem <<delegates>>
+send
Adapter +receive Users
Dial and Hangup are implemented to simulate Solving the Modem Problem
connection state. Send and Receive are delegated
with ADAPTER
to DedicatedModem
Modem
DialModem DedicatedModem
Car CarManufacturer
: Assembly
Contains Contains
: Assembly
: Part
Contains Contains
: Part : Part
: CatalogueEntry : CatalogueEntry
Component
+ cost() : double n
ScrollDecorator BorderDecorator
scrollPosition borderWidth
draw() draw() super.draw()
scrollto() drawBorder() drawBorder()
Motivation: Two File Managers are both observing the same Directory; the user
deletes a subdirectory using File Manager A; we want File Manager B to
immediately and automatically get updated, reflecting the change...
Applicability:
– When there are multiple views of a model (subject) that need to stay in sync.
No view should know about any other.
– When an object needs to communicate to other objects of unknown type (but known
Observer interface) it can notify them.
Pros:
– Promotes loose coupling between objects.
– Excellent communication protocol.
– Avoids polling
Cons:
– None. Knowledge of this pattern is essential.
Command object:
class Hello implements Command {public void execute() {System.out.print("Hello ");}}
class World implements Command {public void execute() {System.out.print("World! ");}}
class IAm implements Command {
public void execute() {System.out.print("I'm the command pattern!");}
}
Command interface:
interface Command { void execute();}
ConcreteCommand
implements execute() by
Client instantiates the calling corresponding
ConcreteCommands and operation(s) in Receiver.
sets its Receiver.
Receiver knows
how to perform the
operation.
Object Oriented Analysis and Design 94
Example: Application independent Menus
Command interface:
public class WmvcExecutor{
public void execute(ActionEvent event){}
public void execute(ItemEvent event){}
public void execute(ChangeEvent event){}
}
Object Oriented Analysis and Design 96
Chain of Resposibility Pattern - Motivation
Consider a context-sensitive help facility for a GUI
The user can obtain help information on any part of the interface just by
clicking on it.
The help that's provided depends on the part of the interface that's
selected and its context.
Problem
How to decouple the button that initiates the help request from the
objects that might provide help information?
Solution
to decouple senders and receivers by giving multiple objects a chance
to handle a request. The request gets passed along a chain of objects
until one of them handles it.
The first object in the chain receives the request and either handles it or
forwards it to the next candidate on the chain, which does likewise.
The object that made the request has no explicit knowledge of who will
handle it
class HelpHandler {
private: HelpHandler* _successor;
public: HelpHandler ( HelpHandler* h, Topic t ) : _successor(h), _topic(t) { }
virtual void HandleHelp() { if (_successor != 0) { _successor->HandleHelp(); } }
…….};
<<interface>>
Modem
+dial
+hangup
+send
+receive
UnixModemConfigura
Hayes Zoom Ernie tor
System.out.println("'"+o.toString()+"'");
else if (o instanceof Float)
System.out.println(o.toString()+"f");
else
System.out.println(o.toString());
}
Visitor pattern
In this example, notice that client’s of NumberCruncher do not know about the
different crunch algorithms. The NumberCruncher.crunch() method is free to
decide which CrunchImpl to use at any time; new algorithms can be easily added.
Object Oriented Analysis and Design 136
Applying a Strategy Pattern in a Database Application
coin / unlock
pass / lock
Unlocked coin / thankyou
Structure