Exploring Structural Design Patterns

With Real World Examples | Part 1 | Day 5 of #100DaysOfCode

Dhananjay Trivedi
4 min readSep 22, 2019

As the name itself suggests, Structural Design Patterns are developed after years of experience and understanding various pains after arranging your classes in various ways.

As your project becomes complex, the number of classes and objects will grow. Structural design patterns help your structure your classes and entities in a way that it simplifies your understanding of the system and you can also easily identify the relationship between those classes. This will help you remove the redundancies, increase the level of abstraction and encapsulation amongst classes.

Hence, you will be maintaining your project as it grows so that it is efficient and flexible.

There are 7 Structural Design Patterns

We will see three of them today, a brief. Rest I will catch up in the upcoming articles. I will update once I am done.

1. Adapter Pattern

Real-world Example, We all use various adapters in our daily life, above is a Type-C to USB adapter which people use to use their Pendrive with their 13' 2017 Macbook Pro which sadly has no USB port. If you have to use a USB Device with a TypeC port device, you don’t replace either of these devices, you just get an adapter to get the job done.

Similarly, in the programming world, we use Adapter Classes. We also call them Wrapper Classes. Their sole job is to act as a mediator between two classes which doesn’t have the idea of implementation of the other one. Also, they shouldn’t be following the Single Responsibility Principle!

Technical Example, If there are two modules, one module is returning you with XML Data, while the other one consumes JSON to give you your final output. You can’t change these perfectly working modules as they are being used elsewhere as well. Hence, you write wrapper classes to convert your XML to JSON. You can even write a two-way wrapper class if that is required.

2. Bridge Pattern

Technical Example, Imagine you have a Shape class, and you are currently required to build two objects Square and a Circle. You create two classes extending the parent shape class. Easy!

Now you are asked to add two Colors, and hence a Red-Square, Red-Circle, Blue-Square, Blue-Circle. Ooh, see how quickly that grew. What if you are now asked to add Triangle as well, and maybe a color as well. You can imagine how quickly the number of classes will grow as we increase some requirements.

We can avoid that by subgrouping two similar requirements here. Shape and Color. No matter how many shapes or color comes, they will be extending only their logical parent class and will be quite easy to maintain.

You can establish a communication between these logical parents through argument constructor where you pass the right color object into the constructor for the right shape. Here the constructor is acting as the bridge.

Real-world example would be of all the remotes and devices. You have various devices and various remotes. Each remote is for a particular device. When you get a particular device to let’s say AC. You don’t have to do change anything with the TV remote. Each device adds a change of a single remote, nothing else.

3. Composite Pattern

You arrange your objects into a tree structure and then work with these as if they were independent objects by defining some helpful logic so the parent doesn’t have to depend upon its child for certain operations.

Real world example — Imagine these dolls above, Now imagine they are made up of different materials like Gold, Silver, Aluminium, Copper, Wood, etc. If they all were packaged into one single outer doll which was made up of Wood. You won’t be able to tell the price of the whole thing without opening each and every doll and check its price.

Similarly, in your coding project, there will be long nested classes and to determine some value of the bottom-most object you will have to iterate over the whole structure which would make it very inefficient. Hence you define a system in the parent class to determine that value of its child classes.

Technical Example, If you had to write a program to give you a price of such dolls. If each doll is following a common interface that could get you the price of the required doll and even its child dolls recursively without you writing a for loop to do so and instantiating millions of object all over the place.

We will catch up again soon to learn more about Structural Design patterns.

That’s all for Day 5! #100DaysOfCode! Thanks for reading, Happy learning!

--

--

Dhananjay Trivedi
Dhananjay Trivedi

Written by Dhananjay Trivedi

Android | Flutter | App Developer | Product Management | Prompt Engineer who loves writing and sharing knowledge

No responses yet