Structural design patterns

Structural design patterns are software patterns that help in solving standard problems in creating static structure of classes through inheritance and composition.

Do check the design patterns catalogue page.
Do not miss the creational patterns and behavioral patterns pages too.

Adapter pattern
Also known as wrapper pattern. Purpose is to make the existing classes with incompatble interfaces work together. It does it by wrapping the existing class in a new class exposing interfaces that match the expectation. A simple non-software example of this would be a power adapter that makes your device work with a power socket of foriegn standard. It’s often used in cases where the existing class definition cannot be changed. The wrapper (adapter) does its intent by routing the calls or transforming the data (input parameters or output).

2 variants in this –

  • Object adapter – The wrapper contains the instance of the class it wraps. Do check the C# example of object adpater. stack is the adatee and the program is the adapter.
  • Class adapter – Uses inheritance to wrap the adpatee.
Facade pattern
Facade pattern hides the implementation details of a sub-system and exposes easy to access interfaces to the client. It’s often used in libraries to provide well-cut APIs and to avoid the direct dependencies to the inner implementations.

3 variants –

  • Static facade – Facade is implemented as a set of static methods.
  • Opaque facade – Only facade is visible outside the sub-system. Often the facade is implemented as singleton in this case.
  • Transparent facade – The facade and the internal implementation interfaces in the sub-system are exposed outside.

Facade pattern Vs adapter pattern – Facade often aggregates the internal APIs and define an abstracted set of APIs whereas the adapter just wraps the existing APIs.

Do read C# implementation of facade pattern.

Decorator pattern
Decorator adds additional behavior to an object at run time. It’s an alternate mechanism to inheritance for extending the functionality of an existing class.

Simple scenario of taking order in restaurant is presented in this sample implementation of decorator pattern in C#. Basic menu is bread. With every topping such as chicken, the price of the order goes up. Toppings are introduced as decorators.

Bridge pattern
Bridge pattern lets us connect two hierarchy of classes. This could be applied to situations where two concepts come together in achieving a task but they grow independently. It creates an othgonal heirarchy instead of typical parallel hierarchy.

A sample scenario of thread scheduling in operating systems have been explained in this C# implementation. Two concepts – Thread scheduling mechanism and platforms used here. They could grow independently. Adding a new platform shall not impact the scheduling algoithm and vice versa. This is made possible via bridge pattern creating an orthogonal hierarchy among them.

Composite pattern
If there is a group of related entities forming a tree kind of relationship, then composite pattern could be used to model them. An interface is defined attribituting common characteristics. Every entity is modeled as a class implementing the said interface. A tree representation among them could be formed connecting these objects of these entities.

Sample C# implementation on composite pattern.

Flyweight pattern
Flyweight pattern is a mechanism to save memory in case of large number of granular objects which share certain common criteria. It does it by reusing objects. Take a common example of word processor application having large quantity of formatted text. Instead of attaching style information to individual characters in the text, style is separated into flyweight object and attached to characters, thus saving memory. Implementation in C# for this scneario can be found here.
Proxy pattern
Intent of proxy pattern is to have a lightweight version of the real object closer to the client in an easily consumable manner. Both the proxy and the real object implements the same interface type. Client interacts with the proxy and not the real object. Communication with real object is wired into the proxy. Proxy could apply certain optimization and abstract complexities. A good example in computing is creating proxy classes for the remote web methods. Proxy in this case abstract the complexity of communicating with remote web methods.

Sample C# implementation of proxy pattern.

Marker Interface pattern
Marker interface concept is available in few programming languages. It is usually an empty interface. It signifies the need for special treatment by the consuming classes. A class that implements the marker interface conveys the consuming class that it has to be treated in special way. Annotations are an alternative to marker interface. In Java, Serializable – signifies that it can be flattened and written to disk/network socket, Cloenable – signifies that the implementation can be legally cloned.

Leave a Reply

Your email address will not be published. Required fields are marked *