Design Patterns for software design and development
What are design patterns?
A general repeatable solution to a commonly occurring problem statement in software or product design. They are ways of expressing reusable architecture that are industry standard for software design and development.
Top 5 commonly used design patterns:
1. Singleton pattern:
It refers to a way of creating a single object that is shared among a bunch of different resources throughout the application without having to recreate that object.
Pros: One reusable source for multiple modules, advantageous when a small set of information is to be shared among a few modules of the application.
Cons: Dependency of major application on one source makes it hard to change/alter, hard to test, can create coupling between different parts of the application.
2. Facade pattern:
The idea is to create a facade or a layer/block/building between your complex code and the actual business logic. A great example of this pattern is the fetch API feature provided by the browser.
Pros: Gives a nice interface, hides/encapsulates the complex code behind the layer.
Cons: Over-simplification to an extent that it is not really usable, Over-verticalization which means that the facade created is so specific to a single use-case that it is no longer general.
3. Bridge/Adapter pattern:
Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
The problem arises when more subclasses of different behavior are added. Say you have a geometric Shape class with a pair of subclasses: Circle and Square. You want to extend this class hierarchy to incorporate colors, so you plan to create Red and Blue shape subclasses. However, since you already have two subclasses, you’ll need to create four class combinations such as BlueCircle and RedSquare.
Adding new shape types and colors to the hierarchy will grow it exponentially. For example, to add a triangle shape you’d need to introduce two subclasses, one for each color. And after that, adding a new color would require creating three subclasses, one for each shape type. The further we go, the worse it becomes.
Pros: Loosely coupled code, Increases the code maintainability, reusability, and reduces code duplication, improved Extensibility – Abstraction and implementation can be extended independently.
Cons: Increases code complexity, Multiple indirection - A level of indirection is introduced as the request is passed from the Abstraction to the actual implementer.
4. Strategy pattern:
It defines a family of algorithms and encapsulates each one and make them interchangeable. The strategy lets the algorithm vary independently from clients that use it. In simple terms, different strategies are created which can be used to solve one or more problems in different ways.
Example, going to work from home to office can have multiple strategies:
- Home -> Cab -> Office - Home -> Bike -> Office - Home -> Bus stand -> Bus -> Office, etc
Pros: Prevents conditional statements (switch, if, else…), algorithms are loosely coupled with the context entity i.e. they can be changed/replaced without changing the context entity, easy extendable.
Cons: Increases the number of objects in the application.
5. Observer pattern(pub sub):
Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.
It revolves around the popular Publisher-Subscriber mechanism where a subscriber subscribes to a publisher and publisher notifies subscriber every expected change that is made. The publisher is also referred as a Subject while the subscriber as Observer.
The straight and simple analogy to understand this is the classic Youtube Channel-Subscriber model. Whenever we subscribe to a channel on Youtube and the channel uploads a video, we are notified by the channel about the new video or a social media model sending updates related to the pages we follow.
Pros: It allows sending data to other objects effectively without any change in the Subject or Observer, Observers can be added/removed at any point in time.
Cons: Observer can add complexity and lead to inadvertent performance issues, the continuous notification sent by the subject can sometimes create spamming.
Youtube: Traversey Media, Web dev simplified
Books: Design Patterns: Elements of Reusable Object-Oriented Software by Gangs of 4, Head first Design pattern by Elisabeth Freeman and Kathy Sierra
Websites: Refactoring guru
If you 💓 this article, please hit an appreciation button and drop a comment, thank you!!