![]() |
Development Lifecycle »
Design and Architecture »
General
Intermediate
Creational Patterns: Writing Abstract Factory pattern with c#By Francesco CarataWriting Abstract Factory pattern with c# (real example) |
C# 2.0.NET 2.0, WinXP, VistaVS2005, Architect, Dev, Design
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article shows how to implement the creational pattern Abstract Factory with c# 2.0 and .NET in a real case.
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.
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 implements the Abstract Item interface.
Client, uses interfaces declared by Abstract Factory and Abstract Item classes.
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
}
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!
.NET Software Developer for BeyondTrust Corporation.
I live in Turin, Italy.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Jul 2007 Editor: |
Copyright 2007 by Francesco Carata Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |