第15章Abstract Factoryパターン

概念

  • Abstract Factoryパターンは、その具象クラスを指定せずに、関連または依存オブジェクトインターフェイスのシリーズを作成しています。

含まれている2つの役割

  • 抽象ファクトリー:それは作成された製品のすべての抽象メソッドを含める必要があります
  • コンクリート工場:特定の実装を有する製品のオブジェクトを作成し、具体的な工場
  • 要約製品:彼らは二つの異なる実装を持っている可能性があります
  • 具体的な製品

3つの利点

  • Abstract Factoryパターンは、ファクトリメソッドの変形例です。製品の一種類のみを処理する場合はない(工場モードの方法で、このタイプの製品のみユーザ、及び低い抽象工場モード、ユーザーと部門を含む製品の2種類)。

四つのC ++コードを達成するために

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

//数据库表项:User 与工厂模式无关
class User
{
public:
    void SetId(const int id)
    {
        this->_id = id;
    }
    int GetId()
    {
        return this->_id;
    }
    void SetName(const string name)
    {
        this->_name = name;
    }
    string GetName()
    {
        return this->_name;
    }
private:
    int _id;
    string _name;
};

//数据库表项 Department 与工厂模式无关
class Department
{
public:
    void SetId(const int id)
    {
        this->_id = id;
    }
    int GetId()
    {
        return this->_id;
    }
    void SetName(const string name)
    {
        this->_deptName = name;
    }
    string GetName()
    {
        return this->_deptName;
    }
private:
    int _id;
    string _deptName;
};

//抽象产品A: IUser
class IUser
{
public:
    virtual void Insert(User user) = 0;
    virtual User* GetUser(int id) = 0;
};
//具体产品A1 SqlserverUser
class SqlserverUser : public IUser
{
public:
    void Insert(User user)
    {
        cout << "在SQL server中给User表增加一条记录" << endl;
    }
    User* GetUser(int id)
    {
        cout << "在SQL server中根据ID得到User表一条记录" << endl;
        return NULL;
    }
};
//具体产品A2 AccessUser
class AccessUser : public IUser 
{
public:
    void Insert(User user)
    {
        cout << "在Access 中给User表增加一条记录" << endl;
    }
    User* GetUser(int id)
    {
        cout << "在Access 中根据ID得到User表一条记录" << endl;
        return NULL;
    }
};


//抽象产品B: IDepartment
class IDepartment
{
public:
    virtual void Insert(Department department) = 0;
    virtual Department* GetUser(int id) = 0;
};

//具体产品B1 SqlserverDepartment
class SqlserverDepartment : public IDepartment
{
public:
    void Insert(Department department)
    {
        cout << "在SQL server中给Department表增加一条记录" << endl;
    }
    Department* GetUser(int id)
    {
        cout << "在SQL server中根据ID得到Department表一条记录" << endl;
        return NULL;
    }
};
//具体产品B2 AccessDepartment
class AccessDepartment : public IDepartment
{
public:
    void Insert(Department department)
    {
        cout << "在Access 中给Department表增加一条记录" << endl;
    }
    Department* GetUser(int id)
    {
        cout << "在Access 中根据ID得到Department表一条记录" << endl;
        return NULL;
    }
};

//抽象工厂 
//抽象工厂接口,它里面应该包含所有的产品创建的抽象方法
class IFactory
{
public:
    virtual IUser* createUser() = 0;
    virtual IDepartment* createDepartment() = 0;

};

//具体工厂1,创建具有特定实现的产品对象
class SqlServerFactory : public IFactory
{
public:
    IUser* createUser()
    {
        IUser* puser;
        puser = new SqlserverUser();
        return puser;
    }
    IDepartment* createDepartment()
    {
        IDepartment* pdepartment;
        pdepartment = new SqlserverDepartment();
        return pdepartment;
    }
};

class AccessFactory : public IFactory
{
public:
    IUser* createUser()
    {
        IUser* puser;
        puser = new AccessUser();
        return puser;
    }
    IDepartment* createDepartment()
    {
        IDepartment* pdepartment;
        pdepartment = new AccessDepartment();
        return pdepartment;
    }
};

int main()
{
    User user;
    Department department;

    //只需要确定实例化哪一个数据库访问对象给factory
    IFactory* factory = new AccessFactory;

    //则此时已与具体的数据库访问解除了依赖
    IUser* iu = factory->createUser();

    iu->Insert(user);
    iu->GetUser(1);

    //则此时已与具体的数据库访问解除了依赖
    IDepartment* id = factory->createDepartment();
    id->Insert(department);
    id->GetUser(1);

    return 0;
}

Shenkaoziliao:
1「C ++での西のデザインパターン-第15章- Abstract Factoryパターン」https://blog.csdn.net/xiqingnian/article/details/41181995
チェン傑と2「西のデザインモード」

おすすめ

転載: www.cnblogs.com/Manual-Linux/p/11114073.html