Design Pattern — Creational — Low level system design — Singleton, Builder, Prototype design pattern (part 2)
If you have not seen part 1 which explains Factory and Abstract Factory Design pattern then it’s a must-watch one. Please find the link for the same below. Understanding both the design patterns will be a great learning
Now, let’s continue with other creational design patterns. Let’s start with Singleton -
3. Singleton Design Pattern
This design pattern says that if there is going to be one class reference for entire class objects with all updates and changes are to be reflected in all the objects instantiating that class then this design pattern comes into great application.
Example:
Let’s consider that we are building a common ORM factory which is simply responsible for creating other ORMS based on a certain type. This is a classic example that we learnt in our last design pattern course on factory design patterns. In that, we used the factory as a static method which in turn serves as a way to generalise the function across all the class’s objects. Below is a simple example to understand the same
4. Builder Design Pattern
Builder pattern emphasises creating a complex object by building it in a small part-by-part manner.
The below example is easy to interpret—
As you can see, we first enable the Enterprise plan for a customer then update his SMS limit to a custom value and the same goes with the Email limit. This is a classic simple example of the builder pattern.
5. Prototype
There is a necessity generally in objects to be cloned into another object while we code. This pattern emphasizes the fact of creating a common interface which declares clone(..) and this clone can be further used to clone the class into another class.
A simple code example explaining the same is mentioned below —