Introduction To Design Patterns
For experienced developers, learning some design patterns will greatly improve the quality of your work. Learning how to structure your application especially bigger projects can make building and maintaining it a lot easier.
While design patterns isn't necessarily something beginners should prioritize learning over other topics such as programming fundamentals, I think at least being aware of them is beneficial.
What is a Design Pattern? 🤷♂️
A design pattern is the reusable form of a solution to common challenges in software development. They’re templates for how to solve a problem and can be used in different situation. They’re formalized best practices which can be utilized when designing an application or system.
When writing code, its common to have hidden subtle issues which aren’t easily detected, some of which can cause larger problems as the source code grows. Reusing design patterns helps prevent such issues and also improves code readability, particularly those who’re familiar with them.
Types of Design Pattern 💁♂️
There are three main classifications of design pattern, each categorized based on the kind of problem they solve
Creational Patterns 👨🌾
Creational patterns are used to create objects in a controlled way instead of directly instantiating them. Common creational patterns are:
The builder pattern is used to build an object by assembling the output in many steps to get the final object instance. Its intent is to separate the construction of a complex object by breaking it down into smaller chunks.
💉 Dependency Injection
A technique where an object receives other objects it depends on. The dependency is passed-in or “injected” to the receiving object (also called the “client”). Its intent is for the client to not be concerned or responsible for how or where the dependency is constructed.
🏭 Factory method
The Factory Method pattern is to use a method which will deal with the problem of creating objects without needing to specify exact class for creating that object. Its intent is to have a single class be responsible for creating the right objects based depending on the context provided.
The Prototype pattern is used to create a clone or a duplicate of an existing object. Its intent is to avoid creating a new object in the normal way if that operation in particular is expensive.
The Singleton Design Pattern ensures that only one instance of a class can exists throughout the application runtime. Its intent is to allow global access to a shared resource i.e. a database or a file.
Structural Patterns 👷
Structural patterns handle different classes or objects and is concerned with how they’re composed to form larger structures.
Also known as Wrapper/Translator, this allows the adapting of interfaces into another to meet your client/customers requirements. Its intent is to allow classes to work together that could not otherwise because of incompatible interfaces.
The Facade pattern involves a class which provides simplified methods and delegate calls to methods of an existing system. Its intent is to hide the complexities of a system and provides an interface to make it easier for another client to use.
The Decorator pattern is to create new class to act as a wrapper to an existing class. Its intent is to allow the addition of new functionality of existing objects without altering its structure.
The bridge pattern solves the problem of binding between functional abstraction and implementation. Its intent is to decouple abstraction from implementation so that the two can vary independently.
The Composite patterns intent is to "compose" objects into tree structures to represent part-whole hierarchies.
Behavioural Pattern 👨🏫
Behavioural patterns identify common communication patterns between objects.
📢 Command Pattern
The command pattern turns a request into a stand-alone object which contains all the information about the request. Its intent is to allow for passing request as method arguments, delay, queue a request’s execution or support undoable operations.
⛓ Chain of Responsibility Pattern
This patterns allows for passing a request along a chain of handler objects, On receiving the request the handler object can choose to perform the action or can choose to send the request to the next handler in the chain.
📡 Observer Pattern
The Observer pattern lets you define a subscription so multiple objects can be notified about triggered events. Its intent is to notify all objects that have subscribed to an event occurrence.
This is a basic introduction to design patterns, there are more which haven't been listed in this post but these are the ones I've explored so far, my plan is to create individual blog post for each of these to go into more detail and provide code examples to help understand how they're implemented and what problems they solve.
I will include a direct link to those on this post as soon as I publish them, I'll also try to further improve this article as my understanding of each design pattern improves.
Thank you for reading and warm regards.