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

2311C++抽象工厂

1,为啥需要工厂设计模式?工厂设计模式可解决什么问题?

先看一下示例,多态示例.

#include <iostream>
using namespace std;
class Shape {
public:Shape() { }virtual void drawShape(){cout << "base draw shape" << endl;}
};
class Rectangular : public Shape {
public:Rectangular(){ }void drawShape (){cout << "draw rectangular" << endl;}
};
class Triangular : public Shape {
public:Triangular () { }void drawShape (){cout << "draw triangular" << endl;}
};
class Circle : public Shape {
public:Circle(){ }void drawShape (){cout << "draw circle" << endl;}
};
int main()
{Shape *rect = new Rectangular();rect->drawShape();delete rect;Shape *tri = new Triangular();tri->drawShape();delete tri;Shape *cir = new Circle();cir->drawShape();delete cir;/*这里添加许多形状...*/return 0;
}

上述代码示例很简单,想带多态,绘画矩形,三角形,圆等.思考一个问题:如果还要画菱形,椭圆等等.很容易出现N多子类继承基本Shape,main()中的会大量的new XXXX,导致很难维护和扩展代码.

这就是工厂对象设计模式解决的问题之一.

还有,基类并不知道具体要实例化哪一个具体的子类,具体实例化要延迟继承类中.

2,工厂设计模式

工厂创建产品. 产品==>多子产品.

工厂设计模式结构图,该模式下,工厂基类知道如何实例化具体产品

工厂=>具体工厂(创建产品)方法
产品(操作)=>具体产品(操作).

工厂设计模式结构图,延迟到子类中具体实例化对象方法

基本要素:
(1)抽象产品Product;
(2)具体产品ConcreteProduct;
(3)抽象工厂Factory.
(4)具体的工厂ConcreteFactory

下面直接给出代码示例:
头文件

#include <iostream>
using namespace std;
class Computer {
public:Computer();virtual ~Computer() = 0;
private:
};
class DellComputer: public Computer {
public:~DellComputer();DellComputer();
private:
};
class AppleComputer : public Computer {
public:AppleComputer();~AppleComputer();
};
class Factory {
public:Factory();virtual ~Factory() = 0;virtual Computer *CreateComputer(const string &type) = 0;//
private:
//串类型;
};
class ConcreateFactory: public Factory {
public:~ConcreateFactory();ConcreateFactory();Computer *CreateComputer(const string &type);
};//main.cpp
#include "product.h"
#include <iostream>
using namespace std;
Computer::Computer()
{cout << "Computer()" << endl;
}
Computer::~Computer()
{cout << "~Computer()" << endl;
}
DellComputer::DellComputer()
{cout << "DellComputer()" << endl;
}
DellComputer::~DellComputer()
{cout << "~DellComputer()" << endl;
}
AppleComputer::AppleComputer()
{cout << "AppleComputer()" << endl;
}
AppleComputer::~AppleComputer()
{cout << "~AppleComputer()" << endl;
}
Factory::Factory()
{cout << "Factory()" << endl;
}
Factory::~Factory()
{cout << "~Factory()" << endl;
}
ConcreateFactory::ConcreateFactory()
{cout << "ConcreateFactory()" << endl;
}
ConcreateFactory::~ConcreateFactory()
{cout << "~ConcreateFactory()" << endl;
}
Computer *ConcreateFactory::CreateComputer(const string &type)
{if (type == "dell")return new DellComputer();else if (type == "apple")return new AppleComputer();elsethrow invalid_argument("Invalid argument");
}
int main()
{Factory *fac = new ConcreateFactory();Computer *dell = fac->CreateComputer("dell"); //制造`dell`电脑delete dell;Computer *apple = fac->CreateComputer("apple"); //制造苹果电脑delete apple;delete fac;return 0;
}

使用工厂类的好处是,可在工厂类中封装创建对象的逻辑,使代码更模块化和清晰.同时,工厂类按需选择创建不同产品对象,客户代码只需与抽象产品类交互,而不需要了解具体产品类.

这样代码更灵活,且更容易扩展和修改.

3,抽象工厂设计模式

工厂模式仅局限于某一个类,有时候需要为一组相关或依赖的对象提供配套的接口.此时就需要抽象工厂模式了,其实抽象工厂模式工厂模式是一回事.

抽象工厂(创建A,创建B)
=>多个具体工厂(创建A,创建B)
抽象产品A=>具体A1,具体A2
抽象产品B=>具体B1,具体B2

抽象工厂设计模式结构图,抽象工厂设计模式就是工厂模式的扩展版.

假如要为生成的电脑装操作系统,假如Dell--->windows系统,Apple----->IOS系统,再来一个华为电脑,需要装鸿蒙系统等.
头文件

#include <iostream>
using namespace std;
class Computer {
public:Computer();virtual ~Computer() = 0;
private:
};
class DellComputer: public Computer {
public:~DellComputer();DellComputer();
private:
};
class OS {
public:OS();virtual ~OS() = 0;
};
class WindowsOS : public OS {
public:WindowsOS();~WindowsOS();
};
class Factory {
public:Factory();virtual ~Factory() = 0;virtual Computer *CreateComputer() = 0;virtual OS *CreateOS() = 0;
private:
//串类型;
};
class ConcreateFactory: public Factory {
public:~ConcreateFactory();ConcreateFactory();Computer *CreateComputer();OS *CreateOS();
};
//main.cpp
#include "product.h"
#include <iostream>
using namespace std;
Computer::Computer()
{cout << "Computer()" << endl;
}
Computer::~Computer()
{cout << "~Computer()" << endl;
}
DellComputer::DellComputer()
{cout << "DellComputer()" << endl;
}
DellComputer::~DellComputer()
{cout << "~DellComputer()" << endl;
}
OS::OS()
{cout << "OS()" << endl;
}
OS::~OS()
{cout << "~OS()" << endl;
}
WindowsOS::WindowsOS()
{cout << "WindowsOS()" << endl;
}
WindowsOS::~WindowsOS()
{cout << "~WindowsOS()" << endl;
}
Factory::Factory()
{cout << "Factory()" << endl;
}
Factory::~Factory()
{cout << "~Factory()" << endl;
}
ConcreateFactory::ConcreateFactory()
{cout << "ConcreateFactory()" << endl;
}
ConcreateFactory::~ConcreateFactory()
{cout << "~ConcreateFactory()" << endl;
}
Computer *ConcreateFactory::CreateComputer()
{return new DellComputer();
}
OS *ConcreateFactory::CreateOS()
{return new WindowsOS();
}
int main()
{Factory *fac = new ConcreateFactory();Computer *dell = fac->CreateComputer();OS *os = fac->CreateOS();delete dell;delete os;delete fac;return 0;
}

相关文章:

2311C++抽象工厂

1,为啥需要工厂设计模式?工厂设计模式可解决什么问题? 先看一下示例,多态示例. #include <iostream> using namespace std; class Shape { public:Shape() { }virtual void drawShape(){cout << "base draw shape" << endl;} }; class Rectang…...

Lavarel定时任务的使用

系统为window 执行命令(执行一次命令只会根据当前时间运行一次定时任务) php artisan schedule:run创建一个任务类(在Jobs文件夹下面) <?phpnamespace App\Jobs;use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contract…...

Java开发者的网络安全指南(二)

目录 一、加密和数据保护 二、身份验证和授权 三、Web应用程序安全 四、安全编码实践 五、网络防火墙和入侵检测系统 六、日志和监视 七、漏洞管理 八、安全教育和培训 九、结论 介绍&#xff1a; 简要说明网络安全的重要性和为什么Java开发者需要关注它。 一、加密…...

Python基础学习016__UnitTest

# UnitTest是python自带的一个单元测试框架,不需要额外安装 # 也是自动化脚本执行框架,使用UnitTest来管理,运行多个框架 # 为什么使用:能够组织多个用例去执行.提供了丰富的断言方法,能够生成测试报告 # 核心要素: # Testcase:测试用例:这个测试用例是UnitTest的组成部分,不是…...

一物一码需求,标签制作功能轻松解决

许多行业存在为人员、物品、设备等做一物一码标签的需求&#xff0c;可使用草料标签制作功能。直接选择标签样式&#xff0c;填入数据&#xff0c;即可批量生成标签&#xff0c;还可批量排版&#xff0c;更易落地。还可保存标签样式&#xff0c;后续多次复用样式&#xff0c;批…...

【Linux】七、基础IO

预备知识 文件 属性&#xff08;本质上也是数据&#xff09;内容&#xff1b; 文件的所有操作大致有两种&#xff0c;对内容的操作&#xff0c;和对属性的操作&#xff1b; 文件在磁盘中放置&#xff0c;磁盘是硬件&#xff0c;只有操作系统可以真正的访问磁盘&#xff1b;C\C…...

Elasticsearch语法之Term query不区分大小写

设置关键词是否区分大小写 说明&#xff1a;case_insensitive是term的可选参数&#xff0c;默认为false&#xff0c;表示关键词区分大小写&#xff0c;设置为true表示关键词不区分大小写。该参数在7.10.0开始有效 需求&#xff1a;分别使用关键词"iphone"和"I…...

远程管理SSH服务

一、搭建SSH服务 1、关闭防火墙与SELinux # 关闭firewalld防火墙 # 临时关闭 systemctl stop firewalld # 关闭开机自启动 systemctl disable firewalld ​ # 关闭selinux # 临时关闭 setenforce 0 # 修改配置文件 永久关闭 vim /etc/selinux/config SELINUXdisabled 2、配置…...

Linux 实现原理 — NUMA 多核架构中的多线程调度开销与性能优化

前言 NOTE&#xff1a;本文中所指 “线程” 均为可执行调度单元 Kernel Thread。 NUMA 体系结构 NUMA&#xff08;Non-Uniform Memory Access&#xff0c;非一致性存储器访问&#xff09;的设计理念是将 CPU 和 Main Memory 进行分区自治&#xff08;Local NUMA node&#x…...

Oracle锁处理

背景&#xff1a; 随着数据库版本不断迭代更新&#xff0c; v$session 视图的内容越来越丰富&#xff0c;可以直接使用blocking_session、blocking_instance、final_blocking_instance和final_blocking_session字段进行定位。对于锁层次的排查可以重复查询v$session来确定&am…...

持续集成交付CICD:安装Jenkins Slave(从节点)

目录 一、实验 1.安装Jenkins Slave&#xff08;从节点&#xff09; 二、问题 1.salve节点启动jenkins报错 2.终止命令行后jenkins从节点状态不在线 一、实验 1.安装Jenkins Slave&#xff08;从节点&#xff09; &#xff08;1&#xff09;查看jenkins版本 Version 2.…...

Dart(一):Dart入门

Dart入门 Dart安装创建项目安装依赖&#xff08;以http为例&#xff09;依赖库查询地址添加依赖编写运行示例 dart常用命令引用核心库、自定义库、第三方库数据类型Numbers (int, double)Strings (String)Booleans (bool)Lists (List)Maps (Map)Sets (Set)Null (null)Records (…...

[动态规划] (十一) 简单多状态 LeetCode 面试题17.16.按摩师 和 198.打家劫舍

[动态规划] (十一) 简单多状态: LeetCode 面试题17.16.按摩师 和 198.打家劫舍 文章目录 [动态规划] (十一) 简单多状态: LeetCode 面试题17.16.按摩师 和 198.打家劫舍题目分析题目解析状态表示状态转移方程初始化和填表顺序 代码实现按摩师打家劫舍 总结 注&#xff1a;本题与…...

【EI会议投稿】第三届计算机、人工智能与控制工程国际学术会议 (CAICE 2024)

The 3rd International Conference on Computer, Artificial Intelligence and Control Engineering (CAICE 2024) 第三届计算机、人工智能与控制工程国际学术会议 第三届计算机、人工智能与控制工程国际学术会议&#xff08;CAICE 2024&#xff09;将于2024年1月26-28日在西…...

python 之 列表推导式

文章目录 基本结构示例 1&#xff1a;将列表中的元素乘以 2 添加条件判断示例 2&#xff1a;筛选出偶数并加倍 嵌套列表推导式示例 3&#xff1a;生成九九乘法表 使用条件表达式示例 4&#xff1a;根据条件返回不同的值 镶嵌使用详细介绍基本结构示例生成二维数组多重筛选和操作…...

【左程云算法全讲2】链表、栈、队列、递归、哈希表和有序表

系列综述&#xff1a; &#x1f49e;目的&#xff1a;本系列是个人整理为了秋招面试的&#xff0c;整理期间苛求每个知识点&#xff0c;平衡理解简易度与深入程度。 &#x1f970;来源&#xff1a;材料主要源于左程云算法课程进行的&#xff0c;每个知识点的修正和深入主要参考…...

SQL第三次上机作业

1.查询与王利就读同一专业学生的借书证号和姓名 SELECT Lno,Rname FROM Reader WHERE Dept(SELECT DeptFROM ReaderWHERE Rname王利)2.查询比希望出版社出版的所有图书价格都高的图书信息 SELECT * FROM Book WHERE Price>(SELECT MAX(Price)FROM BookWHERE Press希望出版…...

前端事件案例补充

目录 定时器示例 搜索框示例 省市联动 定时器示例 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><meta name"viewport" content"widthdevice-width, init…...

3.8 Android eBPF HelloWorld调试(二)

写在前面 我们开发eBPF程序的初衷就是再不改动内核的情况下,将内核监控数据传递给到用户态;像应用进程开发一样开发内核监控程序。 Android开机的时候eBPF程序被加载器加载到内核中,但此时它并没有被附加到内核函数上去,也就是ebpf程序并不会执行,我们可以理解为,它仅仅被…...

xss如何快速提取cookies

<script>alert(111)</script> <img srcx onerroralert(document.cookie)>测试一下baidu的xss <script>alert(111)</script><img srcx οnerrοralert(document.cookie)>...

在 ASP.NET C# 中用Aspose.PDF将 PDF 页面转换为 JPG 图像

PDF 是一种通用格式&#xff0c;通常用于打印和共享文档。 &#xff08;一&#xff09;C# PDF to JPG Converter API - 免费下载 Aspose.PDF for .NET是一个强大的 PDF 操作 API&#xff0c;可让您在 .NET 应用程序中创建和处理 PDF 文件。此外&#xff0c;它还允许您将 PDF 文…...

Docker Compose安装milvus向量数据库单机版-milvus基本操作

目录 安装Ubuntu 22.04 LTS在power shell启动milvus容器安装docker desktop下载yaml文件启动milvus容器Milvus管理软件Attu python连接milvus配置下载wget示例导入必要的模块和类与Milvus数据库建立连接创建名为"hello_milvus"的Milvus数据表插入数据创建索引基于向量…...

极致性能优化:前端SSR渲染利器Qwik.js | 京东云技术团队

引言 前端性能已成为网站和应用成功的关键要素之一。用户期望快速加载的页面和流畅的交互&#xff0c;而前端框架的选择对于实现这些目标至关重要。然而&#xff0c;传统的前端框架在某些情况下可能面临性能挑战且存在技术壁垒。 在这个充满挑战的背景下&#xff0c;我们引入…...

ES6~ES13新特性(二)

文章目录 一、ES71.Array Includes2.指数exponentiation运算符 二、ES81.Object values2.Object entries3.String Padding4.Trailing Commas5.Object Descriptors 三、ES9四、ES101.flat flatMap2.Object fromEntries3.trimStart、trimEnd4.其他知识点 五、ES111.BigInt2.Nulli…...

soildwork2022怎么样添加螺纹孔?

1.退出草图模式&#xff0c;点击需要添加螺纹孔的物体面&#xff0c;选中“特征”中的“异形孔向导” 2.选中“孔类型”为“直螺纹孔”&#xff0c;“标准”&#xff0c;“类型”&#xff0c;“孔规格”终止条件等。 3.设置完之后选择“位置” 4.鼠标左键在物体面上点一下&…...

【t5 pytorch版源码学习】t5-pegasus-pytorch源码学习

0. 项目来源 中文生成式预训练模型&#xff0c;以mT5为基础架构和初始权重&#xff0c;通过类似PEGASUS的方式进行预训练。 bert4keras版&#xff1a;t5-pegasus pytorch版&#xff1a;t5-pegasus-pytorch 本次主要学习pytorch版的代码解读。 项目结构&#xff1a; train…...

【springboot】spring的Aop结合Redis实现对短信接口的限流

前言 场景: 为了限制短信验证码接口的访问次数&#xff0c;防止被刷&#xff0c;结合Aop和redis根据用户ip对用户限流 1.准备工作 首先我们创建一个 Spring Boot 工程&#xff0c;引入 Web 和 Redis 依赖&#xff0c;同时考虑到接口限流一般是通过注解来标记&#xff0c;而注解…...

【MedusaSTears】怎么禁用edge浏览器截图功能?

版本 Microsoft Edge 版本 119.0.2151.44 (正式版本) (64 位) Ctrl Shift S 竟然是浏览器的截屏? 特么的啥时候多了这么个快捷键? 然后还没办法禁用,真TMD傻哔 edge://settings/accessibility解决方式: 参考资料: 怎么禁用edge浏览器截图功能&#xff1f; 您好&#x…...

【计算机网络】(谢希仁第八版)第三章课后习题答案

第三章 1.数据链路(即逻辑链路)与链路(即物理链路)有何区别? “电路接通了”与”数据链路接通了”的区别何在? 答&#xff1a;数据链路与链路的区别在于数据链路出链路外&#xff0c;还必须有一些必要的规程来控制数据的传输&#xff0c;因此&#xff0c;数据链路比链路多了…...

批量异步任务处理

当我们在项目中遇到很多业务同时处理&#xff0c;如果是串行肯定是影响性能的&#xff0c;这时候就需要异步执行了&#xff0c;说道异步肯定就有很多方案了 方案一&#xff1a; 比如使用spring的异步注解&#xff0c;比如下面的代码,每个方法上面都是异步注解&#xff0c;当时…...