.netC#.net

Design Patterns overview in C# (Part – 2)

We have already discussed on Design pattern overview and Creational design Patterns part 1 here. Please go through part 1 before proceeding for Part 2.

 

In this article we will understand structural Design Patterns. That Includes:

  • Adapter Design Pattern
  • Bridge Design Pattern
  • Composite Design Pattern
  • Decorator Design Pattern
  • Façade Design Pattern
  • Flyweight Design Pattern
  • Proxy Design Pattern

Adapter Design Pattern:

  • The adapter pattern is adapting between classes and objects
  • This pattern involves a single class called adapter which is responsible for communication between two independent or incompatible interfaces
  • This works like a bridge between two incompatible interfaces

UML Class Diagram and Implementation:

Adapter Design Pattern

 

ITarget
This is an interface which is used by the client to achieve its functionality/request.
Adapter
This is a class which implements the ITarget interface and inherits the Adaptee class. It is used for communication between Client and Adaptee.
Adaptee
Adaptee  class  have the functionality that is  required by the client. However, its interface is not compatible with the client.
Client
This is a class which interact with a type that implements the ITarget interface. The communication class adaptee, is not compatible with the client

 

Let’s see how to implement it.

When to use Adapter Design Pattern:

  • Adapter makes things work after they’re designed
  • Adapter provides a different interface to its subject
  • Adapter is meant to change the interface of an existing object

 

Bridge Design Pattern:

  • Bridge Pattern separates abstraction from its implementation, so that both can be modified Independently
  • Bridge Pattern behaves like a bridge between abstraction class and Implementer class.

UML Class Diagram and Implementation:

Bridge Design Pattern

 

 

The classes, interfaces and objects in the above UML class diagram are as follows:
Abstraction
This is an abstract class and containing members that define an abstract business object and its functionality. It contains a reference to an object of type Bridge. It can also acts as the base class for other abstractions.
Redefined Abstraction
This is a class which inherits from the Abstraction class. It extends the interface defined by Abstraction class.
Bridge
This is an interface which acts as a bridge between the abstraction class and implementer classes and also makes the functionality of implementer class independent from the abstraction class.
ImplementationA & ImplementationB
These are classes which implement the Bridge interface and also provide the implementation details for the associated Abstraction class.

 

Let’s see how to implement Bridge Design Pattern.

 

When to use Bridge Design Pattern:

  • Bridge Design Pattern is used when you want run-time binding of the implementation,
  • Bridge Design Pattern is used when you have a proliferation of classes resulting from a coupled interface and numerous implementations,
  • Bridge Design Pattern is used when you want to share an implementation among multiple objects,
  • Bridge Design Pattern is used when you need to map orthogonal class hierarchies.

Composite Design Pattern:

  • Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchies.
  • Composite pattern creates a class contains group of its own objects. This class provides ways to modify its group of same objects.
  • Composite pattern is used when we need to treat a group of objects and a single object in the same way

UML Class Diagram and Implementation:

 

Composite Design Pattern

 

The classes, interfaces and objects in the above UML class diagram are as follows:
Component
This is an abstract class containing members that will be implemented by all object in the hierarchy. It acts as the base class for all the objects within the hierarchy
Composite
This is a class which includes Add,Remove,Find and Get methods to do operations on Employee components.
Leaf
This is a class which is used to define leaf components within the tree structure means these cannot have TotalEmployee.

Let’s see how to implement this.

 

When to use Composite Design Pattern

  • Composite Design Pattern rely on recursive composition to organize an open-ended number of objects.
  • Composite Design Pattern can be traversed with Iterator.
  • Composite Design Pattern can let you compose a Mediator out of smaller pieces through recursive composition.
  • Flyweight Design Pattern is often combined with Composite Design Pattern to implement shared leaf
  • nodes.

Decorator Design Pattern:

 

  • Decorator pattern is used to add new functionality to an existing object without changing its structure.
  • Decorators provide a flexible alternative to subclass for extending functionality.
  • This pattern creates a decorator class which wraps the original class and add new behaviors/operations to an object at run-time.

UML Class Diagram and Implementation:

Decorator Design Pattern

The classes, interfaces and objects in the above UML class diagram are as follows:
Component
This is an interface containing members that will be implemented by ConcreteClass and Decorator.
ConcreteComponent
This is a class which implements the Component interface.
Decorator
This is an abstract class which implements the Component interface and contains the reference to a Component instance. This class also acts as base class for all decorators for components.
ConcreteDecorator
This is a class which inherits from Decorator class and provides a decorator for components.

 

Let’s see how to implement it.

 

When to use Decorator Design Pattern:

  • Decorator Design Pattern provides an enhanced interface.
  • Decorator Design Pattern enhances an object’s responsibilities so it is more transparent to client
  • Decorator Design Pattern rely on recursive composition to organize an open-ended number of objects.
  • Decorator Design Pattern  can be viewed as a degenerate Composite with only one component
  • Decorator Design Pattern provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward request

 

Facade Design Pattern:

 

  • Facade Design Pattern makes a software library easier to use, understand and test
  • Facade Design Pattern make the library more readable
  • Facade Design Pattern reduce dependencies of outside code on the inner workings of a library
  • Facade Design Pattern wrap a poorly designed collection of APIs with a single well-designed API.

UML Class Diagram and Implementation:

 

Facade Design Pattern

 

The classes, interfaces and objects in the above UML class diagram are as follows:
Complex System
A library of Testsystems.

TestsystemA, TestsystemB, TestsystemC
These are classes within complex system and offer detailed operations.

Façade
This is a wrapper class  which contains a set of members which are required by client.

Client
This is a class which calls the high-level operations in the Façade.

Let’s see how to implement it.

When to use Facade Design Pattern:

  • Facade defines a new interface
  • Facade shows how to make a single object represent an entire subsystem.
  • Facade abstracts functionality of existing classes
  • Abstract Factory can be used as an alternative to Facade to hide platform-specific classes
  • Facade objects are often Singletons because only one Facade object is required
  • Adapter and Facade are both wrappers; but they are different kinds of wrappers

Flyweight Design Pattern:

 

  • Flyweight design pattern is an object that minimizes memory use by sharing as much data as possible with other similar objects
  • Flyweight pattern is used to reduce the number of objects created, to decrease memory and resource usage. As a result it increase performance
  • Flyweight design pattern provides  a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory.
  • The flyweight pattern uses the concepts of intrinsic and extrinsic data.Intrinsic data is held in the properties of the shared flyweight objects. This information is stateless and generally remains unchanged, if any change occurs it would be reflected among all of the objects that reference the flyweight.Extrinsic data is computed on the fly means at runtime and it is held outside of a flyweight object. Hence it can be stateful.

UML Class Diagram for Flyweight Design Pattern:

 

Flyweight Design Pattern

 

 

The classes, interfaces and objects in the above UML class diagram are as follows:
Flyweight
This is an interface which defines the members of the flyweight objects.
ConcreteFlyweight
This is a class which Inherits from the Flyweight class.
UnsharedFlyweight
This is a class which Inherits from the Flyweight class and enables sharing of information, it is possible to create instances of concrete flyweight classes that are not shared.
FlyweightFactory
This is a class which holds the references of already created flyweight objects. When the GetFlyweight method is called from client code, these references are checked to determine if an appropriate flyweight object is already present or not. If present, it is returned. Otherwise a new object is generated, added to the collection and returned.

 

 

Let’s see how to Implement it.

When to use Flyweight Design Pattern:

  • Flyweight shows how to make lots of little objects represent an entire subsystem.
  • Flyweight is often combined with Composite to implement shared leaf nodes.
  • Terminal symbols within Interpreter’s abstract syntax tree can be shared with Flyweight.
  • Flyweight explains when and how State objects can be shared.

Proxy Design Pattern:

  • Proxy Design pattern involves a class, called proxy class, which represents functionality of another class.
  • Proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes.

UML Class Diagram for Proxy Design Pattern:

Proxy Design Pattern

 

The classes, interfaces and objects in the above UML class diagram are as follows:
Subject
This is an interface having members that will be implemented by RealSubject and Proxy class.
RealSubject
This is a class which we want to use more efficiently by using proxy class.
Proxy
This is a class which holds the instance of RealSubject class and can access RealSubject class members as required.

When to use Proxy Design Pattern:

  • Proxy Design Patterns provides the same interface to its subject
  • Proxy Design Pattern describes how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.

 

We will discuss on Behavioural Design Patterns in next article.

© 2015, www.techkatak.com. All rights reserved.