After Mid-Terms
Design Pattern
Command, Observer, Strategy
Command Pattern
• Command pattern is a data driven design pattern and falls under
behavioral pattern category.
• A request is wrapped under an object as command and passed to
invoker object
Command Design Pattern
Command Design Pattern is used to encapsulate a request as an
object (Command) and pass it into an Invoker, where the does not
know how to serve the request but use the encapsulate command
to perform the action
• It separates the object that invokes the operation from the object
that actually performs the operation.
• It makes easy to add new commands, because existing classes
remain unchanged.
Definition
• GoF defines Command Pattern as “Encapsulate a request as an object,
thereby letting you parameterize clients with different requests, queue or
log requests, and support undoable operations.”
• The Client asks for a command to be
executed.
• The Invoker takes the command,
encapsulates it and places it in a queue, in
case there is something else to do first,
• and the ConcreteCommand that is in
charge of the requested command,
sending its result to the Receiver.
• Command pattern is a very good way of decrease the coupling
between sender and receiver. The most important thing to remember
while implementing the command pattern is that the Command is just
a link between sender and receiver. It should only tell the receiver
what the sender is expecting. It should never alter the logic of sender
and receiver in any way.
Participants
• The classes and objects participating in this pattern are:
• Command (Command)
• declares an interface for executing an operation
• ConcreteCommand (CalculatorCommand)
• defines a binding between a Receiver object and an action
• implements Execute by invoking the corresponding operation(s) on Receiver
• Client (CommandApp)
• creates a ConcreteCommand object and sets its receiver
• Invoker (User)
• asks the command to carry out the request
• Receiver (Calculator)
• knows how to perform the operations associated with carrying out the request.
How it works in real life example
A Customer place an order in restaurant to waiter
Client Command Invoker Receiver
(Order) (Waiter) (Chief)
Observer pattern
• Observer pattern is used when there is one-to-many relationship
between objects such as if one object is modified, its depenedent
objects are to be notified automatically. Observer pattern falls under
behavioral pattern category.
• With the observer pattern, we can subscribe certain objects, the
observers, to another object, called the observable. Whenever an
event occurs, the observable notifies all its observers!
• An Observer Pattern says that "just define a one-to-one dependency
so that when one object changes state, all its dependents are notified
and updated automatically“
• It describes the coupling between the objects and the observer.
• It provides the support for broadcast-type communication.
• An observable object usually contains 3 important parts:
• It describes the coupling between the objects and the observer.
• It provides the support for broadcast-type communication.
Real-World Example:
• If a bus gets delayed, then all the passengers who were supposed to
travel in it get notified about the delay, to minimize inconvenience.
Here, the bus agency is the subject and all the passengers are the
observers. All the passengers are dependent on the agency to provide
them with information about any changes regarding their travel
journey. Hence, there is a one-to-many relationship between the
travel agency and the passenger
Strategy pattern
• In Strategy pattern, a class behavior or its algorithm can be changed
at run time. This type of design pattern comes under behavior
pattern.
• In Strategy pattern, we create objects which represent various
strategies and a context object whose behavior varies as per its
strategy object. The strategy object changes the executing algorithm
of the context object.
• Strategy pattern is a behavioral design pattern that allows the
behavior of an object to be selected at runtime. It is one of the Gang
of Four (GoF) design patterns, which are widely used in object-
oriented programming.
• The Strategy pattern is based on the idea of encapsulating a family of
algorithms into separate classes that implement a common interface.
The pattern consists of three main components: the Context, the
Strategy, and the Concrete Strategy.
• More clean code because you separate the concerns into classes (a
class to each strategy).
• It’s easy to switch between different algorithms (strategies) in runtime
because you’re using polymorphism in the interfaces.