After reading the article, follow your own understanding. I made a DEMO to record my understanding process. There may be something wrong. You are welcome to raise it.
// 1 Abstract Factory
Abstract class Abstract database object
{
Abstract Sqlserver2k database object CreateSqlserver2k ();
Abstract Oracle9i Database Object CreateOracle9i ();
Abstract OleDb database object CreateOleDb ();
Abstract ODBC database object CreateODBC ();
}
// 2 specific factory
Class Sqlserver2k database object: Abstract Database Object
{
Override public synchronized actdatabase CreateSqlserver2k ()
{
Return new Sqlserevr2kHelp ();
}
}
Class Oracle9i database object: Abstract Database Object
{
Override public synchronized actdatabase CreateOracle9i
{
Return new Oracle9iHelp ();
}
}
Class OleDb database object: Abstract Database Object
{
Override public synchronized actdatabase CreateOleDb
{
Return new OleDbHelp ();
}
}
Class Odbc database object: Abstract Database Object
{
Override public synchronized actdatabase CreateOdbc
{
Return new OdbcHelp ();
}
}
// 3 abstract Product
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)
//..
}
// Specific product
Class Sqlserevr2kHelp: AbstractDataBase
{
// Rewrite all methods in the base class based on Sqlserver2k features.
}
Class Oracle9iHelp: AbstractDataBase
{
// Rewrite all methods in the base class based on the features of Oracle9.
}
Class OleDbHelp: AbstractDataBase
{
// Rewrite all methods in the base class based on OleDb features.
}
Class OdbcHelp: AbstractDataBase
{
// Rewrite all methods in the base class based on Odbc features.
}
Enum DataBaseType
{
Sqlserevr2kHelp,
Oracle9iHelp,
OleDbHelp,
OdbcHelp
}
Class Environment
{
Private AbstractDataBase implements actdatabase;
Public Environment (Abstract Database Object 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;
}
}