2311C++抽象工厂

2023-11-07 03:52
文章标签 c++ 抽象 工厂 2311

本文主要是介绍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++抽象工厂的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/361085

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取