SOFTWARE ARCHITECTURE AND DESIGN PATTERNS
UNIT-IV: Structural Patterns
Adapter patterns
Bridge patterns
Composite patterns
Decorator patterns
Facade patterns
Flyweight patterns
Proxy patterns
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
Structure:
Participants:
Collaborations:
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.
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.
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
Structure
Participants:
Collaborations:
Abstraction class forwards client requirements to its implementer class
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; }
};
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
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:
Participants:
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
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
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
Structure:
Participants:
Collaborations:
Decorator class forwards its request to the abstract class component
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.
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.
Structure:
Participants:
Collaborations:
Clients communicates with sub classes by sending request to abstract class and forwards the request to corresponding subclass
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.
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.
Structure:
Participants:
Collaborations:
Client collaborates with abstract class to make the product design ,collaborates with two sub classes to distribute the objects
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.
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
Structure:
Participants:
Collaborations:
Proxy forwards its request to real subject subclass if it is appropriate
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