Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / C#
Article

C#:Abstract Factory Pattern

Rate me:
Please Sign up or sign in to vote.
3.17/5 (28 votes)
18 Jun 2007CPOL 92.9K   893   21   16
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
ASP.NET developer working in India.

Comments and Discussions

 
QuestionThis is wrong! Pin
Member 1040735431-Aug-14 17:50
Member 1040735431-Aug-14 17:50 
General[My vote of 1] This isnt abstract Factory Pin
qazi anis18-Feb-14 3:07
qazi anis18-Feb-14 3:07 
QuestionMy vote of five Pin
LeCacho19-Jan-14 2:51
professionalLeCacho19-Jan-14 2:51 
GeneralMy vote of 1 Pin
Bhupendra Chauhan20-Aug-13 21:19
professionalBhupendra Chauhan20-Aug-13 21:19 
QuestionAbstract Factory explained with Simple and elegant example Pin
Member 960340223-Nov-12 14:41
Member 960340223-Nov-12 14:41 
QuestionI'm not an expert, but this example looks like a Factory Pattern. Sorry Pin
Yogesh Desai27-Oct-12 5:04
Yogesh Desai27-Oct-12 5:04 
AnswerRe: I'm not an expert, but this example looks like a Factory Pattern. Sorry Pin
Pankaj Bahuguna27-May-13 2:25
Pankaj Bahuguna27-May-13 2:25 
GeneralMy vote of 1 Pin
huanhvhd27-Jul-11 0:27
huanhvhd27-Jul-11 0:27 
GeneralRe: My vote of 1 Pin
akhtar7868-Sep-11 23:33
akhtar7868-Sep-11 23:33 
GeneralRe: My vote of 1 Pin
dhirenipatel7-Feb-12 7:42
dhirenipatel7-Feb-12 7:42 
GeneralRe: My vote of 1 Pin
Member 1040735431-Aug-14 18:07
Member 1040735431-Aug-14 18:07 
GeneralMy vote of 5 Pin
sivanphani23-Jun-11 8:06
sivanphani23-Jun-11 8:06 
GeneralMy vote of 5 Pin
Vishal Varshney1-Jun-11 21:16
Vishal Varshney1-Jun-11 21:16 
GeneralMy vote of 4 Pin
shahshi1-Feb-11 17:34
shahshi1-Feb-11 17:34 
GeneralNice! Pin
NAGG21-Jan-09 11:10
NAGG21-Jan-09 11:10 
GeneralGreat Example Pin
myrtle30317-Sep-07 7:43
myrtle30317-Sep-07 7:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.