Chapter 8 Factory Method pattern

A concept

  • Factory method model, define an interface for creating an object, so decide which instance of a subclass of class, method of making factory to instantiate a class to delay its subclasses

Two roles included

  • Abstract Factory
  • Concrete factory
  • Abstract product
  • Specific products

Three advantages

  • Factory method is slightly improved pattern simple factory pattern. The intention is to define the factory method pattern to create a product object factory interface, the actual work will be deferred to subclasses.
  • Compared with simple factory model, manufacturing products not only a factory class, but each class corresponds to a specific product its specific production factory class. The common feature of these concrete factory class again extracted form an abstract class products, these product-specific classes inherit from the abstract class products.
  • When you need to add a product, do it is: add a product derived from the abstract of specific product categories, add a concrete factory class in the inheritance of the abstract factory, change the client. Without the need to change as a simple switch in the plant in the factory mode.

Four simple factory VS factory method

  • Simple factory mode greatest advantage is that factory class contains the necessary logic to determine, based on the class instantiate dynamically associated client selection criteria, for the client, removes the dependence of the specific products, but contrary to the Open - Closed Principle

Four C ++ code to achieve

  • Examples calculator
//工厂方法模式
//计算器的例子
#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;
}
  • Examples of Lei Feng plant
#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;
}

References:
1, "Westward Design Patterns in C ++ - Chapter 8 - factory method pattern" https://blog.csdn.net/xiqingnian/article/details/40957025

Guess you like

Origin www.cnblogs.com/Manual-Linux/p/11112449.html