Professional Documents
Culture Documents
Behavioral Design Patterns: Example
Behavioral Design Patterns: Example
Chain of responsibility
A way of passing a request between a chain of objects
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
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:
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.