02 Dec 2023
Intermediate
The Factory Design Pattern is a creational design pattern that defines an interface for creating objects in a superclass , but allows subclasses (or Concrete Classes) to alter the type of objects that will be created.
Note📝:
Superclass (or Interface/Abstract Class):This is the class that declares the interface for creating objects. In the above definition, it's mentioned as the "superclass" that defines an interface for creating objects. The superclass typically includes one or more abstract methods that the subclasses must implement. It provides a common interface for creating objects, but it doesn't necessarily specify the exact classes of objects to be created.Subclass (or Concrete Class):These are the classes that implement the interface declared by the superclass. In the Factory Design Pattern, subclasses are responsible for instantiating and returning specific types of objects. The choice of the concrete class determines the type of object that will be created. The flexibility of the pattern lies in the ability to have multiple subclasses that provide different implementations of the object creation process.
The Factory Design Pattern separates the responsibility of creating objects from the client code that uses these objects. The superclass declares the interface, and subclasses provide concrete implementations, allowing the client code to create objects without specifying their exact types. This promotes flexibility, extensibility, and code maintainability.
key points of the Factory Design Pattern:
-
Intent:
- The main intent of the Factory pattern is to define an interface for creating objects but let subclasses alter the type of objects that will be created. It provides a way to delegate the instantiation logic to subclasses.
-
Key Components:
- Creator (Factory):
- Declares the factory method, which returns an object of type Product. It may also define other methods that use the product.
- ConcreteCreator (ConcreteFactory):
- Implements the factory method to create a specific instance of the product.
- Product:
- Defines the interface of objects the factory method creates.
- ConcreteProduct:
- Implements the Product interface. It is the specific type of object created by the ConcreteCreator.
- Creator (Factory):
-
Factory Method:
- The factory method is an abstract method declared in the Creator (Factory) class. It is responsible for creating an instance of the Product.
-
Object Creation Delegation:
- The Factory pattern allows the responsibility of creating objects to be delegated to subclasses. Subclasses implement the factory method to create instances of specific products.
-
Advantages:
- Flexibility: The Factory pattern provides a way to delegate the responsibility of instantiating objects to subclasses, allowing flexibility in choosing the concrete classes to be created.
- Encapsulation: The client code is shielded from the details of object creation, focusing only on the interface of the created objects.
-
Use Cases:
- The Factory pattern is useful when a class cannot anticipate the class of objects it must create.
- It is applicable when a class wants its subclasses to specify the objects it creates.