Simple Factory Pattern
Simple Factory is a creational pattern, but it is not one of the 23 standard GoF design patterns.
Definition: Define a factory class that returns instances of different classes based on provided arguments. The created instances usually share a common parent class.
Since the methods used to create instances in this pattern are typically static, it is also known as the Static Factory Method pattern.
You pass a parameter for the product you need, and the factory returns the object without you needing to know the implementation details.
Example: A dumpling shop. When a customer wants a specific type of dumpling, the shop produces it. Here, the shop is the Factory, the dumpling is the Product, and the dumpling name is the parameter.
If a customer wants “Leek Dumplings,” “Leek” is the parameter, and the shop returns the specific product (assuming they have it).

Three Roles:
Factory (Core) Handles the internal logic for creating all products. It is called directly by the client to create objects.
Abstract Product The parent class for all objects created by the factory, defining common methods. Concrete products inherit from this.
Concrete Product The target of the creation; instances of specific classes created by the factory.
Example
| |
Adding a new product requires modifying the factory class, which violates the Open-Closed Principle.
Factory Method Pattern
Factory Method Pattern - A Class Creational Pattern.
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Structure

Roles:
- Product: Defines the interface for the objects the factory method creates.
- ConcreteProduct: Implements the Product interface.
- Creator: Declares the factory method, which returns an object of type Product. It may also define a default implementation that returns a default ConcreteProduct.
- ConcreteCreator: Overrides the factory method to return an instance of a ConcreteProduct.
Applicability
Use the Factory Method pattern when:
- A class can’t anticipate the class of objects it must create.
- A class wants its subclasses to specify the objects it creates.
- Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
Example
| |
Abstract Factory Pattern
Abstract Factory Pattern - An Object Creational Pattern.
Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Structure

Roles:
- AbstractFactory: Declares an interface for operations that create abstract product objects.
- ConcreteFactory: Implements the operations to create concrete product objects.
- AbstractProduct: Declares an interface for a type of product object.
- ConcreteProduct: Defines a product object to be created by the corresponding concrete factory; implements the AbstractProduct interface.
- Client: Uses only interfaces declared by AbstractFactory and AbstractProduct classes.
Applicability
Use the Abstract Factory pattern when:
- A system should be independent of how its products are created, composed, and represented.
- A system should be configured with one of multiple families of products.
- A family of related product objects is designed to be used together, and you need to enforce this constraint.
- You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
Example
| |