The factory design pattern is a creational design pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created. It is used to replace constructor calls with a factory method call, which creates the objects.
The factory pattern is a way to create objects in a super class, but allow subclasses to alter the type of objects that will be created. The factory pattern is useful when a class doesn’t know what sub-classes will be required to create, or it needs to create multiple possible objects based on the input provided.
Here is an example of the factory pattern in Apex:
/* * @description: Factory design pattern * @author: sfdc4u.com */ public abstract class Creator { public abstract Product factoryMethod(); } public class ConcreteCreatorA extends Creator { public Product factoryMethod() { return new ConcreteProductA(); } } public class ConcreteCreatorB extends Creator { public Product factoryMethod() { return new ConcreteProductB(); } } public abstract class Product { // Product interface goes here } public class ConcreteProductA extends Product { // Product A implementation goes here } public class ConcreteProductB extends Product { // Product B implementation goes here }
In this example, the Creator class is an abstract class that defines a factory method for creating products. The ConcreteCreatorA and ConcreteCreatorB classes are concrete implementations of the Creator class, and they override the factoryMethod to create ConcreteProductA and ConcreteProductB objects, respectively. The Product class is an abstract class that defines the interface for products, and the ConcreteProductA and ConcreteProductB classes are concrete implementations of the Product class.
To use the factory pattern, a client class would create an instance of a ConcreteCreator class and call its factoryMethod to create a Product object. The client class would not need to know the specific type of Product object that is being created, as it only needs to know the interface defined by the Product class.
/* * @description: Factory design pattern * @author: sfdc4u.com */ public class Client { public void someMethod() { Creator creator = new ConcreteCreatorA(); Product product = creator.factoryMethod(); // Use the product } }
Advantages of the factory pattern
1. It decouples the client class from the concrete implementations of the Product class. The client class only needs to know the interface defined by the Product class, and not the specific implementation of the Product object that it is using. This allows the client class to work with any concrete implementation of the Product class, as long as it adheres to the interface defined by the Product class.
2. It allows for the creation of objects to be centralized in one place, rather than being spread out throughout the codebase. This can make it easier to maintain and modify the code, as changes to the way objects are created can be made in a single location.
Disadvantage
It can make the code more complex, as it requires the creation of additional classes and methods. Additionally, the factory pattern can make it more difficult to understand the code, as it requires a deeper understanding of how the different classes and methods interact with each other.
Conclusion
The factory design pattern is a useful way to create objects in a super class, but allow subclasses to alter the type of objects that will be created.
1 thought on “Factory design pattern”