Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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.

// 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:

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)

Share

About the Author

Francesco Carata
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralImplementation of IData PinmemberPratik.Patel27-Sep-08 16:46 
GeneralRe: Implementation of IData PinmemberHassan Abdallah9-Dec-08 14:26 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 9 Jul 2007
Article Copyright 2007 by Francesco Carata
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid