玩命加载中 . . .

9-抽象模式


抽象工厂

数据访问层需要创建一系列的数据库对象,这些对象之间有依赖性

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()) {

        }

    }
};

这里也不希望跟具体类绑定,因为后面可能数据库会变化

所以还是需要用工厂指针,每个类型都需要一个工厂,然后在EmployeeDAO中组合三个工厂指针

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

class IDBCommand{
};

class IDataReader{
};

class IDBConnectionFactory{
public:
    virtual IDBConnection* CreateDBConnection()=0;
};

class IDBCommandFactory{
public:
    virtual IDBCommand* CreateDBCommand()=0;
};

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

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里面的三个指针可以提到一个抽象工厂基类IDBFactory中,然后派生出特定版本的SqlDBFactory

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

class SqlDBFactory:public IDBFactory{
public:
    IDBConnection* CreateDBConnection() {
        return new SqlConnection();
    }
    IDBCommand* CreateDBCommand() {
        return new SqlCommand();
    }
    IDataReader* CreateDataReader() {
        return new SqlDataReader();
    }
 
};

class EmployeeDAO{
    IDBFactory* dbFactory;
    
public:
    vector<EmployeeDAO> GetEmployees(){
        IDBConnection* connection = dbFactory->CreateDBConnection();
        connection->ConnectionString("...");

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

        IDBDataReader* reader = command->ExecuteReader(); //关联性
        while (reader->Read()){

        }

    }
};

模式定义

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

  • 如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的工广完全可以
  • “系列对象”指的是在某一特定系列下的对象之间有相互依赖、或作用的关系。不同系列的对象之间不能相互依赖
  • Abstract Factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应对“新对象”的需求变动

简单版本

class B1 {
public:
    virtual void fun1() = 0;
};

class D1 : public B1 {
public:
    void fun1() { cout << "D1::fun1" << endl; }
};

class B2 {
public:
    virtual void fun2() = 0;
};

class D2 : public B2 {
public:
    void fun2() { cout << "D2::fun2" << endl; }
};

class Factory {
public:
    virtual B1* create1() = 0;
    virtual B2* create2() = 0;
};

class DFactory : public Factory {
public:
    B1* create1() {
        return new D1();
    }
    B2* create2() {
        return new D2();
    }
};

int main(int argc, char const *argv[])
{
    DFactory *df = new DFactory();  // 创建工厂
    B1 *pb1 = df->create1();    // 可以生产D1
    pb1->fun1();                // D1::fun1
    B2 *pb2 = df->create2();    // 也可以生产D2
    pb2->fun2();                // D2::fun2
    return 0;
}

文章作者: kunpeng
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 kunpeng !
  目录