1 of 30

SOFTWARE ARCHITECTURE AND DESIGN PATTERNS

UNIT-V: Behavioral Patterns

Command patterns

Interpreter patterns

Iterator patterns

Mediator patterns

Memento patterns

Observer patterns

State patterns

Strategy Patterns

Visitor Patterns

2 of 30

Command patterns

Pattern name and classification: Command and Behavioral

Intent: It encapsulates a request as an object which parameterizes clients with different requests.

Also Known As: Action/Transaction Patterns

Motivation: Command patterns objects makes request of unspecified application objects turns into a complete object

Applicability:

Command patterns are applied to

parameterized object

specifying the queue and executing the request

support undo and redo operations

3 of 30

Structure:

Participants:

  • Client
  • Invoker
  • Command
  • Concrete command
  • receiver

4 of 30

Collaborations:

Client collaborates with command abstract class to access the commands and to execute it

Consequences:

Command patterns decouples the object to invoke the operation and to add the new operation

Implementation:

Command patterns are implemented to specify the object operations and its responsibilities

5 of 30

Sample Code:

Class house {

public:

house();

virtual Maze* Makehouse() const

{ return new house; }

virtual Wall* MakeWall() const

{ return new Wall; }

virtual Room* MakeRoom(int n) const

{ return new Room(n); }

virtual Door* MakeDoor(Room* r1, Room* r2) const

{

return new Door(r1, r2); }}

Known Uses:

Command patterns describes how to implement functions.

Related Patterns:

Composite and memento patterns are related to command patterns.

6 of 30

Interpreter patterns

Pattern Name and classification: interpreter patterns, Behavioral patterns

Intent:

Interpreter patterns defines a representation for its grammar consist of set of production rules

Also known as: execution patterns

Motivation:

Interpreter patterns describes how to represent simple languages which consist of sentences as production rules

Applicability:

Interpreter patterns are applied when

The grammar is simple

Efficiency should not be critical

7 of 30

Structure

Participants:

  • Client
  • Abstract expression
  • Terminals
  • Non Terminals

Collaborations:

Client builds sentences to form an abstract syntax tree consist of terminals and non terminals

8 of 30

Consequences:

To change the grammar by adding the new sentences can be done by interpreting the expressions

Implementation:

Interpreter patterns are implemented to

design an abstract syntax tree

defining the set of operations

Sample Code:

Class client {

public:

virtual void Build Maze() { }

virtual void Build Room(int room) { }

virtual void Build Door(int roomFrom, int roomTo) { }

virtual Maze* GetMaze() { return 0; }

};

9 of 30

Known uses:

Interpreter patterns are widely used in compilers

Related Patterns:

Composite and iterator patterns are related to interpreter patterns

10 of 30

Iterator patterns

Intent:

Iterator patterns provides a way to access the elements of aggregate objects sequentially.

Also known as : Cursor patterns

Motivation:

Aggregate objects consist list of elements and their operations to perform can be accessed by other subclasses sequentially

Applicability:

Iterator patterns are applied to

Access elements sequentially

supporting multiple traversal paths

11 of 30

Structure:

Participants:

  • Aggregate client Iterator concrete iterator concrete aggregate

Collaborations:

Subclasses collaborates with abstract classes to make the current object to be ready for its execution

Consequences:

Abstract class objects can be shared by all the subclasses through different traversal paths.

Implementation:

Iterator patterns are implemented to 1. to control the iteration

2. Designing the traversal algorithm

3. specifying the iterator operations

12 of 30

Sample Code:

Class client {

public:

Void Maze (Maze*, Wall*, Room*, Door*);

virtual Maze* MakeMaze() const;

virtual Room* MakeRoom(int) const;

virtual Wall* MakeWall() const;

virtual Door* MakeDoor(Room*, Room*) const;

};

Known Uses:

Iterator patterns are mainly used for iterating the specific objects operations any number of times.

Related Patterns:

Composite, memento patterns are related to iterator patterns

13 of 30

Mediator Patterns

Pattern name and classification: mediator and Behavioral

Intent:

defines an object that encapsulates how a set of objects interact with each other

Also known as: Interaction Patterns

Motivation:

Object oriented design encourages behavior among objects by providing different interfaces

Applicability:

Mediator patterns are applied to reuse the objects when the classes requires

14 of 30

Structure:

Participants:

  • Mediator colleague id concrete mediator concrete colleague1 concrete colleague2

Collaborations:

Colleague abstract class is the owner for set of objects

Colleague class sends and receive request from mediator class to interact one object with other

15 of 30

Consequences:

For interaction of objects different protocols are defined by abstract classes mediator and colleagues

Implementation:

Mediator patterns are implemented for providing the communication between the objects of different classes

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Mediator patterns are widely used in MVC architecture of small Talk languages

Related Patterns:

Façade patterns are related to mediator patterns.

16 of 30

Memento Patterns

Pattern name and classification: Memento and Behavioral

Intent:

Without violating encapsulation capture the object state and restoring the state of object

Also known as: Token patterns

Motivation:

Memento is an object request which stores the photo copy of internal state of another object

Applicability:

Memento patterns are applied to

saving the photocopy of objects internal states

creating the discrete interface to obtain the state of object

17 of 30

Structure:

Participants:

  • Originator memento caretaker

Collaborations:

Caretaker class issues memento from originator and holds it for a time and executes the request to get back the relation.

18 of 30

Consequences:

Preserving the encapsulation boundaries

Simplifies the originator

Implementation:

Memento patterns are implemented to store the changes of object characteristics if any.

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Memento patterns are used to set the object properties and to add the object properties.

Related Patterns:

Command, Iterator patterns are related to memento patterns.

19 of 30

Observer Patterns

Pattern name and classification: Observer and Behavioral

Intent:

Defines one to many dependency between objects so that when one object changes its state all its dependents updated dynamically

Also Known as:

Dependent patterns

Motivation:

Partitioning a system into collection of cooperative classes to maintain the consistency between related objects.

Applicability:

Observer patterns are applied

when a change to one object requires changing other objects

when an object should be able to notify other objects

20 of 30

Structure:

Participants:

  • Subject Observer concrete observer concrete subject

Collaborations:

Concrete subject notifies its observers whenever a change occurs in its state.

21 of 30

Consequences:

Observer patterns supports broadcast communication.

Implementation:

Observer patterns are implemented to map the related states to the existing objects.

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Observer patterns are used to construct user interfaces.

Related Patterns:

Mediator patterns are related to Observer patterns.

22 of 30

State Patterns

Pattern name and classification: State and Behavioral

Intent:

State patterns allows an object to alter its behavior when its internal state changes.

Motivation:

Whenever the existing connection changes its state, object behavior changes for its users.

Applicability:

State patterns are applied to change the state of objects at runtime depends on users requirements.

23 of 30

Structure:

Participants:

  • Context State Concrete state A Concrete state B

Collaborations:

Context is the primary interface for clients , collaborates with state class to change object state and behavior.

24 of 30

Consequences:

The transitions between context and state abstract classes are explicit

Implementation:

State patterns are implemented for dynamic inheritance of objects.

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

State patterns are used to define an abstract tool from which sub classes implements the behavior of objects.

Related Patterns:

Flyweight and singleton patterns are related to state patterns

25 of 30

Strategy Patterns

Pattern name and classification: Strategy and Behavioral

Intent:

Defines a family of algorithm, encapsulates each one and make them interchangeable if requires.

Also known as :policy patterns

Motivation:

Strategy means using the appropriate algorithm at right time.

Different algorithms are used for different purposes (one algorithm for class, one algorithm for method)

Applicability:

Strategy patterns are applied to define the behavior of subclasses.

26 of 30

Structure:

Participants:

  • Context Strategy Concrete strategy A Concrete strategy B

Collaborations:

Client and abstract classes interacts with each other to choose and implement appropriate algorithms for different operations

27 of 30

Consequences:

Strategy patterns consist of different algorithms , these are alternative to all the subclasses.

Implementation:

Strategy patterns are implemented for defining the abstract class interfaces.

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Strategy patterns are used in computer graphic applications.

Related Patterns:

Flyweight patterns are related to strategy patterns

28 of 30

Visitor Patterns

Pattern name and classification: Visitor and Behavioral

Intent:

Patterns represents an operation to be performed on the elements of objects structure.

Also known as :Observable patterns

Motivation:

Most of the operations of elements are defined at the time of creating the classes

Classes consist of different variables and set of arithmetic expressions.

Applicability:

Visitor patterns are applied when the object structure consists of many classes and each class performing different operations.

29 of 30

Structure:

Participants:

  • Client visitor Concrete visitor A concrete visitor B
  • object structure

Collaborations:

Clients communicates with visitors abstract class to visit what are the elements of class and functions performed by elements

30 of 30

Consequences:

Visitor patterns visits the elements of classes randomly on its own , each pattern has to visit all the elements of class

Implementation:

Every abstract class is having its own visitor class to visit any number of elements of class

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Visitor patterns are used in smalltalk language at first as program node enumerator

Related Patterns:

Composite, interpreter patterns are related to visitor patterns