C++ Design Pattern Factory Class Design Pattern

Simple Factory Pattern

describe:

The logical part of the system is separated from the functional part, and the operation of creating objects is in charge of the factory class.

shortcoming:

  1. The number of system classes is increased, which increases the complexity of the system to a certain extent .

  1. Poor scalability . Once a new product is added, the factory logic needs to be modified, which is not conducive to system expansion and maintenance. And it violates the open-closed principle .

the code

#include <iostream>
#include <string>
using namespace std;
//基类
class Operation {
public :
    double A, B;
    virtual double getResult() {
        return 0;
    }
};
//加
class addOperation : public Operation {
    double getResult() {
        return A + B;
    }
};
 
//减
class subOperation : public Operation {
    double getResult() {
        return A - B;
    }
};
//乘
class mulOperation : public Operation {
    double getResult() {
        return A * B;
    }
};
//除
class divOperation : public Operation {
    double getResult() {
        return A / B;
    }
};
//工厂类
class operFactory {
public :
    static Operation *createOperation(char c) {
        switch (c) {
        case '+' :
            return new addOperation;
            break;
 
        case '-' :
            return new subOperation;
            break;
 
        case '*' :
            return new mulOperation;
            break;
 
        case '/' :
            return new divOperation;
            break;
        }
    }
};
 
int main() {
    Operation *oper = operFactory::createOperation('+');
    oper->A = 12;
    oper->B = 13;
    cout << oper->getResult() << endl;
    delete oper;
    return 0;
}

factory method pattern

Description: Define an interface for creating objects, let subclasses decide which class to instantiate, and the factory method pattern delays the instantiation of a class ( purpose: decoupling; means: virtual function ) to subclasses.

Disadvantages: 1. Increased complexity. 2. All objects are required to have the same creation method/parameters

#include <iostream>
#include <string>
using namespace std;
 
class Operation {
public:
    double A, B;
    virtual double getResult() {
        return 0;
    }
};
//加
class addOperation : public Operation {
    double getResult() {
        return A + B;
    }
};
 
//减
class subOperation : public Operation {
    double getResult() {
        return A - B;
    }
};
//乘
class mulOperation : public Operation {
    double getResult() {
        return A * B;
    }
};
//除
class divOperation : public Operation {
    double getResult() {
        return A / B;
    }
};
 
class IFactory {
public:
    virtual Operation *createOperation() = 0;
};
 
class AddFactory : public IFactory {
public:
    Operation *createOperation() {
        return new addOperation();
    }
};
 
class SubFactory : public IFactory {
public:
    Operation *createOperation() {
        return new subOperation();
    }
};
 
class MulFactory : public IFactory {
public:
    Operation *createOperation() {
        return new mulOperation();
    }
};
 
class DivFactory : public IFactory {
public:
    Operation *createOperation() {
        return new divOperation();
    }
};
 
class Work {
private:
    IFactory* factory;
public:
    Work(IFactory* factory) {
        this->factory = factory;
    }
    virtual ~Work() {
    }
    void calculate(int a, int b) {
        Operation* oper = factory->createOperation();
        oper->A = a;
        oper->B = b;
        cout << oper->getResult() << endl;
        delete oper;
    }
};
 
int main() {
    AddFactory *factory = new AddFactory();
    Work *work = new Work(factory);
    work->calculate(12, 13);
    delete work;
    return 0;
}

Guess you like

Origin blog.csdn.net/cyy1104/article/details/129619103