当前位置: 首页 > 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)>...

Vim 调用外部命令学习笔记

Vim 外部命令集成完全指南 文章目录 Vim 外部命令集成完全指南核心概念理解命令语法解析语法对比 常用外部命令详解文本排序与去重文本筛选与搜索高级 grep 搜索技巧文本替换与编辑字符处理高级文本处理编程语言处理其他实用命令 范围操作示例指定行范围处理复合命令示例 实用技…...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析

今天聊的内容&#xff0c;我认为是AI开发里面非常重要的内容。它在AI开发里无处不在&#xff0c;当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗"&#xff0c;或者让翻译模型 "将这段合同翻译成商务日语" 时&#xff0c;输入的这句话就是 Prompt。…...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?

Golang 面试经典题&#xff1a;map 的 key 可以是什么类型&#xff1f;哪些不可以&#xff1f; 在 Golang 的面试中&#xff0c;map 类型的使用是一个常见的考点&#xff0c;其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...

Day131 | 灵神 | 回溯算法 | 子集型 子集

Day131 | 灵神 | 回溯算法 | 子集型 子集 78.子集 78. 子集 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a; 笔者写过很多次这道题了&#xff0c;不想写题解了&#xff0c;大家看灵神讲解吧 回溯算法套路①子集型回溯【基础算法精讲 14】_哔哩哔哩_bilibili 完…...

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决

Spring Cloud Gateway 中自定义验证码接口返回 404 的排查与解决 问题背景 在一个基于 Spring Cloud Gateway WebFlux 构建的微服务项目中&#xff0c;新增了一个本地验证码接口 /code&#xff0c;使用函数式路由&#xff08;RouterFunction&#xff09;和 Hutool 的 Circle…...

蓝桥杯 冶炼金属

原题目链接 &#x1f527; 冶炼金属转换率推测题解 &#x1f4dc; 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V&#xff0c;是一个正整数&#xff0c;表示每 V V V 个普通金属 O O O 可以冶炼出 …...

Vite中定义@软链接

在webpack中可以直接通过符号表示src路径&#xff0c;但是vite中默认不可以。 如何实现&#xff1a; vite中提供了resolve.alias&#xff1a;通过别名在指向一个具体的路径 在vite.config.js中 import { join } from pathexport default defineConfig({plugins: [vue()],//…...

go 里面的指针

指针 在 Go 中&#xff0c;指针&#xff08;pointer&#xff09;是一个变量的内存地址&#xff0c;就像 C 语言那样&#xff1a; a : 10 p : &a // p 是一个指向 a 的指针 fmt.Println(*p) // 输出 10&#xff0c;通过指针解引用• &a 表示获取变量 a 的地址 p 表示…...

水泥厂自动化升级利器:Devicenet转Modbus rtu协议转换网关

在水泥厂的生产流程中&#xff0c;工业自动化网关起着至关重要的作用&#xff0c;尤其是JH-DVN-RTU疆鸿智能Devicenet转Modbus rtu协议转换网关&#xff0c;为水泥厂实现高效生产与精准控制提供了有力支持。 水泥厂设备众多&#xff0c;其中不少设备采用Devicenet协议。Devicen…...