1 of 24

SOFTWARE ARCHITECTURE AND DESIGN PATTERNS

UNIT-IV: Structural Patterns

Adapter patterns

Bridge patterns

Composite patterns

Decorator patterns

Facade patterns

Flyweight patterns

Proxy patterns

2 of 24

Adapter patterns

Pattern name and classification: Adapter and structural

Intent: Convert the interface of a class into another interface clients expect.

Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Also Known As: Wrapper Patterns

Motivation: Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component.

Applicability:

Use the Adapter pattern when

  • Identify the interface that the client requires.
  • Design a "wrapper" class that can "impedance match" the adaptee to the client.
  • The adapter/wrapper class "has a" instance of the adaptee class.

3 of 24

Structure:

Participants:

  • Client
  • Interface Shape
  • Rectangle
  • Legacy Rectangle
  • Concrete factory

4 of 24

Collaborations:

  • Adapter makes things work after they're designed; Bridge makes them work before they are.
  • Adapter provides a different interface to its subject.
  • Adapter is meant to change the interface of an existing object.

Consequences:

By calling the request from Adapter class that Specific request will be displayed on the screen

Implementation:

Adapter patterns re implemented using class adapters in c++ to perform abstract operations.

5 of 24

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:

ET++ uses redrawing classes in a text editor to draw different shapes.

Related Patterns:

Bridge patterns and Decorator patterns are related to adapter patterns.

6 of 24

Bridge patterns

Pattern Name and classification: Bridge patterns, structural patterns

Intent:

Decouple an abstraction from its implementation so that the two can vary independently.

Also known as: Handle (or) Body patterns

Motivation:

The Bridge design pattern proposes refactoring this exponentially explosive inheritance hierarchy into two orthogonal hierarchies – one for platform-independent abstractions, and the other for platform-dependent implementations.

Bridge is designed up-front to let the abstraction and the implementation vary independently.

Applicability:

Use the Bridge pattern when

  • Changes in the implementation of abstract class should not effect the requirements of client.

7 of 24

Structure

Participants:

  • Client
  • Abstraction
  • Developer scheme
  • Developer1
  • Developer 2

Collaborations:

Abstraction class forwards client requirements to its implementer class

8 of 24

Consequences:

By hiding the internal details implementer class produces a product to client

Implementation:

Bridge patterns are implemented by only one implementer class which shares all the objectives of its subclass

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 24

Known uses:

Bridge patterns creates a platform for specific objects to provide the communication between existing classes

Related Patterns:

Adapter patterns are related to bridge Patterns

10 of 24

Composite patterns

Intent:

Composite objects into a tree structure hierarchies to represent a part whole formats

Motivation:

Graphic applications such as drawing editors, defines classes for graphical primitives

Applicability:

Composite patterns ae applied to differentiate between composite object from individual object

Structure:

11 of 24

Participants:

  • Client
  • Composite
  • Leaf
  • Composite object

Collaborations:

Client uses composite class interface o interact with objects

Consequences:

Composite abstract class defines subclasses to make the clients requirements simple

Implementation:

Composite patterns are implemented to share the objects and their methods in between the sub classes

12 of 24

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:

Composite patterns performs their operations in between the 2 register operations and its result is stored in third register

Related Patterns:

Decorator patterns, iterator patterns are related to composite patterns

13 of 24

Decorator Patterns

Intent:

Adding additional responsibilities to an object dynamically or at run time.

Also known as: Wrapper Patterns

Motivation:

Inheritance is the one way of adding the additional responsibilities to an object of class

Applicability:

Decorator patterns are applied

To add the responsibilities

To withdraw the responsibilities

14 of 24

Structure:

Participants:

  • Client
  • Component
  • Concrete object A
  • Decorator A
  • Concrete decorator A

Collaborations:

Decorator class forwards its request to the abstract class component

15 of 24

Consequences:

Component object and decorator objects are not identical

Decorator object will be formed by adding responsibilities to the component object

Implementation:

Decorator patterns are implemented to make the interface conformance and to decorate the objects individually.

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Decorator patterns are used for compressing the data by using different algorithms.

Related Patterns:

Adapter patterns, Composite patterns, Strategy patterns are related to decorator patterns.

16 of 24

Facade Patterns

Intent:

Provides a unified interface to a set of interfaces in a subsystem.

Motivation:

A segment of the client community needs a simplified interface to the overall functionality of a complex subsystem.

Applicability:

facade patterns are applied

To Wrap a complicated subsystem with a simpler interface.

17 of 24

Structure:

Participants:

  • Façade ( abstract class)
  • Subclass

Collaborations:

Clients communicates with sub classes by sending request to abstract class and forwards the request to corresponding subclass

18 of 24

Consequences:

In between the abstract class and subclasses defines a unified interface to forward the request

Implementation:

At runtime abstract class defines the systems like public and private.

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Subclass section defines its own objects to provide an intermediate results based on client requirements.

Related Patterns:

Mediator patterns are related to facade patterns.

19 of 24

Flyweight Patterns

Intent:

Flyweight patterns uses the sharing of fine grained objects in between the classess effectively.

Also Known as:

Complete patterns

Motivation:

Applications which are benefit from objects through out its design part can be expensive along with its design part.

Applicability:

facade patterns are applied

when a class requires large num of objects.

when objects are having their own identity.

20 of 24

Structure:

Participants:

  • Client
  • Flyweight
  • Concrete flyweight
  • Unshared objects

Collaborations:

Client collaborates with abstract class to make the product design ,collaborates with two sub classes to distribute the objects

21 of 24

Consequences:

Concrete flyweight subclass objects are considered as extrinsic

Unshared subclass objects are considered as intrinsic

Implementation:

Flyweight patterns are implemented in performing the operations with remote objects

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Patterns are majorly used in providing the design techniques of objects.

Related Patterns:

Flyweight patterns are related to composite and strategy patterns.

22 of 24

Proxy Patterns

Intent:

Provides a surrogate or place holder for another object to control access to it.

Also Known as:

Surrogate patterns

Motivation:

The document editor access embedded images through the interface defined by abstract graphic class

Applicability:

Proxy patterns are applied for remote proxy, virtual proxy and to protect the proxy

23 of 24

Structure:

Participants:

  • Client
  • Subject
  • Real subject
  • proxy

Collaborations:

Proxy forwards its request to real subject subclass if it is appropriate

24 of 24

Consequences:

Proxy patterns introduce a virtual memory location for accessing object, finally its result is related to real subject sub class

Implementation:

Proxy patterns are implemented to know the properties of subclass

Sample Code:

Class Clent

{

public: static MazeFactory* Instance();

protected: MazeFactory();

private: static MazeFactory* _instance;

};

MazeFactory::_instance = 0;

Known Uses:

Server creates proxy locations for remote objects based on the client requirements and distributes the objects among all the proxy locations.

Related Patterns:

Adapter patterns, decorator patterns are related to proxy patterns