“对象创建”模式
- 通过“对象创建” 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定。它是接口抽象之后的第一步工作。
- 典型模式
- Factory Method
- Abstract Factory
- Prototype
- Builder
动机(Motivation)
- 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作。
- 如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合?
模式定义
提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。 ——《设计模式》GoF
代码示例
用先前的工厂方法模式(伪代码):
//数据库访问有关的基类
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;
};
//支持SQL Server
class SqlConnection: public IDBConnection{
};
class SqlConnectionFactory:public IDBConnectionFactory{
};
class SqlCommand: public IDBCommand{
};
class SqlCommandFactory:public IDBCommandFactory{
};
class SqlDataReader: public IDataReader{
};
class SqlDataReaderFactory:public IDataReaderFactory{
};
//支持Oracle
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()){
}
}
};
EmployeeDAO
类内放了三个工厂指针字段。
抽象工厂:
#include <iostream>
#include <vector>
using namespace std;
//数据库访问有关的基类
class IDBConnection {
public:
virtual void Connection() = 0;
};
class IDBCommand {
public:
virtual void Command() = 0;
};
class IDataReader {
public:
virtual void DataReader() = 0;
};
// 抽象工厂 基类
class IDBFactory {
public:
virtual IDBConnection* CreateDBConnection() = 0;
virtual IDBCommand* CreateDBCommand() = 0;
virtual IDataReader* CreateDataReader() = 0;
};
//支持SQL Server
class SqlConnection : public IDBConnection {
public:
virtual void Connection()
{
cout << "SQL Server" << "连接成功。" << endl;
}
};
class SqlCommand : public IDBCommand {
public:
virtual void Command()
{
cout << "SQL Server" << "执行命令成功。" << endl;
}
};
class SqlDataReader : public IDataReader {
public:
virtual void DataReader()
{
cout << "SQL Server" << "数据读取成功。" << endl;
}
};
// 抽象工厂实现
class SqlDBFactory :public IDBFactory {
public:
virtual IDBConnection* CreateDBConnection()
{
return new SqlConnection;
}
virtual IDBCommand* CreateDBCommand()
{
return new SqlCommand;
}
virtual IDataReader* CreateDataReader()
{
return new SqlDataReader;
}
};
//支持Oracle
class OracleConnection : public IDBConnection {
public:
virtual void Connection()
{
cout << "Oracle" << "连接成功。" << endl;
}
};
class OracleCommand : public IDBCommand {
public:
virtual void Command()
{
cout << "Oracle" << "执行命令成功。" << endl;
}
};
class OracleDataReader : public IDataReader {
public:
virtual void DataReader()
{
cout << "Oracle" << "数据读取成功。" << endl;
}
};
// 抽象工厂实现
class OracleDBFactory :public IDBFactory {
public:
virtual IDBConnection* CreateDBConnection()
{
return new OracleConnection;
}
virtual IDBCommand* CreateDBCommand()
{
return new OracleCommand;
}
virtual IDataReader* CreateDataReader()
{
return new OracleDataReader;
}
};
class EmployeeDAO {
IDBFactory* dbFactory;
public:
EmployeeDAO(IDBFactory *dbFactory)
{
this->dbFactory = dbFactory;
}
void /* vector<EmployeeDO> */GetEmployees()
{
IDBConnection* connection = dbFactory->CreateDBConnection();
connection->Connection();
IDBCommand* command = dbFactory->CreateDBCommand();
command->Command();
//command->SetConnection(connection); //关联性
//IDBDataReader* reader = command->ExecuteReader(); //关联性
IDataReader* reader = dbFactory->CreateDataReader(); //关联性
/*while (reader->Read()) {
}*/
do
{
reader->DataReader();
} while (false);
delete connection; connection = nullptr;
delete command; command = nullptr;
delete reader; reader = nullptr;
}
};
int main()
{
IDBFactory * factory = new OracleDBFactory;
EmployeeDAO employeeDAO(factory);
employeeDAO.GetEmployees();
delete factory;
getchar();
return 0;
}
输出:
Oracle连接成功。
Oracle执行命令成功。
Oracle数据读取成功。
类图
要点总结
- 如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的工厂完全可以。
- “系列对象”指的是在某一特定系列下的对象之间有相互依赖、或作用的关系。不同系列的对象之间不能相互依赖。
- Abstract Factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应对“新对象”的需求变动。
参考:GeekBand
标签:IDBConnection,Abstract,Factory,virtual,class,抽象,reader,command,public From: https://www.cnblogs.com/huvjie/p/16729529.html