Skip to content

Latest commit

 

History

History
167 lines (132 loc) · 3.52 KB

07.抽象工厂模式.md

File metadata and controls

167 lines (132 loc) · 3.52 KB

抽象工厂模式 Abstract Factory

动机

一系列相互依赖的对象的创建工作

模式定义

提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。

代码

// 数据访问层
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()){
            
        }
    }
};

原始设计

// 数据访问有关的基类
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); // 相关性

        IDataReader* reader = command->ExecuteReader();
        while(reader->Read()){
            
        }
    }
};

重构设计

// 数据访问有关的基类
class IDBConnection{
};
class IDBCommand{
};
class IDataReader{
};

class IDBFactory{
public:
    virtual IDBConnection* CreateDBConnection() = 0;
    virtual IDBCommand* CreateDBCommand() = 0;
    virtual IDataReader* CreateDataReader() = 0;
};

// 支持 Sql Server
class SqlConnection : public IDBConnection{
};
class SqlCommand : public IDBCommand{
};
class SqlDataReader : public IDataReader{
};

class SqlDBFactory : public IDBFactory{

};

// 支持 Oracle
class OracleConnection : public IDBConnection{
};
class OracleCommand : public IDBCommand{
};
class OracleDataReader : public IDataReader{
};

class OracleDBFactory : public IDBFactory{

};

// 数据访问层
class EmployeeDAO{
    IDBFactory* dbFactory;
public:
    vector<EmployeeDO> GetEmployees(){
        IDBConnection* connection = dbFactory->CreateDBConnection();
        connection->ConnectionString("...");

        IDBCommand* command = dbFactory->CreateDBCommand();
        command->CommandText("...");
        command->SetConnection(connection); // 关联性

        IDataReader* reader = command->ExecuteReader();
        while(reader->Read()){
            
        }
    }
};

题目