看完了文章,根据自己的理解。做了一个DEMO,记录自己的理解过程。可能有不对的,欢迎看官提出。
//1 抽象工厂
abstract class Abstract数据库对象
{
abstract Sqlserver2k数据库对象 CreateSqlserver2k();
abstract Oracle9i数据库对象 CreateOracle9i();
abstract OleDb数据库对象 CreateOleDb();
abstract ODBC数据库对象 CreateODBC();
}
//2 具体工厂
class Sqlserver2k数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateSqlserver2k()
{
return new Sqlserevr2kHelp();
}
}
class Oracle9i数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateOracle9i
{
return new Oracle9iHelp();
}
}
class OleDb数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateOleDb
{
return new OleDbHelp();
}
}
class Odbc数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateOdbc
{
return new OdbcHelp();
}
}
//3 抽象产品
abstract class AbstractDataBase
{
abstract public IDbConnection GetConnection(string strConnectionString);
abstract public bool OpenConnection(IDbConnection connection);
abstract public bool CloseConnection(IDbConnection connection);
abstract public int ExecuteNonQuery(CommandType commandType, string commandText);
abstract public DataSet ExecuteDataSet(CommandType commandType, string commandText);
abstract public object ExecuteScalar(CommandType commandType, string commandText);
abstract public IDataReader ExecuteReader(CommandType commandType, string commandText);
abstract public bool UpdateDataSet(DataSet dataSet,string tableName);
abstract public IDbTransaction BeginTransaction(IDbConnection connection);
abstract public void RollbackTransaction(IDbTransaction tran);
abstract public void CommitTransaction(IDbTransaction tran)
//..
}
//具体产品
class Sqlserevr2kHelp:AbstractDataBase
{
//根据Sqlserver2k的特性,重写基类中的所有方法。
}
class Oracle9iHelp:AbstractDataBase
{
//根据Oracle9的特性,重写基类中的所有方法。
}
class OleDbHelp:AbstractDataBase
{
//根据OleDb的特性,重写基类中的所有方法。
}
class OdbcHelp:AbstractDataBase
{
//根据Odbc的特性,重写基类中的所有方法。
}
enum DataBaseType
{
Sqlserevr2kHelp,
Oracle9iHelp,
OleDbHelp,
OdbcHelp
}
class Environment
{
private AbstractDataBase abstractDataBase;
public Environment(Abstract数据库对象 demo, DataBaseType dbtype)
{
if(dbtype == DataBaseType.Sqlserevr2kHelp)
{
abstractDataBase = demo.CreateSqlserver2k();
}
else if(dbtype == DataBaseType.Oracle9iHelp)
{
abstractDataBase = demo.CreateOracle9i();
}
else if(dbtype == DataBaseType.OleDbHelp)
{
abstractDataBase = demo.CreateOleDb();
}
else if(dbtype == DataBaseType.OdbcHelp)
{
abstractDataBase = demo.CreateOdbc();
}
}
public AbstractDataBase RunObject()
{
return abstractDataBase;
}
}
abstract class Abstract数据库对象
{
abstract Sqlserver2k数据库对象 CreateSqlserver2k();
abstract Oracle9i数据库对象 CreateOracle9i();
abstract OleDb数据库对象 CreateOleDb();
abstract ODBC数据库对象 CreateODBC();
}
//2 具体工厂
class Sqlserver2k数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateSqlserver2k()
{
return new Sqlserevr2kHelp();
}
}
class Oracle9i数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateOracle9i
{
return new Oracle9iHelp();
}
}
class OleDb数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateOleDb
{
return new OleDbHelp();
}
}
class Odbc数据库对象:Abstract数据库对象
{
override public AbstractDataBase CreateOdbc
{
return new OdbcHelp();
}
}
//3 抽象产品
abstract class AbstractDataBase
{
abstract public IDbConnection GetConnection(string strConnectionString);
abstract public bool OpenConnection(IDbConnection connection);
abstract public bool CloseConnection(IDbConnection connection);
abstract public int ExecuteNonQuery(CommandType commandType, string commandText);
abstract public DataSet ExecuteDataSet(CommandType commandType, string commandText);
abstract public object ExecuteScalar(CommandType commandType, string commandText);
abstract public IDataReader ExecuteReader(CommandType commandType, string commandText);
abstract public bool UpdateDataSet(DataSet dataSet,string tableName);
abstract public IDbTransaction BeginTransaction(IDbConnection connection);
abstract public void RollbackTransaction(IDbTransaction tran);
abstract public void CommitTransaction(IDbTransaction tran)
//..
}
//具体产品
class Sqlserevr2kHelp:AbstractDataBase
{
//根据Sqlserver2k的特性,重写基类中的所有方法。
}
class Oracle9iHelp:AbstractDataBase
{
//根据Oracle9的特性,重写基类中的所有方法。
}
class OleDbHelp:AbstractDataBase
{
//根据OleDb的特性,重写基类中的所有方法。
}
class OdbcHelp:AbstractDataBase
{
//根据Odbc的特性,重写基类中的所有方法。
}
enum DataBaseType
{
Sqlserevr2kHelp,
Oracle9iHelp,
OleDbHelp,
OdbcHelp
}
class Environment
{
private AbstractDataBase abstractDataBase;
public Environment(Abstract数据库对象 demo, DataBaseType dbtype)
{
if(dbtype == DataBaseType.Sqlserevr2kHelp)
{
abstractDataBase = demo.CreateSqlserver2k();
}
else if(dbtype == DataBaseType.Oracle9iHelp)
{
abstractDataBase = demo.CreateOracle9i();
}
else if(dbtype == DataBaseType.OleDbHelp)
{
abstractDataBase = demo.CreateOleDb();
}
else if(dbtype == DataBaseType.OdbcHelp)
{
abstractDataBase = demo.CreateOdbc();
}
}
public AbstractDataBase RunObject()
{
return abstractDataBase;
}
}