Factory design method is mostly used in open source frameworks. This design method is purely based on the concept of Encapsulation. It is used to create items from the factory and encapsulating it with a creation code.
Therefore, instead of creating a code on the side of the client, we can use the factory pattern in Java to do that, The best example of this pattern ]is the border factory in Java. This design method is used to create objects and offers loose coupling making it more cohesive.
The pattern encapsulates item creation concept that makes it easier to change it after you alter how the item was created.You can also introduce new items by just changing the class.
Factory design pattern makes a design, customizable and less complicated. Other similar methods usually require classes, but this method only needs a completely new operation.
Most programmers use this method to create items. The method is almost synonymous with Abstract factory method only it does not emphasize much on families. The method is defined by an architectural framework and is used by users of the same framework.
This method states that you are only required to define an interface when creating items and leave the subclass to decide which types of class to create an instance for. The method is also referred to as the virtual constructor.
Advantages of using the Factory design Method
The method gives the sub-classes the freedom to create the type of items they want
It enhances loose-coupling by removing the requirement of binding application-specific classes into the creation code.
This implies that the code only mingles with the abstract class.meaning that it will only work with any class that implements that particular interface.
The method offers an alternative for the creation of various products
The creation code formed in this method is easier to debug and troubleshooting it won’t be too hard.
Where is the factory design pattern used? This method is used in most instances where a specific class does not know what are the sub-classes that will be needed to create.
- When a certain class needs its sub-classes to specify the items to create
- When an original class chooses to create items for its various sub-classes
The factory method is one of the widely used in the Java frameworks. If you want to encapsulate an item, then try this method to obtain the best results. The method has proved useful in many cases where you are not sure the type of class to instantiate.