设计模式中的工厂模式,有哪些分类,请详细说明,并且使用c++ 语言分别写出对应的代码

时间: 2023-06-04 11:07:33 浏览: 67
好的,我可以回答这个问题。以下是设计模式中工厂模式的分类: 1. 简单工厂模式 简单工厂模式又叫静态工厂模式,是指根据传递进来的参数,动态决定创建哪种类的对象。该模式只有一个工厂方法,负责根据传入的参数来创建具体的产品。在该模式中,工厂类负责创建所有的对象,但这样也带来了类的膨胀和耦合的问题。 以下是使用 c 语言写的简单工厂模式: ```c //产品接口 typedef struct Product { void (*operation)(struct Product *); } Product; //具体产品1 typedef struct ConcreteProduct1 { void (*operation)(Product *); } ConcreteProduct1; //具体产品2 typedef struct ConcreteProduct2 { void (*operation)(Product *); } ConcreteProduct2 //工厂类 Product *factory(int type) { switch(type) { case 1: return (Product *)ConcreteProduct1_new(); case 2: return (Product *)ConcreteProduct2_new(); default: return NULL; } } //具体产品1实现接口 void ConcreteProduct1_operation(Product *product) { //具体实现 } //具体产品2实现接口 void ConcreteProduct2_operation(Product *product) { //具体实现 } ``` 2. 工厂方法模式 工厂方法模式是指每个具体产品对应一个工厂,由这个工厂负责创建该产品的对象实例。这样每个产品类和产品类的创建者都可以被单独的修改而互不影响。工厂方法模式由抽象工厂类、具体工厂类、抽象产品类和具体产品类组成。 以下是使用 c 语言写的工厂方法模式: ```c //产品接口 typedef struct IProduct { void (*operation)(struct IProduct *); } IProduct; //具体产品1 typedef struct ConcreteProduct1 { void (*operation)(IProduct *); } ConcreteProduct1; //具体产品2 typedef struct ConcreteProduct2 { void (*operation)(IProduct *); } ConcreteProduct2; //抽象工厂类 typedef struct IFactory { IProduct *(*create_product)(int); } IFactory; //具体工厂1 typedef struct ConcreteFactory1 { IProduct *(*create_product)(int); } ConcreteFactory1; //具体工厂2 typedef struct ConcreteFactory2 { IProduct *(*create_product)(int); } ConcreteFactory2; //具体工厂1实现接口 IProduct *ConcreteFactory1_create_product(int type) { switch(type) { case 1: return (IProduct *)ConcreteProduct1_new(); case 2: return (IProduct *)ConcreteProduct2_new(); default: return NULL; } } //具体工厂2实现接口 IProduct *ConcreteFactory2_create_product(int type) { switch(type) { case 1: return (IProduct *)ConcreteProduct1_new(); case 2: return (IProduct *)ConcreteProduct2_new(); default: return NULL; } } //具体产品1实现接口 void ConcreteProduct1_operation(IProduct *product) { //具体实现 } //具体产品2实现接口 void ConcreteProduct2_operation(IProduct *product) { //具体实现 } ``` 以上是对工厂模式分类的详细说明并使用 c 语言分别写出了对应的代码,希望能够对你有所帮助。

相关推荐

最新推荐

recommend-type

用UML描述C++设计模式,且附带实现代码

C++设计模式 一、功能 二、结构图 ...//结构图 三、实现 和其他很多模式一样,学习设计模式的重点是学习每种模式的思想,而不应拘泥于它的某种具体结构图和实现。因为模式是灵活的,其实现可以是千变万化的,只是...
recommend-type

C/C++语言宏定义使用实例详解

主要介绍了 C/C++语言宏定义使用实例详解的相关资料,需要的朋友可以参考下
recommend-type

linux系统中c++写日志文件功能分享

主要介绍了linux系统中c++写日志文件功能,简化了glog,只保留了写日志文件的功能,只是改写了linux版本,需要的朋友可以参考下
recommend-type

vscode使用官方C/C++插件无法进行代码格式化问题

官方的C/C++插件是支持使用.clang-format配置文件进行自定义风格代码格式化的,无需另外安装clang-format插件。 但是使用clang-format -style=llvm -dump-config > .clang-format导出的默认配置文件进行格式化的时候...
recommend-type

C++ 中boost::share_ptr智能指针的使用方法

主要介绍了C++ 中boost::share_ptr智能指针的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。