Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C#:Abstract Factory Pattern

0.00/5 (No votes)
18 Jun 2007 1  
This tutorial describes the implementation of Abstract Factory Pattern in c#

Introduction

Design patterns make it easier to reuse successful designs and architectures.
Design patterns help you choose design alternatives that make a system reusable and avoid alternatives that compromise reusability.
An abstract factory provides an interface for creating families of related objects without specifying their concrete classes

The Abstract Factory pattern let's us group like factory classes together. In the example below we group a factory for creating Indian Bread and a factory for creating American bread together and then we decide what "kind of" bread to create based on the information which we will get at run time.

Using the code

And below there is an implementation in C#:

//Let's define type of bread bases

public enum BreadBase
{
     HotIndianMasalaBase,
     PunjabiTadkaBase,
     ItalianCheeseBase,
     VeggieBase,
}
//This is a breadfactory where people visit to get their favorite bread bases

public interface BreadFactory
{
    Bread GetBread(BreadBase BreadBase);
}

//The abstract bread

public interface Bread
{
    void Bake();
}

//create concrete classes


public class HotIndianMasalaBread :Bread
{
    public void Bake()
    {
        Console.WriteLine ("For you::Hotindian Masala base Bread.");
    }
}
public class VeggieBread : Bread
{
    public void Bake()
    {
        Console.WriteLine("For you::Veggie base Bread.");
    }
}

public class ItalianCheeseBread : Bread
{
    public void Bake()
    {
        Console.WriteLine("For you::Italian cheese base Bread.");
    }
}
public class PunjabiTadkaBaseBread : Bread
{
    public void Bake()
    {
        Console.WriteLine("For you::Punjabi tadka base bread.");
    }
}
//Lets create bread factories aka concrete classes


public class AmericanBreadFactory :BreadFactory
{
    public Bread GetBread(BreadBase BreadBase)
    {
        Bread vBread = null;
        switch (BreadBase)
        {
            case BreadBase.VeggieBase:
                vBread = new VeggieBread();
                break;
            case BreadBase.ItalianCheeseBase:
                vBread = new ItalianCheeseBread();
                break;
        }
        return vBread;
    }
}

public class IndianBreadFactory :BreadFactory
{
    public Bread GetBread(BreadBase BreadBase)
    {
        Bread vBread = null;
        switch (BreadBase)
        {
            case BreadBase.HotIndianMasalaBase:
                vBread = new HotIndianMasalaBread();
                break;
            case BreadBase.PunjabiTadkaBase:
                vBread = new PunjabiTadkaBaseBread();
                break;
        }
        return vBread;
    }
}

//lets order breads


class Program
{
    static void Main(string[] args)
    {
    //example of abstract factory

    AmericanBreadFactory vAmericanBread = new AmericanBreadFactory();
    Bread vBread = vAmericanBread.GetBread(BreadBase.VeggieBase);
    vBread.Bake();

    //lets bak indian punjabi tadka bread

    IndianBreadFactory vIndianBreadFactory = new IndianBreadFactory();
    Bread vIndianBread = vIndianBreadFactory.GetBread(BreadBase.PunjabiTadkaBase);
    vIndianBread.Bake();
    }
} 
//Himachalsoft.com:: my website

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here