GOF Design Patterns - Part 3 - Structural Design Patterns
Sushant Kumar Tarway
Experienced Product Manager | Agile & SAFe 6.0 Practitioner | Microservices, APIs, Containerization, Web Development| Product Strategy and User-Centric Design, API - first and Cloud Native focussed on TDD/BDD
Structural Design Patterns
In starting post of this GOF Design Pattern Series, I described what a Design Pattern is and why do we need Design patterns and what are different types of Design Patterns. In subsequent post I wrote about Creational Design Patterns
In this post, I will deep dive a step further and will discuss briefly on different types of Structural Design Patterns.
Structural patterns provide different ways to create a class structure, for example using inheritance and composition to create a large object from small objects.
Adapter Pattern
Adapter design pattern is one of the structural design patterns and it is used so that two unrelated interfaces can work together. The object that joins these unrelated interfaces is called an Adapter. As a real life example, we can think of a mobile charger as an adapter because mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.
Composite Pattern
Composite pattern is one of the Structural design patterns and is used when we have to represent a part-whole hierarchy. When we need to create a structure in a way that the objects in the structure have to be treated the same way, we can apply composite design pattern. You can take real life example of a organization. It have general managers and under general managers, there can be managers and under managers there can be developers. Now you can set a tree structure and ask each node to perform common operation like getSalary().
Proxy Pattern
Proxy pattern intent is to “Provide a surrogate or placeholder for another object to control access to it”. The definition itself is very clear and proxy pattern is used when we want to provide controlled access of functionality. In the real work a cheque or credit card is a proxy for what is in our bank account. It can be used in place of cash, which is what is needed, and provides a means of accessing that cash when required. And that's exactly what the Proxy pattern does - controls and manage access to the object they are "protecting".
Flyweight Pattern
Flyweight is used when there is a need to create high number of objects of almost similar nature. High number of objects consumes high memory and flyweight design pattern gives a solution to reduce the load on memory by sharing objects. It is achieved by segregating object properties into two types intrinsic and extrinsic. For example assume we are working with an application that maps stars from universe. In this application if we are going to create an object for every star then think of it how much memory we will need.
Facade Pattern
Facade Pattern is used to help client applications to easily interact with the system. Facade exposes a simplified interface (in this case a single interface to perform that multi-step process) and internally it interacts with those components and gets the job done for you. It can be taken as one level of abstraction over an existing layer. Let’s take a car; starting a car involves multiple steps. Imagine how it would be if you had to adjust n number of valves and controllers. The facade you have got is just a key hole. On turn of a key it send instruction to multiple subsystems and executes a sequence of operation and completes the objective. All you know is a key turn which acts as a facade and simplifies your job.
Bridge Pattern
When we have interface hierarchies in both interfaces as well as implementations, then builder design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client programs. The display of different image formats on different operating systems is a good example of the Bridge pattern. You might have different image abstractions for both jpeg and png images. The image structure is the same across all operating systems, but the how it's viewed (the implementation) is different on each OS. This is the type of decoupling that the Bridge pattern allows.
Decorator Pattern
Decorator design pattern is used to modify the functionality of an object at runtime. At the same time other instances of the same class will not be affected by this, so individual object gets the modified behavior. To extend or modify the behavior of ‘an instance’ at runtime decorator design pattern is used. Inheritance is used to extend the abilities of ‘a class’. Unlike inheritance, you can choose any single object of a class and modify its behavior leaving the other instances unmodified. In implementing the decorator pattern you construct a wrapper around an object by extending its behavior. The wrapper will do its job before or after and delegate the call to the wrapped instance.