当前位置: 首页 > news >正文

【设计模式】工厂模式总结

工厂模式

定义一个创建对象的接口,让子类决定实例化哪个类,而对象的创建统一交由工厂去生产。

工厂模式大致可以分为三类:简单工厂模式、工厂方法模式、抽象工厂模式

简单工厂模式

简单工厂模式提供一个工厂类,根据传入的参数来创建不同类型的对象,而客户端代码无需了解对象的创建过程。

#include <iostream>// 基类,所有图形对象的父类
class Shape {
public:virtual void draw() = 0;
};// 具体的圆形类
class Circle : public Shape {
public:void draw() {std::cout << "Draw a circle" << std::endl;}
};// 具体的矩形类
class Rectangle : public Shape {
public:void draw() {std::cout << "Draw a rectangle" << std::endl;}
};// 简单工厂类
class ShapeFactory {
public:// 根据传入的参数创建不同类型的图形对象Shape* createShape(const std::string& shapeType) {if (shapeType == "circle") {return new Circle();} else if (shapeType == "rectangle") {return new Rectangle();} else {return nullptr; // 可以添加错误处理逻辑}}
};int main() {ShapeFactory factory;// 创建圆形对象Shape* circle = factory.createShape("circle");if (circle) {circle->draw();delete circle;}return 0;
}

缺点:增加新类型时需要修改工厂类,违反开闭原则。

工厂方法模式

工厂方法模式定义了一个创建对象的接口,但将具体对象的创建交给子类来实现。

#include <iostream>
#include <string>// 抽象产品类:文档
class Document {
public:virtual void open() = 0;virtual void save() = 0;
};// 具体产品类:PDF 文档
class PDFDocument : public Document {
public:void open() {std::cout << "Opening a PDF document" << std::endl;}void save() {std::cout << "Saving a PDF document" << std::endl;}
};// 具体产品类:文本文档
class TextDocument : public Document {
public:void open() {std::cout << "Opening a Text document" << std::endl;}void save() {std::cout << "Saving a Text document" << std::endl;}
};// 抽象工厂类
class DocumentFactory {
public:virtual Document* createDocument() = 0;
};// 具体工厂类:PDF 文档工厂
class PDFDocumentFactory : public DocumentFactory {
public:Document* createDocument() {return new PDFDocument();}
};// 具体工厂类:文本文档工厂
class TextDocumentFactory : public DocumentFactory {
public:Document* createDocument() {return new TextDocument();}
};int main() {DocumentFactory* factory = nullptr;Document* doc = nullptr;// 创建 PDF 文档factory = new PDFDocumentFactory();doc = factory->createDocument();doc->open();doc->save();delete doc;delete factory;// 创建文本文档factory = new TextDocumentFactory();doc = factory->createDocument();doc->open();doc->save();delete doc;delete factory;return 0;
}

优点: 扩展性好,符合了开闭原则, 新增一种产品时,只需增加改对应的产品类和对应的工厂子类即可。

缺点:每增加一种新类型,就需要增加一个对象的工厂; 相比简单工厂模式,工厂方法模式需要更多的类定义。

抽象工厂模式

抽象工厂模式允许你创建一系列相关的对象,而不指定具体的类。

#include <iostream>
#include <string>// 抽象产品类:按钮
class Button {
public:virtual void render() = 0;
};// 具体产品类:Windows按钮
class WindowsButton : public Button {
public:void render() {std::cout << "Rendering a Windows button" << std::endl;}
};// 具体产品类:Linux按钮
class LinuxButton : public Button {
public:void render() {std::cout << "Rendering a Linux button" << std::endl;}
};// 抽象产品类:窗口
class Window {
public:virtual void createButton() = 0;
};// 具体产品类:Windows窗口
class WindowsWindow : public Window {
public:void createButton() {Button* button = new WindowsButton();button->render();}
};// 具体产品类:Linux窗口
class LinuxWindow : public Window {
public:void createButton() {Button* button = new LinuxButton();button->render();}
};// 抽象工厂类
class GUIFactory {
public:virtual Window* createWindow() = 0;
};// 具体工厂类:Windows工厂
class WindowsGUIFactory : public GUIFactory {
public:Window* createWindow() {return new WindowsWindow();}
};// 具体工厂类:Linux工厂
class LinuxGUIFactory : public GUIFactory {
public:Window* createWindow() {return new LinuxWindow();}
};int main() {GUIFactory* factory = nullptr;Window* window = nullptr;// 创建Windows风格的窗口和按钮factory = new WindowsGUIFactory();window = factory->createWindow();delete factory;// 创建Linux风格的窗口和按钮factory = new LinuxGUIFactory();window = factory->createWindow();delete factory;return 0;
}

优点: 工厂抽象类创建了多个类型的产品,当有需求时,可以创建相关产品子类和子工厂类来获取。

缺点: 需要在抽象工厂类中提前确定可能需要的产品种类,以满足不同型号的多种产品的需求;如果我们需要的产品种类并没有在抽象工厂类中提前确定,则需要修改抽象工厂类了,以及所有的工厂子类。

http://www.lryc.cn/news/222827.html

相关文章:

  • 网络安全管理员高级工理论题库(持续更新中)
  • RestTemplate配置和使用
  • 【Hadoop】YARN容量调度器详解
  • 20个Python实用小技巧!来自十年老程序员的推荐~
  • jenkins原理篇——成员权限管理
  • 13.求面积[有问题]
  • 【力扣】面试经典150题——哈希表
  • Python批量导入及导出项目中所安装的类库包到.txt文件(补充)
  • 2023 全栈工程师 Node.Js 服务器端 web 框架 Express.js 详细教程(更新中)
  • 【Leetcode】【数据结构】【C语言】判断两个链表是否相交并返回交点地址
  • Selenium爬取内容并存储至MySQL数据库
  • 蓝桥等考C++组别六级 007
  • 集合框架:Set集合的特点、HashSet集合的底层原理、哈希表、实现去重复
  • 【T690 之十二】基于方寸EVB2开发板(T690芯片)构建基于GMSSL的文件系统的方式
  • 使用Selenium发邮件附件
  • 公共数据这座金矿,授权运营为何是赋能的关键路径?
  • 昇腾CANN 7.0 黑科技:大模型推理部署技术解密
  • OAuth 2.0
  • 7个设计师必备的Figma汉化插件,高效设计超简单!
  • 缓存-基础理论和Guava Cache介绍
  • 机器人伺服驱动控制环
  • 单链表(3)
  • Android14前台服务适配指南
  • Spring Boot中使用Spring Data JPA访问MySQL
  • Go 语言函数闭包(匿名函数)
  • 2023年11月编程语言流行度排名
  • apache-maven-3.6.3 安装配置教程
  • 你一般什么时候使用GPT
  • kubernetes (k8s)的使用
  • RK3568平台开发系列讲解(音视频篇)RTMP 推流