Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#

Creational Patterns: Implementing the Abstract Factory Pattern in C#

Rate me:
Please Sign up or sign in to vote.
3.63/5 (24 votes)
8 Jul 2007CPOL1 min read 75.8K   905   39   6
Writing the Abstract Factory pattern with C# (real example).

Introduction

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.

Purpose

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

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.

Let's write the code for the Abstract Factory

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

C#
// The Abstract Factory interface. 
Public interface IDataFactory 
{ 
     IData CreateData(); 
}

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

// The Concrete Factory #2 class.
public class DataFromFile: IDataFactory
{
     public IData CreateData()
     {
          // IMPLEMENT YOUR LOGIC
          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();
                    break;
               case DataType.DB:
                    factory = new DataFromDB();
                    _data = factory.CreateData();
                    break;
           }  
      }

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

//The DataType enumeration.
public enum DataType
{
     File,
     DB
}

How to use the Abstract Factory

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

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

History

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!

License

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


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
AbdulMuheet25-Apr-12 0:03
AbdulMuheet25-Apr-12 0:03 
GeneralMy vote of 3 Pin
codenit31-Jan-12 2:34
codenit31-Jan-12 2:34 
GeneralGreat Artical Pin
raananv19-Apr-11 5:16
raananv19-Apr-11 5:16 
GeneralNice article Pin
Su_shamim3-Feb-10 18:01
Su_shamim3-Feb-10 18:01 
GeneralImplementation of IData Pin
Pratik.Patel27-Sep-08 16:46
Pratik.Patel27-Sep-08 16:46 
GeneralRe: Implementation of IData Pin
Hassan Abdallah9-Dec-08 14:26
Hassan Abdallah9-Dec-08 14:26 
sure u can, each method or property will return IData can return different object, with different implementation.

Regards,
Hassan

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.