Top killer software design patterns you should know 🚀

·

4 min read

Top killer software design patterns you should know 🚀

In software engineering Design patterns refers to the typical solution to commonly occurring problems. These patterns can be customised to solve a different design problem in software design. These patterns are broadly categorised into three:- creational, structural, and behavioural design patterns. This article aims to brief the common design patterns that every developer should know.

1. Singleton

At the top of our list is the singleton pattern. This is a creational pattern that ensures that only one instance of the class is created and shared throughout the program code. The most common reason for the pattern is the control of shared resources. for example, in your application, you may only need to create a single database connections pool or instance of logger or cache and share it throughout your application code.

This can be achieved by tracking whether an instance of a class has been created before creating a new one. If the object has been created before, the initially created object is returned instead of creating a new one.

2. Observer

This design pattern defines a one-to-many relationship between objects. It eases the propagation of state changes of one object(subject) to the other objects(observers) interested in the changes. The observer will often create a subscription on the subject, and the subject will intern update all the subscribers of that subject in case of a state change. This pattern is also known as publisher/subscriber.

for example, If you subscribe to a newspaper or magazine, you no longer need to go to the store to check if the next issue is available. Instead, the publisher sends new issues directly to your mailbox right after publication or even in advance.

The publisher maintains a list of subscribers and knows which magazines they’re interested in. Subscribers can leave the list whenever they wish to stop the publisher from sending new magazine issues to them.

3. Chain of Responsibility

A chain of Responsibility is a behavioural design pattern that lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

Imagine you're building a school management system, whenever a request is received from the client you may need to perform a set of operations ie. validation, authentication, authorization checks, and serve the request response from the cache if it exists, before processing the request.

Chain of responsibility patterns relies on transforming these operations into stand-alone objects referred to as handlers. These methods are chained together, the request object is passed to the fast handler in the chain process and if it is successfully processed this handler decides whether to pass it to the next handler in the chain or not.

4. Factory Method

This design pattern provides an interface for creating objects by providing some information about the object such as its type without providing its underlying implementation logic.

Imagine that you start a motor production factory and the first release of your motors are say trucks, but due to the high quality of your motors your customers start to request for busses too. This is good for the business, right? but there is a problem, and that is your production line was only engineered to produce trucks this means the entire production line will have to be re-engineered to support the production of busses too, easy right? what if your customer request for more different motors says motorbikes, cars.

This cycle may become expensive and tiresome for your company. Maybe it would be easy if we have a single production line that can easily be switched to different production modes, say switching to different programs without having to redesign it again.

This is the type of design problem that the factory method solves in your code, that is defining a creation interface that every object must meet. Thus making it possible to create objects of different types by simply providing information such as object type without providing the creation logic.

5. Constructor/Builder

Builder is a design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code. This design pattern may be suitable, when the process involved in creating an object is extremely complex with lots of mandatory and optional parameters, an increase in the number of constructor parameters leads to a large list of constructors or when the client expects different representations for the object that's constructed.

Conclusion

In this article, we have briefed the top 5 important design patterns that you should know as a developer. This includes the singleton pattern that ensures that only one instance of the class is created and shared throughout the program code, observer patterns that define a one-to-many relationship between objects, a chain of responsibility that lets you pass requests along a chain of handlers, factory method that provides an interface for creating objects by providing some information about the object such as its type without providing its underlying implementation logic and constructor pattern that lets you construct complex objects step by step.

Did you find this article valuable?

Support Evans Opilo by becoming a sponsor. Any amount is appreciated!

Â