Abstract Factory Pattern in C# – Real-Time Example (UI Theme System)

What is Abstract Factory Pattern?

The Abstract Factory Pattern is a creational design pattern that creates families of related objects without specifying their concrete classes.

It provides a factory of factories.


Why Use Abstract Factory?

  • Ensure related objects are used together
  • Maintain consistency across components
  • Avoid mixing incompatible objects
  • Improve scalability and flexibility

Real-Time Scenario

In a UI system:

  • Light Theme → Light Button, Light TextBox
  • Dark Theme → Dark Button, Dark TextBox

👉 All components should match the selected theme


Implementation

Step 1: Abstract Products


public interface IButton
{
    void Render();
}

public interface ITextBox
{
    void Render();
}

Step 2: Concrete Products (Light Theme)


public class LightButton : IButton
{
    public void Render() => Console.WriteLine("Light Button");
}

public class LightTextBox : ITextBox
{
    public void Render() => Console.WriteLine("Light TextBox");
}

Step 3: Concrete Products (Dark Theme)


public class DarkButton : IButton
{
    public void Render() => Console.WriteLine("Dark Button");
}

public class DarkTextBox : ITextBox
{
    public void Render() => Console.WriteLine("Dark TextBox");
}

Step 4: Abstract Factory


public interface IUIFactory
{
    IButton CreateButton();
    ITextBox CreateTextBox();
}

Step 5: Concrete Factories


public class LightThemeFactory : IUIFactory
{
    public IButton CreateButton() => new LightButton();
    public ITextBox CreateTextBox() => new LightTextBox();
}

public class DarkThemeFactory : IUIFactory
{
    public IButton CreateButton() => new DarkButton();
    public ITextBox CreateTextBox() => new DarkTextBox();
}

Usage Example


IUIFactory factory = new DarkThemeFactory();

var button = factory.CreateButton();
var textbox = factory.CreateTextBox();

button.Render();
textbox.Render();

Output


Dark Button
Dark TextBox

Key Concept

Instead of:


new DarkButton();
new LightTextBox(); // ❌ mismatch

We do:


factory.CreateButton();
factory.CreateTextBox();

👉 Ensures consistency


Advantages

  • Ensures compatible object creation
  • Promotes consistency
  • Easy to switch entire family
  • Follows Open/Closed Principle

Disadvantages

  • More interfaces and classes
  • Complex structure
  • Harder to implement initially

When to Use

  • When system needs multiple related object families
  • When consistency between objects is required
  • When switching between configurations (themes, environments)
  • When object creation should be abstracted

Real Project Mapping (.NET + Angular)

Feature Usage
UI themes Abstract Factory
Multi-database support Abstract Factory
Payment providers (family) Abstract Factory
Multi-tenant apps Abstract Factory

Pro Tip (Advanced .NET Usage)

  • Combine with Dependency Injection for runtime switching
  • Use configuration to select factory (appsettings.json)
  • Useful in multi-environment systems (Dev, Prod)

Summary

Abstract Factory Pattern helps you:

  • Create related objects consistently
  • Switch entire families easily
  • Build scalable and maintainable systems

👉 Perfect for themes, multi-provider systems, environment-based configurations

Comments