The Factory Method Pattern is a creational design pattern that lets a class delegate the responsibility of creating objects to its subclasses.
- Instead of creating objects directly using
new, you define a method (called a factory method) to create objects. - This gives you flexibility and reduces tight coupling between code that uses the object and the code that creates it.
Why Use It?
- To simplify object creation.
- To hide the details of which class is being instantiated.
- To allow different types of objects to be created through a common interface.
- To decide object type dynamically at runtime.
Core Components
- Product: Abstract base class or interface for the objects
- Concrete Product: Actual class created by the factory
- Creator: Abstract class declaring the factory method
- Concrete Creator: Subclass that overrides the factory method to create specific products
Implementation of the Factory Method in C++
Let’s implement the Factory Method Pattern step by step with a dynamic shape example, which shows the real benefit of the pattern.
Step 1: Abstract Product – Shape
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
virtual ~Shape() {}
};
- This base class defines a common interface for all shapes.
- Every shape must implement the draw() function.
Step 2: Concrete Products – Circle and Square
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing Circle\n";
}
};
class Square : public Shape {
public:
void draw() override {
std::cout << "Drawing Square\n";
}
};
- These classes implement the Shape interface.
- Each class defines its own version of draw().
Step 3: Abstract Creator – ShapeFactory
class ShapeFactory {
public:
virtual Shape* createShape() = 0; // Factory method
virtual ~ShapeFactory() {}
};
- Declares a method createShape() but doesn’t implement it.
- Subclasses or implementations provide the actual object creation logic.
Step 4: Concrete Creators – CircleFactory and SquareFactory
class CircleFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Circle();
}
};
class SquareFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Square();
}
};
- These factories override createShape() to create the correct object.
Step 5: Dynamic Client Code
ShapeFactory* getFactory(char type) {
if (type == 'C') return new CircleFactory();
else if (type == 'S') return new SquareFactory();
return nullptr;
}
int main() {
char userChoice;
cout << "Enter C for Circle, S for Square: ";
cin >> userChoice;
ShapeFactory* factory = getFactory(userChoice);
if(factory) {
Shape* shape = factory->createShape();
shape->draw();
delete shape;
delete factory;
}
return 0;
}
- The main function creates factories and uses them to get shapes.
- The decision of which object to create happens at runtime.
- Client only interacts with Shape*, not Circle or Square.
Advantages
The Factory Method pattern provides several benefits that improve code flexibility and maintainability:
- Loose Coupling: Client doesn’t depend on concrete classes.
- Easy to Extend: Add new types by creating new factories.
- Cleaner Code: Object creation logic is in one place.
- Supports Polymorphism: You can treat all shapes uniformly.
Disadvantages
Despite its benefits, the Factory Method pattern also has some drawbacks to consider:
- More Classes: One factory per product type increases class count.
- More Complexity: Not worth using for very simple object creation.
- Subclassing Needed: Every new type needs a new factory subclass.
Real-World Use Cases
The Factory Method pattern is widely used in software development to create objects dynamically based on runtime conditions.
- Logger Factory: Creates different types of loggers.
- Database Connector: Creates connectors for MySQL, PostgreSQL, etc.
- UI Component Factory: Used in frameworks like Qt or JavaFX.
- Game Object Factory: Creates enemies, power-ups, etc., in games.

