Contents Creational design patterns Factory Method When to use Factory Method Design Pattern in Java? Usage of Factory Design Pattern
Creational design patterns In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design.
Factory Method I s a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Continue… Factory pattern is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
Factory Method The factory method in the interface lets a class defer the instantiation to one or more concrete subclasses. Since these design patterns talk about the instantiation of an object they come under the category of creational design pattern.
Factory Method If we notice the name Factory method , that means there is a method which is a factory, and in general, factories are involved with creational stuff and here with this, an object is being created. It is one of the best ways to create an object where object creation logic is hidden from the client. Now Let’s look at the implementation.
When to use Factory Method Design Pattern in Java? Factory method design pattern can be used in java in following cases: A class cannot predict the type of objects it needs to create. A class wants its subclasses to specify the objects it creates. Classes delegate responsibility to one of multiple helper subclasses, and you aim to keep the information about which helper subclass is the delegate within a specific scope or location.
Usage of Factory Design Pattern When a class doesn't know what sub-classes will be required to create When a class wants that its sub-classes specify the objects to be created. When the parent classes choose the creation of objects to its sub-classes.