Thursday, August 25, 2011

Abstract Factory Design Pattern


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

The Abstract Factory pattern is one level of abstraction higher than the ‘factory pattern’.
You can use this pattern when you want to return one of several related classes of objects, each of which can return several different objects on request.

In other words, the Abstract Factory is a factory object that returns one of several factories.

The Abstract Factory pattern is one level of abstraction higher than the ‘factory pattern’.
It’s another level of factory pattern that encapsulates one or more factories to adhere common interface.

In other word Abstract factory pattern is a factory object that returns one or several factories.

This pattern is very fundamental and be implemented in most of multilevel class hierarchy.
In general we could use this pattern when we want to utilize an object among the several objects of which can be further utilized to retrieve objects.

Model:







UML class diagram:




Structure


• Abstract Factory, declares an interface for operations that create abstract items.
• Concrete Factory, implements the operations for creating concrete item objects.
• Abstract Item, declares an interface for a type of item object.
• Item, defines an item object to be created by the corresponding concrete factory that implements the Abstract Item interface.
• Client, uses interfaces declared by the Abstract Factory and Abstract Item classes.

UML Diagram:






 This pattern is quite useful in systems where we want to expose a library of products to be used by a client, independent of their implementations. It is worthwhile to note that the Factories used by the client are typically Singletons, and the Abstract Factory is only an interface, the concrete implementation of the factory actually creates the products. For this, the interface defines a Factory Method (another creational design pattern) for each product, which is in turn implemented in the concrete factory.


Example Below:-

You can also compare the following Example UML Diagram with above showing UML diagram of Abstract factory.






Example Code is as following:-

using System;
using System.Configuration;

namespace AbstractFactory
{
    public interface IButton
    {
        void Show();
    }

    public interface ILabel
    {
        void Show();
    }

    public interface IGUIFactory
    {
        IButton CreateButton();
        ILabel CreateLabel();
    }

    public class MacButton : IButton
    {
        public void Show()
        {
            System.Console.WriteLine("I'm an MacButton");
        }
    }

    public class WinButton : IButton, ILabel
    {
        public void Show()
        {
            System.Console.WriteLine("I'm a WinButton");
        }
    }

    public class MacLabel : ILabel
    {
        public void Show()
        {
            System.Console.WriteLine("I'm an MacLabel");
        }
    }

    public class WinLabel : ILabel
    {
        public void Show()
        {
            System.Console.WriteLine("I'm a WinLabel");
        }
    }

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

        public ILabel CreateLabel()
        {
            return new MacLabel();
        }
    }

    public class WinFactory : IGUIFactory
    {
        public IButton CreateButton()
        {
            return new WinButton();
        }

        public ILabel CreateLabel()
        {
            return new WinLabel();
        }
    }

    public class Client
    {
        static void Main(string[] args)
        {
            IGUIFactory objWinFactory = new WinFactory();
            Console.WriteLine("*****  " + "Going to Create Controls for " + objWinFactory.GetType() + "  *****");
            IButton buttonWinFactory = objWinFactory.CreateButton();
            buttonWinFactory.Show();
            ILabel labelWinFactory = objWinFactory.CreateLabel();
            labelWinFactory.Show();


            IGUIFactory objMacFactory = new MacFactory();
            Console.WriteLine("*****  " + "Going to Create Controls for " + objMacFactory.GetType() + "  *****");
            IButton buttonMacFactory = objMacFactory.CreateButton();
            buttonMacFactory.Show();
            ILabel labelMacFactory = objMacFactory.CreateLabel();
            labelMacFactory.Show();

            Console.ReadLine();
        }
    }
}



Output:-

*****  Going to Create Controls for AbstractFactory.WinFactory  *****

I'm a WinButton

I'm a WinLabel

*****  Going to Create Controls for AbstractFactory.MacFactory  *****

I'm an MacButton

I'm an MacLabel

 

 

 


Related Posts:-

Design Pattern

Factory Design Pattern

Abstract Factory Design Pattern

Difference between Factory and Abstract Factory Pattern


 

0 comments: