5,276,801 members and growing! (15,944 online)
Email Password   helpLost your password?
Development Lifecycle » Design and Architecture » General     Intermediate

Creational Patterns: Writing Abstract Factory pattern with c#

By Francesco Carata

Writing Abstract Factory pattern with c# (real example)
C# 2.0, C#Windows, .NET, .NET 2.0, WinXP, VistaVS2005, VS, Arch, Dev, Design

Posted: 8 Jul 2007
Updated: 8 Jul 2007
Views: 5,658
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.01 Rating: 2.89 out of 5
1 vote, 9.1%
1
3 votes, 27.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
7 votes, 63.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This article shows how to implement the creational pattern Abstract Factory 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 is design pattern 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 implements the Abstract Item interface.

Client, uses interfaces declared by Abstract Factory and Abstract Item classes.

Let's write the code of Abstract Factory

This example shows how to create differents datasource 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 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!

About Francesco Carata

.NET Software Developer for BeyondTrust Corporation.

I live in Turin, Italy.

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

About the Author

Francesco Carata



Occupation: Web Developer
Location: Italy Italy

Other popular Design and Architecture articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Jul 2007
Editor:
Copyright 2007 by Francesco Carata
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project