简单工厂方法模式之“构造函数和析构函数”理解

迈不过友情╰ 2022-11-14 14:40 185阅读 0赞

reference: https://blog.csdn.net/weixin\_46826913/article/details/106289647
简单工厂模式(C语言实现)
简单工厂、工厂方法模式、抽象工厂模式
C++ 类构造函数 & 析构函数

使用C语言的来模拟C++中的面向对象概念。

1 简介

简单工厂方法定义一个用于创建对象的类,该类接受一个参数,通过参数决定创建不同的对象。

GOF并没有把简单工厂方法定义为23种设计模式之一,可以认为简单工厂方法是工厂方法的简化形式。

为了体现简单工厂方法和工厂方法的区别和联系,此处把简单工厂方法先单独讲一下。

2 模拟场景

假设你要生产电脑,电脑由硬盘、内存条、CPU、主板的部件组成。你为了保证供应链可靠,每种部件都选择了至少两家供应商。比如:

  1. 硬盘供应商 seagateToshiba
  2. 内存条供应商 SAMSUNGCrucial
  3. CPU供应商 intelAMD
  4. 主板供应商 intelAMD

此处列出多个部件是为了后面讲解工厂方法、抽象工厂方法时使用同一个模拟场景。本章讲简单工厂方法暂时不需要涉及这么多部件,所以仅以硬盘这一个部件为例进行讲解。

3 使用简单工厂方法实现的思路

硬盘就是要创建的对象(即:产品)。为了让不同供应商提供的硬盘可以通用,要定义一个硬盘产品类,并让不同供应商的硬盘都继承硬盘产品类的接口。

还需要定义一个创建硬盘对象的类(即:工厂)。工厂类根据参数决定创建哪家供应商的硬盘对象。

4 使用简单工厂方法实现硬盘对象创建

参与者

Product: HardDisk
定义硬盘对象的接口

Concrete Product: SeagateHardDisk, ToshibaHardDisk
实现不同供应商的硬盘

SimpleFactory: HardDiskFactory
根据参数,创建不同供应商的硬盘对象

UML:在这里插入图片描述
HardDisk代码示例

hard_disk.h

  1. #ifndef HARD_DISK_H
  2. #define HARD_DISK_H
  3. struct HardDisk {
  4. void (*Operation)(struct HardDisk *this);
  5. };
  6. #endif

SeagateHardDisk代码示例

  1. seagate_hard_disk.h
  2. #ifndef SEAGATE_HARD_DISK_H
  3. #define SEAGATE_HARD_DISK_H
  4. #include "hard_disk.h"
  5. struct SeagateHardDisk {
  6. struct HardDisk hardDisk;
  7. };
  8. // 构造函数
  9. void SeagateHardDisk(struct SeagateHardDisk *this);
  10. // 析构函数
  11. void _SeagateHardDisk(struct SeagateHardDisk *this);
  12. #endif

seagate_hard_disk.c

  1. #include "seagate_hard_disk.h"
  2. #include "stdio.h"
  3. void SeagateOperation(struct SeagateHardDisk *this)
  4. {
  5. printf("这是 Seagate 硬盘\n");
  6. }
  7. void SeagateHardDisk(struct SeagateHardDisk *this)
  8. {
  9. this->hardDisk.Operation = (void(*)(struct HardDisk *))SeagateOperation;
  10. }
  11. void _SeagateHardDisk(struct SeagateHardDisk *this)
  12. {
  13. this->hardDisk.Operation = NULL;
  14. }

ToshibaHardDisk代码示例

toshiba_hard_disk.h

  1. #ifndef TOSHIBA_HARD_DISK_H
  2. #define TOSHIBA_HARD_DISK_H
  3. #include "hard_disk.h"
  4. struct ToshibaHardDisk {
  5. struct HardDisk hardDisk;
  6. };
  7. // 构造函数
  8. void ToshibaHardDisk(struct ToshibaHardDisk *this);
  9. // 析构函数
  10. void _ToshibaHardDisk(struct ToshibaHardDisk *this);
  11. #endif

toshiba_hard_disk.c

  1. #include "toshiba_hard_disk.h"
  2. #include "stdio.h"
  3. void ToshibaOperation(struct ToshibaHardDisk *this)
  4. {
  5. printf("这是 Toshiba 硬盘\n");
  6. }
  7. void ToshibaHardDisk(struct ToshibaHardDisk *this)
  8. {
  9. this->hardDisk.Operation = (void(*)(struct HardDisk *))ToshibaOperation;
  10. }
  11. void _ToshibaHardDisk(struct ToshibaHardDisk *this)
  12. {
  13. this->hardDisk.Operation = NULL;
  14. }

HardDiskFactory代码示例

  1. hard_disk_factory.h
  2. #ifndef HARD_DISK_FACTORY_H
  3. #define HARD_DISK_FACTORY_H
  4. #include "hard_disk.h"
  5. enum HARD_DISK_SUPPLIER_E {
  6. HARD_DISK_SUPPLIER_SEAGATE,
  7. HARD_DISK_SUPPLIER_TOSHIBA
  8. };
  9. struct HardDiskFactory {
  10. struct HardDisk* (*Create)(struct HardDiskFactory *this,
  11. enum HARD_DISK_SUPPLIER_E supplier);
  12. void (*Destroy)(struct HardDiskFactory *this,
  13. struct HardDisk* hardDisk);
  14. };
  15. // 构造函数
  16. void HardDiskFactory(struct HardDiskFactory *this);
  17. // 析构函数
  18. void _HardDiskFactory(struct HardDiskFactory *this);
  19. #endif

hard_disk_factory.c

  1. #include "hard_disk_factory.h"
  2. #include "seagate_hard_disk.h"
  3. #include "toshiba_hard_disk.h"
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. struct HardDisk *Create(struct HardDiskFactory *this,
  7. enum HARD_DISK_SUPPLIER_E supplier)
  8. {
  9. switch (supplier) {
  10. case HARD_DISK_SUPPLIER_SEAGATE:
  11. {
  12. struct SeagateHardDisk *seagateHardDisk = NULL;
  13. if ((seagateHardDisk = malloc(sizeof(struct SeagateHardDisk))) == NULL) {
  14. printf("fail in malloc\n");
  15. return NULL;
  16. }
  17. SeagateHardDisk(seagateHardDisk);
  18. return (struct HardDisk *)seagateHardDisk;
  19. }
  20. case HARD_DISK_SUPPLIER_TOSHIBA:
  21. {
  22. struct ToshibaHardDisk *toshibaHardDisk = NULL;
  23. if ((toshibaHardDisk = malloc(sizeof(struct ToshibaHardDisk))) == NULL) {
  24. printf("fail in malloc\n");
  25. return NULL;
  26. }
  27. ToshibaHardDisk(toshibaHardDisk);
  28. return (struct HardDisk *)toshibaHardDisk;
  29. }
  30. default:
  31. printf("未知的供应商\n");
  32. return NULL;
  33. }
  34. }
  35. void Destroy(struct HardDiskFactory *this, struct HardDisk* hardDisk)
  36. {
  37. if (hardDisk != NULL) {
  38. free(hardDisk);
  39. }
  40. }
  41. // 构造函数
  42. void HardDiskFactory(struct HardDiskFactory *this)
  43. {
  44. this->Create = Create;
  45. this->Destroy = Destroy;
  46. }
  47. // 析构函数
  48. void _HardDiskFactory(struct HardDiskFactory *this)
  49. {
  50. this->Create = NULL;
  51. this->Destroy = NULL;
  52. }

客户端代码示例

  1. #include "hard_disk.h"
  2. #include "hard_disk_factory.h"
  3. #include "stddef.h"
  4. void main()
  5. {
  6. struct HardDisk *hardDisk = NULL;
  7. struct HardDiskFactory hardDiskFactory;
  8. HardDiskFactory(&hardDiskFactory);
  9. // 创建 seagate 硬盘对象
  10. hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_SEAGATE);
  11. // 使用 seagate 硬盘对象
  12. hardDisk->Operation(hardDisk);
  13. // 销毁 seagate 硬盘对象
  14. hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);
  15. // 创建 toshiba 硬盘对象
  16. hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_TOSHIBA);
  17. // 使用 seagate 硬盘对象
  18. hardDisk->Operation(hardDisk);
  19. // 销毁 toshiba 硬盘对象
  20. hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);
  21. _HardDiskFactory(&hardDiskFactory);
  22. }

客户端显示示例

  1. ./hard_disk
  2. 这是 Seagate 硬盘
  3. 这是 Toshiba 硬盘

发表评论

表情:
评论列表 (有 0 条评论,185人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C#函数构造函数

          在学习C\时这两个函数放到一起讲了,听课的时候感觉只是有了肤浅的认识,于是查了一些资料,下面做个比较全面的理解。       析构函数——垃圾回收器,它用来清理

    相关 C++ 构造函数函数

    百度百科:构造函数 ,是一种特殊的方法。主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。特别的一个类可以有多个构造函数

    相关 使用构造函数函数

    构造函数和析构函数控制对象的创建和毁坏。 若要为类创建构造函数,请在类定义的任何位置创建名为 Sub New 的过程。若要创建参数化构造函数,请像为其他任何过程指定参数那样为