SADP Module 3
SADP Module 3
SADP Module 3
Design pattern
18CS731
Behavioral Patterns
➢Applicability : When more than one object may handle the request, and the
handler isn’t known a priori.
• When you want to issue a request to one of several objects without
specifying the receiver explicitly.
➢Structure :
➢Participants :
• Client : Initiates the request to the concrete handler object on the chain.
• Handler (help handler) : Defines the interface for handling requests and
implements the successor link.
• Concrete Handler (Print button) : It handler the request it is responsible for
and can access its successors.
➢Motivation :
➢Applicability : Commands are an object-oriented replacement for callback.
• Specify, queue and execute request at different times.
• It support undo operation.
➢Structure :
➢Participants :
• Client : Creates a concrete command object and sets its receiver.
• Receiver (Application) : Know how to perform the operation associated
with carrying out a request.
• Concrete command (open command) : Defines a binding between object
and an action.
• Command : Declares an interface for executing an operation.
• Invoker (Menu Item) : Asks the command to carry out the request.
➢ Collaboration : The client creates a concrete command object and specify its
receiver and then invoker object stores the concrete command object . The
invoker issues the request by calling execute on the command and concrete
command object invokes operation on ite receiver to carry out the request.
➢Consequences : Command decouple the objects that invokes the operation from
the one that knows how to perform it.
• Commands are first class objects.
• It’s easy to add new commands, because you don’t have to change existing
classes.
➢Applicability : The interpreter pattern works best when the grammar is simple.
• Efficiency is not critical concerns.
➢Structure :
➢Participants : Client : Invokes the Interpreter operations.
• Abstract Expression (Regular Expression) : Declares an abstract interpret
operation that is common to all nodes in the abstract syntax tree.
• Terminal Expression (Literal Expression) : Implements an interpret
operation associated with terminal symbols in the grammar.
• Non Terminal Expression (Alternation Expression) : Implements an
interpret operation for nonterminal symbols in the grammar.
• Context : Contains information that’s global to the interpreter.
➢Structure :
➢Participants :
• Aggregate : Defines an interface for creating an iterator object.
• Iterator : Defines an interface for accessing and traversing elements.
• Concrete Iterator : Implements the iterator interface.
• Concrete Aggregate : Implements the iterator creation interface to return
an instance of the proper concrete iterator.
➢Related patterns : The related patterns to the iterator are Composite, Factory
method and memento.
Mediator
➢Intent : Defines an object that encapsulates how a set of objects interact.
➢Structure :
➢Participants : Originator (Constraint Solver) : Creates a memento containing a
snapshots of its current internal state.
• Memento (Solver state) : Stores internal state of the originator object
• Caretaker (Undo mechanism) : It is responsible for the memento’s
safekeeping.
➢Related patterns : Commands can use memento to maintain state for undoable
operations.
• Memento’s can be used for iteration as described earlier.
Observer
➢Intent : Defines a one-to-many dependency between objects so that when
one object changes state, all its dependents are notified and updated
automatically.
➢Structure :
➢Participants :
• Subject : Knowns its observer. Any number of observer objects may observe
a subject.
• Observer : Defines a updating interface for objects that should be notified of
changes in a objects.
• Concrete Subject : Store state of interest to concrete observer objects.
• Concrete observer : It maintains a references to a concrete subject objects.
➢Related patterns : Mediator and singleton are the related pattern for observer.
State
➢Intent : Allow an object to alter its behavior when its internal state changes.
➢Structure :
➢Participants :
• Context (TCP Connection) : Defines the interface of interest to clients.
• State (TCP state ) : Defines an interface for encapsulating the behavior
associated with a particular state of the content.
• Concrete State subclasses (TCP established) :Each subclass implements a
behavior associated with a state of the context.
➢Related pattern : The flyweight patters explains when and how state objects can
be shared
Template method
➢Intent : Template method lets subclasses redefine certain steps of an
algorithm without changing the algorithm's structure.