一。簡單的類圖
二。抽象工場文件
using
System;
namespace DBFactory.AbstractFactory
{
/**//// <summary>抽象工場角色類</summary>
public abstract class DBTypeFactory
{
public DBTypeFactory(){}
public abstract DBFactory GetDB();
}
/**//// <summary>具體工場角色類</summary>
public class SqlDataBase : DBTypeFactory
{
public SqlDataBase(){}
public override DBFactory GetDB()
{
return new SqlFactory();
}
}
/**//// <summary>具體工場角色類</summary>
public class AccessDataBase : DBTypeFactory
{
public AccessDataBase(){}
public override DBFactory GetDB()
{
return new AccessFactory();
}
}
}
namespace DBFactory.AbstractFactory
{
/**//// <summary>抽象工場角色類</summary>
public abstract class DBTypeFactory
{
public DBTypeFactory(){}
public abstract DBFactory GetDB();
}
/**//// <summary>具體工場角色類</summary>
public class SqlDataBase : DBTypeFactory
{
public SqlDataBase(){}
public override DBFactory GetDB()
{
return new SqlFactory();
}
}
/**//// <summary>具體工場角色類</summary>
public class AccessDataBase : DBTypeFactory
{
public AccessDataBase(){}
public override DBFactory GetDB()
{
return new AccessFactory();
}
}
}
三。抽象產品文件
using
System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
namespace DBFactory.AbstractFactory
{
/**//// <summary>抽象產品類</summary>
public abstract class DBFactory
{
public DBFactory(){}
private string connectionString;
/**//// <summary>數據庫連接字符串</summary>
public string ConnectionString
{
get
{
if(connectionString==null) connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
return connectionString;
}
set{connectionString = value;}
}
public abstract DataSet DataSetExecuteSql(string sqlString);
public abstract IDataReader DataReaderExecuteSql(string sqlString);
}
/**//// <summary>具體產品類</summary>
public class SqlFactory : DBFactory
{
public SqlFactory(){}
public override IDataReader DataReaderExecuteSql(string sqlString)
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlString,conn);
return comm.ExecuteReader();
}
public override DataSet DataSetExecuteSql(string sqlString)
{
DataSet ds = new DataSet();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlString,conn);
da.Fill(ds);
return ds;
}
}
/**//// <summary>具體產品類</summary>
public class AccessFactory : DBFactory
{
public AccessFactory(){}
public override IDataReader DataReaderExecuteSql(string sqlString)
{
OleDbConnection conn = new OleDbConnection(ConnectionString);
conn.Open();
OleDbCommand comm = new OleDbCommand(sqlString,conn);
return comm.ExecuteReader();
}
public override DataSet DataSetExecuteSql(string sqlString)
{
DataSet ds = new DataSet();
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbDataAdapter da = new OleDbDataAdapter(sqlString,conn);
da.Fill(ds);
return ds;
}
}
}
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
namespace DBFactory.AbstractFactory
{
/**//// <summary>抽象產品類</summary>
public abstract class DBFactory
{
public DBFactory(){}
private string connectionString;
/**//// <summary>數據庫連接字符串</summary>
public string ConnectionString
{
get
{
if(connectionString==null) connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
return connectionString;
}
set{connectionString = value;}
}
public abstract DataSet DataSetExecuteSql(string sqlString);
public abstract IDataReader DataReaderExecuteSql(string sqlString);
}
/**//// <summary>具體產品類</summary>
public class SqlFactory : DBFactory
{
public SqlFactory(){}
public override IDataReader DataReaderExecuteSql(string sqlString)
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
conn.Open();
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlString,conn);
return comm.ExecuteReader();
}
public override DataSet DataSetExecuteSql(string sqlString)
{
DataSet ds = new DataSet();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlString,conn);
da.Fill(ds);
return ds;
}
}
/**//// <summary>具體產品類</summary>
public class AccessFactory : DBFactory
{
public AccessFactory(){}
public override IDataReader DataReaderExecuteSql(string sqlString)
{
OleDbConnection conn = new OleDbConnection(ConnectionString);
conn.Open();
OleDbCommand comm = new OleDbCommand(sqlString,conn);
return comm.ExecuteReader();
}
public override DataSet DataSetExecuteSql(string sqlString)
{
DataSet ds = new DataSet();
OleDbConnection conn = new OleDbConnection(ConnectionString);
OleDbDataAdapter da = new OleDbDataAdapter(sqlString,conn);
da.Fill(ds);
return ds;
}
}
}
以上就實現了一個簡單的采用抽象工場模式來實現數據庫操作的類,下面看看我們是如何調用的:
string
connString
=
"
data source=192.168.1.9;initial catalog=sqldll;persist security info=False;user id=sa;password=123456;workstation id=Server;packet size=4096
"
;
string commString = " select * from tbl_Vip " ;
DBTypeFactory dbtypefactory = new SqlDataBase(); // AccessDataBase();
DBFactory.AbstractFactory.DBFactory dbfactory = dbtypefactory.GetDB();
dbfactory.ConnectionString = connString;
DataGrid1.DataSource = dbfactory.DataSetExecuteSql(commString);
DataGrid1.DataBind();
string commString = " select * from tbl_Vip " ;
DBTypeFactory dbtypefactory = new SqlDataBase(); // AccessDataBase();
DBFactory.AbstractFactory.DBFactory dbfactory = dbtypefactory.GetDB();
dbfactory.ConnectionString = connString;
DataGrid1.DataSource = dbfactory.DataSetExecuteSql(commString);
DataGrid1.DataBind();
是不是與以上寫的關於工場方法模式實現數據庫操作的類相比更加簡單些:)
四.工場方法模式與抽象工場模式
从以上我们可以得出工场方法模式与抽象工场模式的区别,以及何时该采用工场方法模式何时该采用抽象工场模式来设计.
工场方法模式:用简单明了的话来说就是如一个生产镜片的工厂生产树脂镜片与玻璃镜片,那么何时该生产树脂镜片,何时该生产玻璃镜片呢。这就是工厂方法模式的实现.
抽象工场模式:是比工厂方法模式更加抽象的概念,如一个生成眼镜的工厂它相对于眼镜的品牌来讲就是一个抽象工场的模式。