CLOSE
Updated on 03 Oct, 202512 mins read 358 views

When writing software, creating objects is one of the most basic operations we do. In C++, for example, it's easy as calling new. But as systems grows larger and more complex, object creation itself can become a design problem.

  • What if creating an object a long or expensive process?
  • What if your system needs to create families of related objects?
  • What if you need only a single instance of a class to exist?
  • What if you want to build objects step by step instead of all at once?

Simply scattering new everywhere doesn't give us the flexibility, scalability, or maintainability we need in these situations. This is where Creational Design Patterns comes into play.

Suppose we are building a simple drawing application with different shapes (Circle, Square).

#include <iostream>
#include <string>
using namespace std;

class Shape {
public:
    virtual void draw() = 0;
    virtual ~Shape() = default;
};

class Circle : public Shape {
public:
    void draw() override { cout << "Drawing Circle" << endl; }
};

class Square : public Shape {
public:
    void draw() override { cout << "Drawing Square" << endl; }
};

int main() {
    // We want to create shapes based on user input
    string shapeType;
    cout << "Enter shape (circle/square): ";
    cin >> shapeType;

    Shape* shape = nullptr;

    // Direct object creation
    if (shapeType == "circle") {
        shape = new Circle();
    } else if (shapeType == "square") {
        shape = new Square();
    }

    if (shape) shape->draw();

    delete shape;
    return 0;
}

What's Wrong Here?

  1. Tight Coupling
    • main() directly depends on Circle and Square.
    • If we add Triangle, we must modify main().
  2. Scattered Logic
    • Imagine 20 places in code where shapes are created -> we duplicate the same if/else logic everywhere.
  3. Poor Flexibility
    • Switching to another set of shapes (e.g., RoundCircle, FancySquare) means editing code in multiple files.
  4. Not Scalable
    • As more shapes are added, the if/else chain keeps growing, code becomes harder to maintain.

This is where Creational Design Patterns help, which we will learn through this unit.

What are Creational Design Patterns?

Creational design patterns focus on the process of object creation. Instead of hardcoding how and when objects are created, these patterns gives us reusable, flexible ways to construct them.

Definition: Creational Design Patterns provide various object creation mechanisms that make a system independent of how its objects are created, composed, and represented.

In other words: They separate the “what” from the “how” when it comes to object creation.

The Five Creational Patterns

There are five well-known creational design patterns from the “Gang of Four” (GoF) catalog. Here's a quick overview:

  1. Singleton: Ensures a class has only one instance, and provide a global point of access to it.
  2. Factory Method: Lets subclasses decide which concrete object to create, hiding the creation logic.
  3. Abstract Factory: Creates families of related objects without exposing their concrete classes.
  4. Builder: Constructs complex objects step by step, offering flexibility in the construction process.
  5. Prototype: Creates new objects by cloning an existing object (a prototype).

 

Buy Me A Coffee

Leave a comment

Your email address will not be published. Required fields are marked *

Your experience on this site will be improved by allowing cookies Cookie Policy