Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Behavioral design patterns

These design patterns are all about Class's objects communication.

 Chain of responsibility
A way of passing a request between a chain of objects

Example: A man asks a question to a student, if he doesn’t know the


answer, he passes the question to next person and so on. When one
student answers the question, the passing flow stops. Sometimes, the
passing might reach the last student and still, nobody gives the answer

 Command
Encapsulate a command request as an object

Example: In a Pizza hurt, the waiter will be there. the waiter will take
an order from the customer. The customer will tell what kind of pizza he wants.
The waiter will note it on the page. Then the waiter passes the page to the cook.
The cook will prepare pizza and given it back to the waiter and the waiter will
give it back to the customer. In this example, the customer is the client. Order i.e.
what kind of pizza the customer wants is the Command.

 Interpreter
A way to include language elements in a program

Example: Google Translator is an example of the interpreter design pattern


where the input can be in any language and we get the output in another
language.

 Iterator
Sequentially access the elements of a collection

Example:

 Mediator
Defines simplified communication between classes
Example: In chat application we can have several participants. It’s
not a good idea to connect each participant to all the others because the
number of connections would be really high. The best solution is to have
a hub where all participants will connect; this hub is just the mediator
class.

 Memento
Capture and restore an object's internal state

Example:

 Null Object
Designed to act as a default value of an object

Example:

 Observer
A way of notifying change to a number of classes
 Example: Leader who has many fans.each of these fans wants to get
latest updates of his leader.he follow leader as long as his
interest.when he loses interest he stop following the leader. Here the
fans are observer.
 State
Alter an object's behavior when its state changes

Example:

Our TV operated with remotes. We can change the state of TV by pressing


buttons on remote. But the state of TV will change or not, it depends on the
current state of the TV. If TV is ON, we can switch it OFF, mute or change
aspects and source. But if TV is OFF, nothing will happen when we press
remote buttons.For a switched OFF TV. only possible next state can be switch
ON.

 Strategy
Encapsulates an algorithm inside a class
Example: Person to go to the hospital. he can use Auto, or Train, or Taxi,
or Bus to go hospital. So, he has four options and he can choose any one option
to go hospital.As per the Strategy Design Pattern, for the particular task
(problem), there will be multiple solutions and from the solutions, the client will
choose only one solution at runtime. So, in our example traveling is the task and
person has four options to go hospital and person will choose only one option
based on the cost, convenience, and time.

 Template method
Defer the exact steps of an algorithm to a subclass

Example:

 Visitor
Defines a new operation to a class without change

Example: One biscuit company wants to promote their biscuit. So, the
company salesman visits the Marts and give a sample biscuit box to each mart.

You might also like