Design Patterns & Refactoring
Comparison of the Behavioral Patterns
Oliver Haase
HTWG Konstanz
Encapsulation of the Variation
Many behavioral patterns encapsulate the varying concept, clients are loosely coupled to the encapsulated concept:
behavioral pattern encapsulated concept
strategy algorithm
state state dependend behavior broker protocol between objects
iterator access to and traversal of collections
Without encapsulation, varying concept would be integrated into client object
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 2 / 7
Objects as Arguments
Some behavioral patterns define objects that are always used as arguments, e.g. visitor pattern → Visitoralways used as argument to accept-call of a Node.
Some patterns define object that is passed around as magic token without the clients’ knowledge of its content (Command object, Memento object).
Encapsulation or Distribution of Communication
Observer Pattern:
Communication is distributed across subjects (observables) and observers
Responsibility to preserve consistency is shared.
Communication flows at run-time are hard to understand
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 4 / 7
Encapsulation or Distribution of Communication
Broker Pattern:
Communication is centralized in broker (mediator) object Broker is responsible to preserve consistency
Broker is single point of failure and scalability bottle-neck Communication flows at run-time are easier to understand
Decoupling of Sender and Receiver
An object that uses another object gets dependent on it. The following patterns follow different approaches to minimize this coupling:
Command: Sender usesCommand object through a small, predefined interface, .i.e. executeoperation. Commandobject uses the receiver’s specific, possibly complex interface.
Observer: Subjects notify observers through a simplynotify operation, decoupling subjects from observers.
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 6 / 7
Decoupling of Sender and Receiver
Broker: Sender usesBrokerobject to communicate with receiver.
This decouples senders from receivers, but couples them to broker.
Chain of Responsibility: Client is decoupled from receivers through their internal chaining of the request.