DEV Community

Juarez Júnior
Juarez Júnior

Posted on

C# Design Pattern: Abstract Factory

The Abstract Factory pattern is useful when you want to create families of related objects without specifying the concrete classes directly. It provides an interface to create different objects that belong to the same family. This pattern is common in situations where you want to ensure that different products work well together, like creating user interfaces that depend on the operating system or creating different types of vehicles, each with specific parts that need to be compatible with each other.

C# Code Example:

// Abstract product 1
public abstract class Car
{
    public abstract void ShowDetails();
}

// Concrete product 1A
public class Sedan : Car
{
    public override void ShowDetails()
    {
        Console.WriteLine("Sedan Car");
    }
}

// Concrete product 1B
public class SUV : Car
{
    public override void ShowDetails()
    {
        Console.WriteLine("SUV Car");
    }
}

// Abstract product 2
public abstract class Engine
{
    public abstract void ShowType();
}

// Concrete product 2A
public class CombustionEngine : Engine
{
    public override void ShowType()
    {
        Console.WriteLine("Combustion Engine");
    }
}

// Concrete product 2B
public class ElectricEngine : Engine
{
    public override void ShowType()
    {
        Console.WriteLine("Electric Engine");
    }
}

// Abstract factory
public abstract class VehicleFactory
{
    public abstract Car CreateCar();
    public abstract Engine CreateEngine();
}

// Concrete factory 1
public class SedanCombustionFactory : VehicleFactory
{
    public override Car CreateCar()
    {
        return new Sedan();
    }

    public override Engine CreateEngine()
    {
        return new CombustionEngine();
    }
}

// Concrete factory 2
public class SUVElectricFactory : VehicleFactory
{
    public override Car CreateCar()
    {
        return new SUV();
    }

    public override Engine CreateEngine()
    {
        return new ElectricEngine();
    }
}

class Program
{
    static void Main(string[] args)
    {
        VehicleFactory factory = new SedanCombustionFactory();
        Car car = factory.CreateCar();
        Engine engine = factory.CreateEngine();

        car.ShowDetails();   // Output: Sedan Car
        engine.ShowType();   // Output: Combustion Engine

        factory = new SUVElectricFactory();
        car = factory.CreateCar();
        engine = factory.CreateEngine();

        car.ShowDetails();   // Output: SUV Car
        engine.ShowType();   // Output: Electric Engine
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

In this example, the abstract factory VehicleFactory defines two methods to create related objects: a car and an engine. Each concrete factory (SedanCombustionFactory and SUVElectricFactory) creates a specific combination of car and engine that are compatible with each other. In the main code, you can switch between the factories to create different combinations of cars and engines, like a Sedan with a combustion engine or an SUV with an electric engine.

Conclusion:

The Abstract Factory pattern is useful when you want to create families of objects that work together. It helps maintain consistency between the products created, as the concrete factories ensure that each combination of objects is compatible. This pattern is ideal for scenarios where different configurations of objects need to be created modularly without exposing implementation details.

Source code: GitHub

Top comments (0)