Home > C#, Design Pattern, Programming > Abstract Factory

Abstract Factory

Frequency of Use
High

Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Structure

Abstract Factory Pattern

Applicability
Use the Abstract Factory pattern when:

  • a system should be independent of how its products are created, composed, and represented.
  • a system should be configured with one of multiple families of products.
  • a family of related product objects is designed to be used together, and you need to enforce this constraint.
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Code Implementation (C#) :

using System;

namespace AbstractFactoryExample
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var factory1 = new ConcreteFactory1();
            var client1 = new Client(factory1);
            client1.run();
            
            var factory2 = new ConcreteFactory2();
            var client2 = new Client(factory2);
            client2.run();
        }
    }

    abstract class AbstractFactory
    {
        abstract public AbstractProductA CreateProductA();
        abstract public AbstractProductB CreateProductB();
    }
    
    class ConcreteFactory1 : AbstractFactory
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }
        
        public override AbstractProductB CreateProductB()
        {
            return new ProductB1();        
        }
    }
    
    class ConcreteFactory2 : AbstractFactory
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }
        
        public override AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }
    }
    
    abstract class AbstractProductA
    {
        
    }
    
    class ProductA1 : AbstractProductA
    {
        
    }
    
    class ProductA2 : AbstractProductA
    {
        
    }
    
    abstract class AbstractProductB
    {
        abstract public void Interact(AbstractProductA productA);
    }
    
    class ProductB1 : AbstractProductB
    {
        public override void Interact(AbstractProductA productA)
        {
            Console.WriteLine(this.GetType().Name + 
                              " Interact With " + 
                              productA.GetType().Name);
        }
    }
    
    class ProductB2 : AbstractProductB
    {
        public override void Interact(AbstractProductA productA)
        {
            Console.WriteLine(this.GetType().Name + 
                              " Interact With " + 
                              productA.GetType().Name);
        }
    }

    class Client
    {
        private AbstractProductA _productA;
        private AbstractProductB _productB;
        
        public Client(AbstractFactory factory)
        {
            _productA = factory.CreateProductA();
            _productB = factory.CreateProductB();
        }

        public void run()
        {
            _productB.Interact(_productA);
        }
    }
}

Output Code

ProductB1 Interact With ProductA1
ProductB2 Interact With ProductA2
Advertisements
Categories: C#, Design Pattern, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: