工厂模式 ---- 简单工厂模式和抽象工厂模式

简单工厂模式

/*
 * 简单工厂模式
 *
 * 优点:
帮助封装
实现组件封装,面向接口编程
解耦合
客户端和具体实现类的解耦合

缺点:
可能增加客户端的复杂度
不方便扩展子工厂
 */


#include <iostream>
#include <string>
using namespace std;

//产品抽象父类
class Operation{
public:
    double a;
    double b;

    virtual double calculate_a_b() = 0;

};

class AddOperation:public Operation{
    double calculate_a_b(){
        return a + b;
    }
};

class SubOperation:public Operation{
    double calculate_a_b(){
        return a - b;
    }
};

class DivOperation:public Operation{
    double calculate_a_b(){
        return a / b;
    }
};

class MulOperation:public Operation{
    double calculate_a_b(){
        return a * b;
    }
};

//产品工厂类
class OperationFactory{
public:
    static Operation* createOperation(char c){
        switch (c) {
            case '+':
                return new AddOperation;
            case '-':
                return new SubOperation;
            case '/':
                return new DivOperation;
            case '*':
                return new MulOperation;
            default:
                return nullptr;
        }
    }
};

int main(){
    Operation* operation = OperationFactory::createOperation('+');

    if(operation != nullptr){
        operation->a = 1.5;
        operation->b = 2.5;

        double result = operation->calculate_a_b();

        cout << result << endl;
    }
    else{
        cout << "nullptr" << endl;
    }


}


抽象工厂模式

/*抽象工厂模式

优点:
抽象工厂封装了变化,封装了对象创建的具体细节
增加新的产品族很方便,无须修改已有系统
针对接口进行编程而不是针对具体进行编程

缺点:
增加新的产品等级结构需对原系统做较大修改(违背开放封闭)

 */

#include <iostream>
#include <string>
using namespace std;

//抽象产品
class Product{
public:
    virtual void show() = 0;
};

//A产品
class A_Product:public Product{};
//B产品
class B_Product:public Product{};

//M公司的A产品
class M_A_Product: public A_Product{
    void show(){
        cout << "M Factory A Product" << endl;
    }
};
//M公司的B产品
class M_B_Product:public B_Product{
    void show(){
        cout << "M Factory B Product" << endl;
    }
};

//N公司的A产品
class N_A_Product:public A_Product{
    void show(){
        cout << "N Factory A Product" << endl;
    }
};
//N公司的B产品
class N_B_Product:public B_Product{
    void show(){
        cout << "N Factory B Product" << endl;
    }
};

//抽象工厂
class Factory{
public:
    virtual A_Product* create_A() = 0;
    virtual B_Product* create_B() = 0;
};

//M 工厂
class M_Factory:public Factory{
    A_Product* create_A(){
        return new M_A_Product;
    }

    B_Product* create_B(){
        return new M_B_Product;
    }
};


//N 工厂
class N_Factory:public Factory{
    A_Product* create_A(){
        return new N_A_Product;
    }

    B_Product* create_B(){
        return new N_B_Product;
    }
};

int main(){
    Factory* factory_M = new M_Factory;

    A_Product* m_a_product = factory_M->create_A();
    B_Product* m_b_product = factory_M->create_B();

    m_a_product->show();
    m_b_product->show();

    delete factory_M;
    delete m_a_product;
    delete m_b_product;



    Factory* factory_N = new N_Factory;

    A_Product* n_a_product = factory_N->create_A();
    B_Product* n_b_product = factory_N->create_B();

    n_a_product->show();
    n_b_product->show();

    delete factory_N;
    delete n_a_product;
    delete n_b_product;

}
posted @ 2023-12-08 19:22  水三丫  阅读(9)  评论(0编辑  收藏  举报