Abstract Factory Design Pattern in C# with Real-Time Example

In this article, we will discuss Abstract Factory Design Patterns in C# with models. it is a type of Creational Design Pattern.

What is Abstract Factory Design Pattern?

According to the Gang of Four Definition: “The Abstract Factory Design Pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes “.

The Abstract Factory Design Pattern is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows you to produce different sets of products that belong to different families.
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes

Abstract Factory pattern belongs to creational patterns and is one of the most used design patterns in real-world applications

An abstract factory is a super factory that creates other factories

We need to Choose an Abstract Factory Pattern when

  1. The application needs to create multiple families of objects or products
  2. We need to use only one of the subsets of families of objects at a given point of time
  3. We want to hide the implementations of the families of products by decoupling the implementation of each of these operations

Here's a real-time example in C#: creating a family of UI components (buttons and text boxes) for different platforms (Windows and Mac).

Step 1: Define Abstract Product Interfaces


public interface IButton
{
    void Render();
}

public interface ITextBox
{
    void Render();
}

Step 2: Implement Concrete Products

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

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

public class WindowsTextBox : ITextBox
{
    public void Render()
    {
        Console.WriteLine("Rendering Windows TextBox");
    }
}

public class MacTextBox : ITextBox
{
    public void Render()
    {
        Console.WriteLine("Rendering Mac TextBox");
    }

}

Step 3: Define Abstract Factory Interface


public interface IUIFactory
{
    IButton CreateButton();

    ITextBox CreateTextBox();
}

Step 4: Implement Concrete Factories


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

    public ITextBox CreateTextBox()
    {
        return new WindowsTextBox();
    }
}

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

    public ITextBox CreateTextBox()
    {
        return new MacTextBox();
    }
}

Step 5: Use the Abstract Factory Pattern

class Program
{
    static void Main(string[] args)
    {
        IUIFactory factory;
        // Create a Windows UI

        factory = new WindowsFactory();

        IButton windowsButton = factory.CreateButton();

        ITextBox windowsTextBox = factory.CreateTextBox();

        windowsButton.Render();

        windowsTextBox.Render();
        // Create a Mac UI

        factory = new MacFactory();

        IButton macButton = factory.CreateButton();

        ITextBox macTextBox = factory.CreateTextBox();

        macButton.Render();

        macTextBox.Render();

    }

}

Explanation

  1. IButton and ITextBox: Abstract product interfaces that define the common behavior for buttons and text boxes.
  2. WindowsButton, MacButton, WindowsTextBox, and MacTextBox: Concrete product classes that implement the abstract product interfaces for different platforms.
  3. IUIFactory: The abstract factory interface that declares methods for creating abstract products (buttons and text boxes).
  4. WindowsFactory and MacFactory: Concrete factory classes that implement the IUIFactory interface to create products for Windows and Mac platforms, respectively.
  5. Program: The client code that uses the abstract factory pattern to create a family of related UI components for different platforms.

This example demonstrates how the Abstract Factory Design Pattern can be used to create families of related objects without specifying their concrete classes, making it easy to switch between different product families (e.g., Windows UI and Mac UI) without changing the client code.

 

When to use it Abstract Factory Design Pattern?

When we need to use the Abstract Factory Design Pattern in the following cases

  1. When you want to create a set of related objects or dependent objects which must be used together.
  2. When the system should be configured to work with multiple families of products.
  3. When the Concrete classes should be decoupled from the clients.


Differences between Abstract Factory and Factory Method Design Pattern:

Share this

Related Posts

Previous
Next Post »

1 comments:

Write comments