Factory Design Pattern is one of the most widely used patterns in Programming Languages like Java, Javascript, Python, etc.
Here's the simplest explanation to what it is:
Here's the simplest explanation to what it is:
1. The factory design pattern is a way to create objects without specifying the exact class of object.
This is done by defining a separate factory class that has a method for creating objects.
Let understand this from Java's perspective.
This is done by defining a separate factory class that has a method for creating objects.
Let understand this from Java's perspective.
2. To implement the factory design pattern, we need to create an interface that defines the contract for the objects that will be created.
This interface should specify the methods that will be available on the objects.
This interface should specify the methods that will be available on the objects.
3. Next, we need to create one or more classes that implement this interface.
These classes will define the specific implementation of the methods specified in the interface.
These classes will define the specific implementation of the methods specified in the interface.
4. Finally, we need to create a factory class that has a method for creating objects.
This method should take in the necessary information for creating an object (type of object) and use that information to instantiate the appropriate class that implements the interface.
This method should take in the necessary information for creating an object (type of object) and use that information to instantiate the appropriate class that implements the interface.
5. Benefits of Factory Pattern
- As per above example - our code mainly depends on common interface (Shape) instead of Implementation classes (Circle or Rectangle). This enables us to introduce new shapes in future without having to worry about much code changes.
- As per above example - our code mainly depends on common interface (Shape) instead of Implementation classes (Circle or Rectangle). This enables us to introduce new shapes in future without having to worry about much code changes.
- Also, provides loose coupling between classes as you can plug any Implementation without breaking the code.
- Makes code more maintainable in the longer run.
- Makes code more maintainable in the longer run.
That's a wrap!
I help people get started with Backend Development.
Follow me @vikasrajputin for more.
If you find this thread helpful then Like/Retweet the first tweet below:
I help people get started with Backend Development.
Follow me @vikasrajputin for more.
If you find this thread helpful then Like/Retweet the first tweet below:
جاري تحميل الاقتراحات...