Today, I am not working much. I looked at the "Abstract Factory" mode.

Source: Internet
Author: User

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;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.