Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# Abstract Factory Design Pattern



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. This pattern allows you to create objects that follow a general pattern, making the code more modular and scalable.

Here's an example of how you can implement the Abstract Factory Design Pattern in C#:

csharp
using System;

// Abstract Product A
public interface IAbstractProductA
{
    void OperationA();
}

// Abstract Product B
public interface IAbstractProductB
{
    void OperationB();
}

// Concrete Product A1
public class ConcreteProductA1 : IAbstractProductA
{
    public void OperationA()
    {
        Console.WriteLine("Operation of ConcreteProductA1");
    }
}

// Concrete Product A2
public class ConcreteProductA2 : IAbstractProductA
{
    public void OperationA()
    {
        Console.WriteLine("Operation of ConcreteProductA2");
    }
}

// Concrete Product B1
public class ConcreteProductB1 : IAbstractProductB
{
    public void OperationB()
    {
        Console.WriteLine("Operation of ConcreteProductB1");
    }
}

// Concrete Product B2
public class ConcreteProductB2 : IAbstractProductB
{
    public void OperationB()
    {
        Console.WriteLine("Operation of ConcreteProductB2");
    }
}

// Abstract Factory
public interface IAbstractFactory
{
    IAbstractProductA CreateProductA();
    IAbstractProductB CreateProductB();
}

// Concrete Factory 1
public class ConcreteFactory1 : IAbstractFactory
{
    public IAbstractProductA CreateProductA()
    {
        return new ConcreteProductA1();
    }

    public IAbstractProductB CreateProductB()
    {
        return new ConcreteProductB1();
    }
}

// Concrete Factory 2
public class ConcreteFactory2 : IAbstractFactory
{
    public IAbstractProductA CreateProductA()
    {
        return new ConcreteProductA2();
    }

    public IAbstractProductB CreateProductB()
    {
        return new ConcreteProductB2();
    }
}

// Client code
class Program
{
    static void Main(string[] args)
    {
        IAbstractFactory factory1 = new ConcreteFactory1();
        IAbstractProductA productA1 = factory1.CreateProductA();
        IAbstractProductB productB1 = factory1.CreateProductB();
        productA1.OperationA();
        productB1.OperationB();

        IAbstractFactory factory2 = new ConcreteFactory2();
        IAbstractProductA productA2 = factory2.CreateProductA();
        IAbstractProductB productB2 = factory2.CreateProductB();
        productA2.OperationA();
        productB2.OperationB();

        // Outputs:
        // Operation of ConcreteProductA1
        // Operation of ConcreteProductB1
        // Operation of ConcreteProductA2
        // Operation of ConcreteProductB2
    }
}

Explanation:

  1. Abstract Products (IAbstractProductA and IAbstractProductB): These interfaces define the methods that all concrete products must implement.

  2. Concrete Products (ConcreteProductA1, ConcreteProductA2, ConcreteProductB1, ConcreteProductB2): These classes implement the abstract product interfaces and define the specific behavior for each product.

  3. Abstract Factory (IAbstractFactory): This interface declares the methods for creating abstract products.

  4. Concrete Factories (ConcreteFactory1 and ConcreteFactory2): These classes implement the abstract factory interface and return instances of the concrete products.

  5. Client Code: The client code uses the abstract factory to create products without needing to know the specific type of product that will be created. This makes the code more flexible and easier to extend.

The Abstract Factory Design Pattern is useful when you need to create families of related objects and ensure they are compatible with each other. It promotes consistency among products and makes it easier to add new product families.




All rights reserved | Privacy Policy | Sitemap