第8章ファクトリメソッドパターン

概念

  • ファクトリメソッドモデルは、オブジェクトを作成するためのインタフェースを定義するので、工場の製造方法は、そのサブクラスを遅らせるためにクラスをインスタンス化するクラスのサブクラスのどのインスタンスを決定します

含まれている2つの役割

  • 抽象ファクトリー
  • コンクリート工場
  • 抽象製品
  • 具体的な製品

3つの利点

  • ファクトリメソッドは、少し、パターン、単純なファクトリパターンを向上させることができます。その意図は、実際の作業は、サブクラスに延期され、製品のオブジェクト・ファクトリ・インタフェースを作成するためにファクトリメソッドパターンを定義することです。
  • シンプルな工場のモデルと比較すると、製造品出荷時のクラスだけでなく、各クラスには、特定の製品の特定の生産工場のクラスに対応しています。再び抽出これらの具体的なファクトリクラスの共通の特徴は、抽象クラスの製品を形成し、これらの製品固有のクラスは抽象クラスの製品から継承します。
  • あなたが製品を追加する必要がある場合、それは実行します。クライアントを変更する、抽象工場の継承に具体的なファクトリクラスを追加し、特定の製品カテゴリの抽象由来製品を追加します。工場モードの工場での単純なスイッチとして変更することなく。

4つの簡単な工場VSファクトリメソッド

  • シンプル工場モードの最大の利点は、ファクトリクラスを決定するために必要なロジックが含まれていることであるクラスのインスタンス化、動的に関連するクライアントの選択基準に基づいて、クライアントのために、特定の製品の依存性を削除しますが、オープンに反して - クローズ原理

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

  • 例電卓
//工厂方法模式
//计算器的例子
#include "pch.h"
#include <iostream>
using namespace std;

//抽象产品类
class Operation
{
public:
    double GetA() const
    {
        return numberA;
    }
    double GetB() const
    {
        return numberB;
    }
    void SetA(const double number)
    {
        numberA = number;
    }
    void SetB(const double number)
    {
        numberB = number;
    }
    virtual double GetResult()
    {
        double result = 0.0;
        return result;
    }

protected:
    double numberA;
    double numberB;
};

//下面是四个具体的产品类
class OperationAdd :public Operation
{
public:
    double GetResult()
    {
        double result = 0;
        result = numberA + numberB;
        return result;
    }
};

class OperationSub :public Operation
{
public:
    double GetResult()
    {
        double result = 0;
        result = numberA - numberB;
        return result;
    }
};

class OperationMul :public Operation
{
public:
    double GetResult()
    {
        double result = 0;
        result = numberA * numberB;
        return result;
    }
};

class OperationDiv :public Operation
{
public:
    double GetResult()
    {
        double result = 0;
        if (numberB != 0)
            result = numberA / numberB;
        return result;
    }
};

//抽象工厂类
class IFactory
{
public:
    virtual Operation* createOperation()
    {
        return new Operation;
    }
};

//下面是四个具体工厂类,分别用于产生四个具体产品
class AddFactory : public IFactory
{
public:
    Operation* createOperation()
    {
        oper = new OperationAdd;
        return oper;
    }
    ~AddFactory()
    {
        if (oper != NULL)
        {
            delete oper;
            oper = NULL;
        }
    }
private:
    Operation* oper;
};

class SubFactory : public IFactory
{
public:
    Operation* createOperation()
    {
        oper = new OperationSub;
        return oper;
    }
    ~SubFactory()
    {
        if (oper != NULL)
        {
            delete oper;
            oper = NULL;
        }
    }
private:
    Operation* oper;
};

class MulFactory : public IFactory
{
public:
    Operation* createOperation()
    {
        oper = new OperationMul;
        return oper;
    }
    ~MulFactory()
    {
        if (oper != NULL)
        {
            delete oper;
            oper = NULL;
        }
    }
private:
    Operation* oper;
};

class DivFactory : public IFactory
{
public:
    Operation* createOperation()
    {
        oper = new OperationDiv;
        return oper;
    }
    ~DivFactory()
    {
        if (oper != NULL)
        {
            delete oper;
            oper = NULL;
        }
    }
private:
    Operation* oper;
};

int main()
{
    IFactory *af = NULL;
    af = new SubFactory();

    Operation* oper = af->createOperation();
    oper->SetA(50);
    oper->SetB(19);
    cout << oper->GetResult() << endl;
}
  • 雷鋒の植物の例
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

class LeiFeng
{
public:
    virtual void Sweep()
    {
        cout << "扫地" << endl;
    }
    virtual void Wash()
    {
        cout << "洗衣" << endl;
    }
    virtual void BuyRice()
    {
        cout << "买米" << endl;
    }
};

class Undergraduate : public LeiFeng
{
public:
    void Sweep()
    {
        cout << "学生-扫地" << endl;
    }
    void Wash()
    {
        cout << "学生-洗衣" << endl;
    }
    void BuyRice()
    {
        cout << "学生-买米" << endl;
    }
};

class Volunteer : public LeiFeng
{
public:
    void Sweep()
    {
        cout << "志愿者-扫地" << endl;
    }
    void Wash()
    {
        cout << "志愿者-洗衣" << endl;
    }
    void BuyRice()
    {
        cout << "志愿者-买米" << endl;
    }
};

//雷锋工厂
class IFactory
{
public:
    virtual LeiFeng* CreateLeiFeng()
    {
        oper = new LeiFeng;
        return oper;
    }
    ~IFactory()
    {
        if (oper != NULL)
        {
            delete oper;
            oper = NULL;
        }
    }
private:
    LeiFeng* oper;
};

//生成学雷锋的大学生的工厂
class UndergraduateFactory : public IFactory
{
public:
    LeiFeng* CreateLeiFeng()
    {
        return new Undergraduate;
    }
};

//生成社区志愿者的工厂
class VolunteerFactory : public IFactory
{
public:
    LeiFeng* CreateLeiFeng()
    {
        return new Volunteer;
    }
};

int main()
{
    IFactory* factory = new UndergraduateFactory;
    LeiFeng* student = factory->CreateLeiFeng();

    student->BuyRice();
    student->Sweep();
    student->Wash();
    return 0;
}

参考文献:
1、 "C ++での西のデザインパターン-第8章- Factory Methodパターン" https://blog.csdn.net/xiqingnian/article/details/40957025

おすすめ

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