目录
- 1.动机
- 2.模式定义
- 3.要点总结
- 4.代码感受
- 1.代码一
- 2.代码二 -- 工厂方法
- 3.代码三 -- 抽象工厂
1.动机
- 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作
- 如何应对这种变化?如何绕过常规的对象创建方法(new),提供一 种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合?
2.模式定义
- 定义:提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类
- 结构
3.要点总结
- 如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的工厂完全可以
- “系列对象”指的是在某一特定系列下的对象之间有相互依赖、 或作用的关系。不同系列的对象之间不能相互依赖
- Abstract Factory模式主要在于应对“新系列”的需求变动
4.代码感受
1.代码一
class EmployeeDAO
{
public:
vector<EmployeeDO> GetEmployees()
{
SqlConnection* connection = new SqlConnection();
connection->ConnectionString = "...";
SqlCommand* command = new SqlCommand();
command->CommandText = "...";
command->SetConnection(connection);
SqlDataReader* reader = command->ExecuteReader();
while (reader->Read())
{}
}
};
2.代码二 – 工厂方法
class IDBConnection
{};
class IDBConnectionFactory
{
public:
virtual IDBConnection* CreateDBConnection() = 0;
};
class IDBCommand
{};
class IDBCommandFactory
{
public:
virtual IDBCommand* CreateDBCommand() = 0;
};
class IDataReader
{};
class IDataReaderFactory
{
public:
virtual IDataReader* CreateDataReader() = 0;
};
class SqlConnection: public IDBConnection
{};
class SqlConnectionFactory:public IDBConnectionFactory
{};
class SqlCommand: public IDBCommand
{};
class SqlCommandFactory:public IDBCommandFactory
{};
class SqlDataReader: public IDataReader
{};
class SqlDataReaderFactory:public IDataReaderFactory
{};
class OracleConnection: public IDBConnection
{};
class OracleCommand: public IDBCommand
{};
class OracleDataReader: public IDataReader
{};
class EmployeeDAO
{
IDBConnectionFactory* dbConnectionFactory;
IDBCommandFactory* dbCommandFactory;
IDataReaderFactory* dataReaderFactory;
public:
vector<EmployeeDO> GetEmployees()
{
IDBConnection* connection =
dbConnectionFactory->CreateDBConnection();
connection->ConnectionString("...");
IDBCommand* command =
dbCommandFactory->CreateDBCommand();
command->CommandText("...");
command->SetConnection(connection);
IDBDataReader* reader = command->ExecuteReader();
while (reader->Read())
{}
}
};
3.代码三 – 抽象工厂
class IDBConnection
{};
class IDBCommand
{};
class IDataReader
{};
class IDBFactory
{
public:
virtual IDBConnection* CreateDBConnection() = 0;
virtual IDBCommand* CreateDBCommand() = 0;
virtual IDataReader* CreateDataReader() = 0;
};
class SqlConnection: public IDBConnection
{};
class SqlCommand: public IDBCommand
{};
class SqlDataReader: public IDataReader
{};
class SqlDBFactory:public IDBFactory
{
public:
virtual IDBConnection* CreateDBConnection()
{}
virtual IDBCommand* CreateDBCommand()
{}
virtual IDataReader* CreateDataReader()
{}
};
class OracleConnection: public IDBConnection
{};
class OracleCommand: public IDBCommand
{};
class OracleDataReader: public IDataReader
{};
class EmployeeDAO
{
IDBFactory* dbFactory;
public:
vector<EmployeeDO> GetEmployees()
{
IDBConnection* connection =
dbFactory->CreateDBConnection();
connection->ConnectionString("...");
IDBCommand* command =
dbFactory->CreateDBCommand();
command->CommandText("...");
command->SetConnection(connection);
IDBDataReader* reader = command->ExecuteReader();
while (reader->Read())
{}
}
};