0% found this document useful (0 votes)
2 views

Module - 3 SA& DP (1)

The document discusses behavioral design patterns that focus on the interaction and responsibility of objects, emphasizing loose coupling to avoid dependencies. It outlines ten specific patterns: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, and Template, each with its advantages and usage scenarios. The document provides a brief description of each pattern, highlighting their benefits and practical applications.

Uploaded by

bhargavahero625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module - 3 SA& DP (1)

The document discusses behavioral design patterns that focus on the interaction and responsibility of objects, emphasizing loose coupling to avoid dependencies. It outlines ten specific patterns: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, and Template, each with its advantages and usage scenarios. The document provides a brief description of each pattern, highlighting their benefits and practical applications.

Uploaded by

bhargavahero625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Module – 3

Behavioural Patterns

Behavioural patterns are concerned with the interaction and


responsibility of objects. In these design patterns, the interaction
between the objects should be in such a way that they can easily
talk to each other and still should be freely coupled. That means
the implementation and the client should be loosely coupled in order
to avoid hard coding and dependencies.

There are 10 types of behavioural patterns

1. Chain of Responsibility Pattern

2. Command Pattern

3. Interpreter Pattern

4. Iterator Pattern

5. Mediator Pattern

6. Memento Pattern

7. Observer Pattern

8. State Pattern

9. Strategy Pattern

10.Template Method
1.Chain of Responsibility Pattern

 In chain of responsibility, sender sends a request to a chain of


objects. The request can be handled by any object in the chain.
 A Chain of Responsibility Pattern says that just "avoid
coupling the sender of a request to its receiver by giving
multiple objects a chance to handle the request". For
example, an ATM uses the Chain of Responsibility design
pattern in money giving process.
 In other words, we can say that normally each receiver contains
reference of another receiver. If one object cannot handle the
request then it passes the same to the next receiver and so on.

Advantage of Chain of Responsibility Pattern

 It reduces the coupling.


 It adds flexibility while assigning the responsibilities to objects.
 It allows a set of classes to act as one; events produced in one
class can be sent to other handler classes with the help of
composition.

Usage of Chain of Responsibility Pattern:

o When more than one object can handle a request and the handler
is unknown.
o When the group of objects that can handle the request must be
specified in dynamic way.

Example of Chain of Responsibility Pattern

Let's understand the example of Chain of Responsibility Pattern by


the above UML diagram.

UML for Chain of Responsibility Pattern:

2. Command Pattern

A Command Pattern says that “encapsulates a request under an


object as a command and passes it to invoker object. Invoker object
looks for the appropriate object which can handle this command and
pass the command to the corresponding object and that object
executes the command".
Advantage of command pattern

o It separates the object that invokes the operation from the object
that actually performs the operation.
o It makes easy to add new commands, because existing classes
remain unchanged.

Usage of command pattern:

o When you need parameterize objects according to an action


perform.
o When you need to create and execute requests at different times.
o When you need to support rollback, logging or transaction
functionality.

Example of command pattern


Let's understand the example of adapter design pattern by the above
UML diagram.

UML for command pattern:

These are the following participants of the Command Design


pattern:

 Command This is an interface for executing an operation.


 Concrete Command This class extends the Command interface
and implements the execute method. This class creates a binding
between the action and the receiver.
 Client This class creates the Concrete
 Command class and associates it with the receiver.
 Invoker This class asks the command to carry out the request.
 Receiver This class knows to perform the operation.

Example:

3. Interpreter Pattern

An Interpreter Pattern says that "to define a representation of


syntax of a given language, along with an interpreter that uses
this representation to interpret sentences in the language".
Basically the Interpreter pattern has limited area where it can be
applied. We can discuss the Interpreter pattern only in terms of formal
syntax but in this area there are better solutions that are why it is not
frequently used.

Advantage of Interpreter Pattern

 It is easier to change and extend the syntax.


 Implementing the syntax is straight forward.

Usage of Interpreter pattern:

o When the syntax of the language is not complicated.


o When the efficiency is not a priority.

Example:

.
4. Iterator Pattern

Iterator Pattern is used "to access the elements of an


aggregate object sequentially without exposing its underlying
implementation".

Advantage of Iterator Pattern

 It supports variations in the traversal of a collection.


 It simplifies the interface to the collection.

Usage of Iterator Pattern:

o When you want to access a collection of objects without exposing its


internal representation.
o When there are multiple traversals of objects need to be supported in the
collection.

Example of Iterator Pattern


Let's understand the example of iterator pattern pattern by the above UML
diagram.
5. Mediator Pattern

A Mediator Pattern says that "to define an object that


encapsulates how a set of objects interact".

I will explain the Mediator pattern by considering a problem.


When we begin with development, we have a few classes and these
classes interact with each other producing results. Now, consider
slowly, the logic becomes more complex when functionality
increases. Then what happens? We add more classes and they still
interact with each other but it gets really difficult to maintain this
code now. So, Mediator pattern takes care of this problem.

Advantage:

o It decouples the number of classes.


o It simplifies object protocols.
o It centralizes the control.
o The individual components become simpler and much easier to
deal with because they don't need to pass messages to one
another.

Usage:

o It is commonly used in message-based systems likewise chat


applications.
o When the set of objects communicate in complex but in well-
defined way.
6. Memento Pattern

A Memento Pattern says that "to restore the state of an object to


its previous state". But it must do this without violating
Encapsulation. Such case is useful in case of error or failure.

Memento design pattern is used to implement the undo


operation. This is done by saving the current state of the object as it
changes state.

Advantages:

o It preserves encapsulation boundaries.


o It simplifies the originator.
Usage:

o It is used in Undo and Redo operations in most software.


o It is also used in database transactions.

Example:

7. Observer Pattern

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".

Advantages:

o It describes the coupling between the objects and the observer.


o It provides the support for broadcast-type communication.
Usage:

o When the change of a state in one object must be reflected in


another object without keeping the objects tight coupled.
o When the framework we writes and needs to be enhanced in
future with new observers with minimal changes.

UML for Observer Pattern:

8.State Pattern

A State Pattern says that "the class behavior changes based on


its state". In State Pattern, we create objects which represent various
states and a context object whose behavior varies as its state object
changes.
Advantages:

o It keeps the state-specific behavior.


o It makes any state transitions explicit.

Usage:

o When the behavior of object depends on its state and it must be


able to change its behavior at runtime according to the new
state.
o It is used when the operations have large, multipart conditional
statements that depend on the state of an object.

9.Strategy Pattern

A Strategy Pattern says that "defines a family of functionality,


encapsulate each one, and make them interchangeable".
Benefits:

o It provides a substitute to subclassing.


o It defines each behavior within its own class, eliminating the
need for conditional statements.
o It makes it easier to extend and incorporate new behavior
without changing the application.

Usage:

o When the multiple classes differ only in their behaviors.e.g.


Servlet API.
o It is used when you need different variations of an algorithm.
.
10.Template Pattern

A Template Pattern says that "just define the skeleton of a


function in an operation, deferring some steps to its subclasses".

Advantages:

o It is very common technique for reusing the code.This is only


the main benefit of it.

Usage:

o It is used when the common behavior among sub-classes should


be moved to a single common class by avoiding the duplication

You might also like