Professional Documents
Culture Documents
Part-1
Purpose
Creational Structural Behavioral
Interpreter
Class Factory Method Adapter
Template Method
Chain of Responsibility
ShoppingMallContext
Client Class
• This class is the main user of the functionality or the
particular algorithm. It requests the exact strategy
based on certain requirements and conditions. Client
class should first invoke the Context class in order
to grab the decided strategy at runtime.
Client Class
Advantages of Strategy Pattern
• This gives a greater flexibility and reuse
• It is possible to eliminate large conditional
statements using this pattern
• Since this is dynamic we don’t need to find
alternatives to painful hard coding
• Easier to maintain the different behaviors
since they are encapsulated in separate classes.
• There are wide variety of selections for same
behavior.
Challenges of Strategy Pattern
This will increase the number of objects in
the system which will lead to performance
issues later.
There will be additional operational cost
due to interaction between strategies and
their context.
• Strategy pattern gives you the ability to stay
independent and provide space for all
possible approaches to come into the play when
designing a software system.
• It is a great way to enhance the versatile
nature of a system.
Command Pattern
Command Pattern
• In this pattern an object is used to encapsulate
all information needed to perform an action or
trigger an event at a later time.
This information includes ......
The method name,
The object that owns the method and
Values for the method parameters.
The GoF definition
‘Encapsulate a request as an object, thereby letting
you parameterize clients with different requests,
queue or log requests, and support undo of
operations’
What is ‘Command’ Object?
• This is the intermediary object that links the
request invoker, which triggers the request and
the request receiver, which handle the
request by performing several operations.
2. Concrete Command
3. Invoker
4. Receiver
5. Client
Command
• Represents the common interface for all concrete
commands. This is the core of the contract and must
contain the execute() equal method to pass the
trigger.
Concrete Command
• This class extends the command interface and
implements the execute() method with actual
operations to be performed in order to fulfil the
client request. It also creates a binding between
the action and the receiver. Often concrete command
uses the external object ‘Receiver’ to store and use
the required parameters to execute the methods.
Invoker
• This is the main entry point to execute a
complex set of commands.
• Client calls the invoker’s execute command method
to access the command interface. Invoker stores a
reference of a command object to trigger the
operation executed when required. It is unaware of
the concrete command and does not create any
command objects, but instruct the available
command object to carry out the request.
Receiver
• This object knows how to perform the actual
operations and carry out the actions to fulfil
the request. That means it contains the business
logic or data related to response creation.
Client
• The client instantiates a concrete command object
and passes those to appropriate invokers. It creates
the invoker and receiver objects as well. Then it
checks and decides which receiver objects link with
the command objects and which commands to link
with the invoker. In addition, it decides which
command to execute at which points
Command Pattern Example
• Let’s assume a smart home electrical unit
system that controls by a universal remote.
• There are two kinds of systems, which are
operated by the universal remote.
• Those two are the living room light system and
Air Conditioner.
• We will include those two sub-systems into a
super system as ‘Home Electronics’.
• Universal remote has two buttons as ‘On’ and
‘Off’. When the homeowner press ‘On’ both the
light system and air-conditioned activates.
Let’s apply the command pattern to this
scenario,
• Command: Command interface to initiate the
execution and abstract the concrete commands.
• OnCommand: Concrete command class that
implements the ‘On’ operations for the
‘HomeElectronics’
• OffCommand: Concrete command class that
implements the ‘Off’ operations for the
‘HomeElectronics’
• UniversalRemote: UniversalRemote class will
maintain all the receivers and will provide a
method to identify the currently active
receiver.
• HomeElectronics: Parent class for ‘LightSystem’
and ‘Airconditioner’ home electronic systems.
• LightSystemReceiver: Act as a ‘Receiver’ object
for the scenario, contains actual method
implementations to execute the required functions.
• Air-condition Receiver: Act as the ‘Receiver’
object for the scenario, contains actual method
implementations to execute the required functions.
• Button Invoker: This class act as the invoker for
any action.
• Home Owner Client: The client who needs the
service of receivers.
How to Implement the Pattern in Example
• Define the base interface ‘Command’ with the ‘execute()’
similar method
• Create concrete classes implementing the ‘Command’
interface and including a ‘Receiver’ object instance with
appropriate arguments to perform the concrete execution
• Include an instance of ‘Command’ inside the ‘Invoker’
object
• The client creates the ‘Command’ and ‘Invoker’ instances
and passes a command instance to the invoker instance
• Then invoker decides and triggers the ‘execute()’ method
at the required time
• Finally, receivers get the signal and execute the actual
methods to cater the request
High-Level Diagram
When to use the Command Pattern
• When the requests need to be handled in certain
time occurrences and according to different
triggers situations.
• When the client and the service provider needs
to be decoupled.
• When there is a need for rollback
functionality for certain operations.
• When the history of requests required.
operation
object collection
Components of the Visitor Pattern
Visitor
Concrete Visitor
Element (Visitable)
Concrete Element (Concrete Visitable)
Object Structure
Client
Visitor
• This is the interface or abstract class, which
declares the common interface for all types of
visitors.
• The visitor declares a bunch of visiting methods that
accept various concrete components as parameters.
This will be somewhat similar to the overloading
mechanism but the types of parameters should be
different hence the operations are totally different.
Visit operations will be performed on each concrete
element in the object structure via the visit()
method. The input concrete element type will decide
the method to be called.
Concrete Visitor
• These are the concrete classes, which implement
all the operations described in the common visitor
interface.
• This each concrete visitor represents a
single behaviour of the object structure that is
each visitor is identical with different operations.
• Each concrete visitor will have a natively behaving
visit() method implementation.
Element (Visitable)
• This is an abstract component to represent the accept
operation of the visitor. It contains the accept() method
and should accept visitor objects as arguments. This is
considered as the entry point for the component
object to be ‘visited’ by the visitor object.
Concrete Element (Concrete Visitable)
• Concrete elements contain the actual
implementation of the abstract acceptance method
from the ‘Element’ component. Accept method will
take a visitor object as an argument in order to redirect
the call to a proper visitor’s method corresponding to
the current component class.
Object Structure
• The object structure is a class containing all the
element objects, which offers a mechanism to
iterate through all the elements.
• This object structure can be a collection or a
complex structure like a composite object.
Client
• The client does not know the concrete class
structure of the component and just call the
accept() method.
Example for Visitor Pattern
• Let’s assume a credit card scenario in a bank.
• The bank issues three types of credit cards for the
customers.
• Those cards get different types of offers
depending on the type of card.
• Card types are Silver, Gold and Platinum credit
cards and the offer types are Dining,
Supermarket, Hotel, and Travel.
• Bank system has to implement a system to
incorporate those offer types to appropriate credit
card types. Visitor design pattern suits best
to solve this design scenario in following way.
• Design pattern related class breakdown for the
credit card and offer types scenario,
• Credit Card Visitor: This is the base interface,
which acts as the ‘Visitor’ abstract component in the
design pattern.
• Offer: This is the base ‘Element’ interface for the
offer types object structure in the bank
• Following are the concrete visitor classes to
implement the abilities of each credit card type.
• Silver Credit Card Visitor / Gold Credit Card
Visitor
• Platinum Credit Card Visitor / Dinning Offer
Following are the concrete offer type classes to
differentiate each offer validities.
Dining Offer/ Super market Offer / Hotel Offer /
Travel Offer
• ‘Offer’ interface allows the ‘Credit Card Visitor’
interface to pass the right object so the correct
operation occurs on the right type of object. The
accept() method in ‘Offer’ interface pass the same
abstract visitor object and later specialized version
of visit() method is called. Method overloading is
used at this position to select the appropriate visit()
method.
Class Diagram for the Credit Card Offers
System
Code samples for credit card offers scenario
When to use the Visitor Design Pattern
• When there is a complex object structure with
many classes and different interface and client
needs to perform several operations on these objects
depending on their concrete classes.
• When the operations are distinct and
unrelated with one another it’s easy to keep
organized the classes and operations using this
pattern.
• When the classes in the object structure rarely
change but there is a need to define new operations
over the structure.
• When there is a need for adding a new
operation to the visitor hierarchy or new
component to the object structure we can achieve it
by not polluting the existing design
Visitor Pattern Limitations
• It’s necessary to know the return type of visit()
methods at the time of designing the pattern otherwise
we have to change the interface and all of its
implementations.
• It contains many implementations for the visitor
interface, which makes it harder to extend.
• If one new element object is added to the system,
we have to change all the implemented visitor
classes.
Thank You..