#2 Design Patterns — Constructor Pattern
Hello Everyone, Welcome to my blog 🙏.
I hope you are all safe and that this blog finds you in good health ❤️.
In my previous blog, we have learnt about
1. What are design patterns?
2. What is Observer Pattern?
If you’ve missed it, check out here
In this blog, we will learn more about
👉 Constructor Pattern
In simple terms, a Constructor pattern is a method which creates and initialises objects of the same type. It falls under Creational patterns
As the name suggests, it is a class-based approach but let's understand it in both ways (functional & class).
How does Constructor Pattern work?
To understand it better, Let us take an example
Steve had a boutique, where he manufactures high-quality shirts and also customers like his collections and purchase them.
He imports High-quality fabrics from different countries which is a primary reason for his successful business. In his manufacturing factory, everything is automated with different machinery. For all his machines, there is a common
process followed to get similar shirts.
Now, let’s see how the constructor pattern is applied here.
Check out the working code for functional-based
Check out the working code here for class-based
This is how the process is set for manufacturing shirts.
To grow the business, Steve purchases a few more machines but still, the process would be the same.
In simple terms, a Constructor Pattern is a blueprint that can be used for creating similar objects.
Now, we know the working of the constructor pattern. let’s know when to use this.
When to use the Constructor pattern?
As the definition clearly depicts constructor pattern is used to create objects of the same type.
All the plugins, packages, libraries, etc which we use in our projects are nothing but the best examples of constructor patterns.
Here we go, That’s it folks for this blog. Hope everyone liked it.
If you like it, give it a clap 👏 ,
and share it with your friend.
For more exciting content on Frontend, Please do follow me 🎯.
Thanks a lot, Everyone 🙏.
Until Next time,
Happy Learning ✍️
Abhishek Kovuri, UI developer