Monday, March 2, 2026

Creational Design Patterns

Creational patterns deal with object creation mechanisms.


1) Singleton Pattern

Ensures only one instance of a class exists.

Example

Used for logging, configuration, database connection, etc.

public class Logger
{
    private static Logger _instance;

    private Logger() { }

    public static Logger GetInstance()
    {
        if (_instance == null)
            _instance = new Logger();

        return _instance;
    }

    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

// Usage
Logger log1 = Logger.GetInstance();
Logger log2 = Logger.GetInstance();

Console.WriteLine(log1 == log2); // True (same instance)

2) Factory Method Pattern

Creates object without exposing instantiation logic.

Example

Creating different types of vehicles.

public interface IVehicle
{
    void Drive();
}

public class Car : IVehicle
{
    public void Drive()
    {
        Console.WriteLine("Car Driving");
    }
}

public class Bike : IVehicle
{
    public void Drive()
    {
        Console.WriteLine("Bike Riding");
    }
}

public class VehicleFactory
{
    public static IVehicle GetVehicle(string type)
    {
        if (type == "Car")
            return new Car();
        else if (type == "Bike")
            return new Bike();
        else
            return null;
    }
}

// Usage
IVehicle vehicle = VehicleFactory.GetVehicle("Car");
vehicle.Drive();

3) Abstract Factory Pattern

Creates families of related objects.

Example

Creating Windows and Mac UI elements.

public interface IButton
{
    void Render();
}

public class WindowsButton : IButton
{
    public void Render()
    {
        Console.WriteLine("Windows Button");
    }
}

public class MacButton : IButton
{
    public void Render()
    {
        Console.WriteLine("Mac Button");
    }
}

public interface IUIFactory
{
    IButton CreateButton();
}

public class WindowsFactory : IUIFactory
{
    public IButton CreateButton()
    {
        return new WindowsButton();
    }
}

public class MacFactory : IUIFactory
{
    public IButton CreateButton()
    {
        return new MacButton();
    }
}

// Usage
IUIFactory factory = new WindowsFactory();
IButton button = factory.CreateButton();
button.Render();

4) Builder Pattern

Builds complex object step by step.

Example

Building a House object.

public class House
{
    public string Walls { get; set; }
    public string Roof { get; set; }
}

public class HouseBuilder
{
    private House _house = new House();

    public HouseBuilder BuildWalls()
    {
        _house.Walls = "Concrete Walls";
        return this;
    }

    public HouseBuilder BuildRoof()
    {
        _house.Roof = "Steel Roof";
        return this;
    }

    public House GetHouse()
    {
        return _house;
    }
}

// Usage
House house = new HouseBuilder()
                .BuildWalls()
                .BuildRoof()
                .GetHouse();

5) Prototype Pattern

Creates new object by copying existing object.

Example

public class Person : ICloneable
{
    public string Name { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

// Usage
Person p1 = new Person { Name = "Naresh" };
Person p2 = (Person)p1.Clone();

Console.WriteLine(p2.Name);

Summary (Simple Understanding)

Singleton → One object only
Factory → Create object based on condition
Abstract Factory → Create related object families
Builder → Build complex object step by step
Prototype → Copy existing object