Click here to Skip to main content
13,301,195 members (81,749 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 8 Jul 2007

Creational Patterns: Implementing the Abstract Factory Pattern in C#

, 8 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Writing the Abstract Factory pattern with C# (real example).


This article shows how to implement the creational Abstract Factory pattern with C# 2.0 and .NET in a real case.

What's a Design Pattern?

A Design Pattern is a general repeatable solution to a commonly occurring problem in software design. A Design Pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

What's a Creational Design Pattern

Creational Design Patterns are Design Patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational Design Patterns solve this problem by somehow controlling this object creation.


The Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.


  • 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.

Let's write the code for the Abstract Factory

This example shows how to create different datasources using the Abstract Factory pattern.

// The Abstract Factory interface. 
Public interface IDataFactory 
     IData CreateData(); 

// The Concrete Factory #1 class.
public class DataFromDB: IDataFactory
     public IData CreateData()
          return new Data("DataBase source");

// The Concrete Factory #2 class.
public class DataFromFile: IDataFactory
     public IData CreateData()
          return new Data("File source");

// The Abstract Item class.
public interface IData
     string GetData();

// The Item class.
public class Data: IData 
     private string _value;

     public Data(string value)
          _value = value;
     public string GetData()
          return _value;

// The Client class.
public class DataSource
     private IData _data;
     public DataSource(DataType dataType)
          IDataFactory factory;
          switch (dataType)
               case DataType.File:
                    factory = new DataFromFile();
                    _data = factory.CreateData();
               case DataType.DB:
                    factory = new DataFromDB();
                    _data = factory.CreateData();

      public string GetDataFromSource()
           return _data.GetData();

//The DataType enumeration.
public enum DataType

How to use the Abstract Factory

The following code shows how to call the Abstract Factory from an application client:

DataSource ds = new DataSource(DataType.DB);
//DataSource ds = new DataSource(DataType.File);
string result = ds.GetDataFromSource();


This is the first iteration of this code. Please provide any feedback as to whether you have used this or not, or any problems that anyone has found with it!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Francesco Carata
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
AbdulMuheet25-Apr-12 1:03
memberAbdulMuheet25-Apr-12 1:03 
GeneralMy vote of 3 Pin
codenit31-Jan-12 3:34
membercodenit31-Jan-12 3:34 
GeneralGreat Artical Pin
raananv19-Apr-11 6:16
memberraananv19-Apr-11 6:16 
GeneralNice article Pin
Su_shamim3-Feb-10 19:01
memberSu_shamim3-Feb-10 19:01 
GeneralImplementation of IData Pin
Pratik.Patel27-Sep-08 17:46
memberPratik.Patel27-Sep-08 17:46 
GeneralRe: Implementation of IData Pin
Hassan Abdallah9-Dec-08 15:26
memberHassan Abdallah9-Dec-08 15:26 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by Francesco Carata
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid