解释工厂模式

参考文献:C++几种工厂模式和实现实例_工厂方法模式c++实例-CSDN博客

什么是工厂模式

工厂模式是一种创建对象的设计模式,它提供了一种创建对象的方式,将对象的创建和使用分离

通过工厂模式,可以根据不同的条件创建不同类型的对象,而不需要在客户端代码中显示地指出具体地对象类型

简单工厂

特点:简单工厂模式定义了一个工厂类,通过工厂类的静态方法来创建不同的对象

优点:使用者只需要知道工厂类的名称和参数,不需要了解具体的构造过程

缺点:增加新产品时需要修改工厂类,违反了开闭原则(对扩展开放,对修改关闭)

cpp 复制代码
#include<iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0;
};
class Cricle :public Shape {
public:
    void draw() {
        cout << "draw cricle" << endl;
    }
};
class Rectangle :public Shape {
public:
    void draw() {
        cout << "draw rectangle" << endl;
    }
};
class ShapeFactory {
public:
    static Shape* createFactory(string shapeType) {
        if (shapeType == "Cricle") {
            return new Cricle();
        }
        else if (shapeType == "Rectangle") {
            return new Rectangle();
        }
        return NULL;
    }
};
int main() {
    Shape* s=ShapeFactory::createFactory("Cricle");
    s->draw();
    s = ShapeFactory::createFactory("Rectangle");
    s->draw();
    return 0;
    
}

工厂方法模式

特点:工厂方法模式将对象的创建延迟到子类中,每一个具体产品对应一个具体工厂

优点:增加新产品时,只需增加一个具体工厂类,不需要修改工厂类,符合开闭原则

缺点:类的数量增加,使系统更加麻烦

cpp 复制代码
#include<iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0;
};
class Cricle :public Shape {
public:
    void draw() {
        cout << "draw cricle" << endl;
    }
};
class Rectangle :public Shape {
public:
    void draw() {
        cout << "draw rectangle" << endl;
    }
};
class ShapeFactory {
public:
    virtual Shape* creatFactory() = 0;
    
};
class CricleFactory :public ShapeFactory {
    Shape* creatFactory() {
        return new Cricle;
    }
};
class RectangleFactory :public ShapeFactory {
    Shape* creatFactory() {
        return new Rectangle;
    }
};
int main() {
    ShapeFactory* s = new CricleFactory;
    Shape* sp = s->creatFactory();
    sp->draw();
    return 0;
    
}

抽象工厂模式

特点:工厂方法模式将对象的创建延迟到子类中,每一个具体产品对应一个具体工厂

优点:增加新产品时,只需增加一个具体工厂类,不需要修改工厂类,符合开闭原则

缺点:类的数量增加,使得系统更加复杂

cpp 复制代码
//假设我们要创建一个图形用户界面(GUI)工厂,它可以创建不同风格(如 Windows 风格和 Mac 风格)的按钮和文本框。
#include<iostream>
using namespace std;
class Button {
public:
    virtual void show() = 0;
};
class Text {
public:
    virtual void show() = 0;
};
class WindowsButton : public Button {
public:
    void show()  {
        cout << "Windows button." << endl;
    }
};
class MacButton : public Button {
public:
    void show()  {
        cout << "Mac button." << endl;
    }
};
class WindowsText : public Text {
public:
    void show()  {
        cout << "Windows text." << endl;
    }
};
class MacText : public Text {
public:
    void show() {
        cout << "Mac text." << endl;
    }
};
class Factory {
public:
    virtual Button* createButton() = 0;
    virtual Text* createText() = 0;
};
class WindowFactory :public Factory {
public:
    Button* createButton() {
        return new WindowsButton;
    }
    Text* createText() {
        return new WindowsText;
    }
};
class MacFactory :public Factory {
public:
    Button* createButton() {
        return new MacButton;
    }
    Text* createText() {
        return new MacText;
    }
};



int main() {
    WindowFactory w;
    Button* b=w.createButton();
    b->show();
    return 0;
    
}

简单工厂模式:适用于产品种类不多且不频繁变动的场景,但不符合开闭原则

工厂方法模式:适用于产品种类多且经常变动的场景,每增加一个产品只需要增加一个对应的工厂,符合开闭原则

抽象工厂模式:适用于创建一系列相互依赖的产品对象的场景,但增加新的产品族时,改动大,符合开闭原则

相关推荐
程序员-King.2 小时前
【接口封装】——13、登录窗口的标题栏内容设置
c++·qt
萌の鱼3 小时前
leetcode 2826. 将三个组排序
数据结构·c++·算法·leetcode
Biomamba生信基地3 小时前
两天入门R语言,周末开讲
开发语言·r语言·生信
RAN_PAND3 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
Bio Coder3 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言
Tiger Z3 小时前
R 语言科研绘图第 27 期 --- 密度图-分组
开发语言·程序人生·r语言·贴图
life_time_6 小时前
C语言(22)
c语言·开发语言
Minner-Scrapy6 小时前
DApp 开发入门指南
开发语言·python·web app
mit6.8246 小时前
[实现Rpc] 通信-Muduo库的实现 | && 完美转发 | reserve | unique_lock
c++·网络协议·rpc