Structural Design Pattern

Learn via video courses
Topics Covered

Abstract

This article discusses various structural patterns which use composition for merging both classes and objects into the bigger structure, Which helps us to solve many real-life problems that a developer is most likely to encounter.

What is Structural Design Pattern?

Structural design pattern is a blueprint of how different objects and classes are combined together to form a bigger structure for achieving multiple goals altogether. The patterns in structural designs show how unique pieces of a system can be combined together in an extensible and flexible manner. So, with the help structural design pattern we can target and change a specific parts of the structure without changing the entire structure.

Types of structural design patterns

When it comes to types of design patterns we have several design patterns, the ones that developers use more often are:

  • Adapter Pattern
  • Bridge Pattern
  • Composite Pattern
  • Decorator Pattern
  • Facade Pattern
  • Flyweight Pattern
  • Proxy Pattern

starting with,

Adapter Pattern

The adapter pattern helps in converting the interface of a class into another interface depending on the client's requirements. So, basically providing what is required by the client by using the service of a class with a different interface. Adapter pattern is also famously known as Wrapper.

Now, let us discuss When to use Adapter Pattern:

  • When you want to create a reusable class that can cooperate easily with other classes, which does not have any compatible interface.
  • When you want to use an object in a certain environment that is expecting an interface that is different from its object interface.
  • When you want to use an existing class, but its interface does not match the interface the class needs.

so, basically, the adapter pattern acts as an intermediate state between two classes.

Now, next, we have

Bridge Pattern

The main function of the bridge pattern is to separate the implementation part from the abstraction part so that the abstraction and implementation of a class can vary independently.

There are in two part in Bridge Pattern:

  • Implementation or Body
  • Abstraction or Handling

Now, let us discuss

When to use Bridge Pattern

  • When both implementation and its functional abstraction needs are extended using another sub-class.
  • When you don't want to affect clients due to changes in implementation.
  • When you want to avoid permanent binding between implementation and its functional abstraction.

Next, we have

Composite Pattern

In a composite pattern, the client is able to operate objects which may or may not represent its hierarchy. In simple terms composite pattern allows you to create hierarchical tree of unique complexities which allows treating every object individually.

Now, let us discuss

When to use Composite Pattern

  • when you want to add new kinds of components to specific objects
  • It provides manageable interfaces and flexibility in its structure.
  • It helps to define hierarchies containing complex and primitive objects.

Next, we have

Decorator Pattern

In Decorator Pattern, we can add or remove object functionality without changing the function or external appearance of the object. Therefore with help of a decorator pattern, we can add additional responsibilities to an object without changing the functionalities of the object.

Now, let us discuss

When to use Decorator Pattern

  • It enhances the extensibility of an object, as changes are made by coding new classes.
  • Provides greater flexibility as compared to static inheritance.
  • Rather than coding all behaviour into a single object Each specific part of the functionality is simplified by coding a series of classes.

Next, we have

Facade Pattern

In Facade Pattern, we provide a simplified interface to a set of interfaces of a subsystem for hiding its complexity of subsystem from the client. In simple words with the help of facade pattern, we make sub-systems easier to use by describing a higher-level interface.

Now, let us discuss

When to use Facade Pattern

  • When there are many dependencies between implementation classes of abstraction and client requirements.
  • When you want to have layers in your subsystems.
  • When you want to provide a simple interface instead of a complex subsystem.

Next, we have

Flyweight Pattern

Flyweight Patterns helps us to reuse similar kinds of existing objects by storing and creating new objects when similar objects are not found. Therefore in simple terms, it helps us to reduce the expense of multiple instances that contains the same data.

Now, let us discuss

When to use Flyweight Pattern

  • To reduce storage cost, by reusing similar objects.
  • When a large number of similar objects are required.
  • When the application is dependent on unique object identity.

Next, we have

Proxy Pattern

In Proxy Pattern we provide a placeholder or a surrogate object to control access to the original object. In simple terms, providing a proxy or dummy layer of information before the client before accessing the original data. There are in total four types of proxy

  1. Remote proxy
  2. Virtual proxy (most common)
  3. Protective Proxy
  4. Smart Proxy

Now, let us discuss

When to use which Proxy Pattern

  • Remote proxy can be used to make interface resources remotely available like a web service.
  • Virtual Proxy can be used when there are multiple database calls being done for extracting a huge file, Since it is an expensive process we can use a virtual proxy to reduce cost.
  • Protective Proxy can be used when there is security or privacy of data is required. With the help of a proxy pattern, we can verify whether the user has access to the data or not.
  • Smart Proxy can be used when we require an extra layer of security for a specific object.

So, till now we have only discussed when to use structural patterns for solving problems, but now you might be thinking what is the need for structural patterns in the real world.

What Are the Problems Solved by Structural Design Patterns?

  • The structure of the application becomes simplier and clean.
  • The efficiency of the application increases.
  • Reusability of code increases.

Summary

I hope you got a high-level idea of what a structural design pattern is, and what are its type.

Let's have a recap of what we have learned up till now,

  • What is structural pattern?
  • How each pattern differs from the other and when it is suitable to use specific patterns.
  • Finally, we have discussed the problems that are solved by structural design patterns and why it is efficient in building application.

Traverse the Instagram System Design landscape with our course, arming you with the skills to tackle any design project.