aspnet网站开发实例教程pdf,网站设计制作厂家有哪些,素材网站的素材可以商用吗,项目建设方案怎么写点击蓝字关注我们因公众号更改推送规则#xff0c;请点“在看”并加“星标”第一时间获取精彩技术分享来源于网络#xff0c;侵删1、简介简单工厂方法定义一个用于创建对象的类#xff0c;该类接受一个参数#xff0c;通过参数决定创建不同的对象。GOF并没有把简单工厂方法…点击蓝字关注我们因公众号更改推送规则请点“在看”并加“星标”第一时间获取精彩技术分享来源于网络侵删1、简介简单工厂方法定义一个用于创建对象的类该类接受一个参数通过参数决定创建不同的对象。GOF并没有把简单工厂方法定义为23种设计模式之一可以认为简单工厂方法是工厂方法的简化形式。为了体现简单工厂方法和工厂方法的区别和联系此处把简单工厂方法先单独讲一下。2、模拟场景假设你要生产电脑电脑由硬盘、内存条、CPU、主板的部件组成。你为了保证供应链可靠每种部件都选择了至少两家供应商。比如硬盘供应商 seagate、Toshiba内存条供应商 SAMSUNG、CrucialCPU供应商 intel、AMD主板供应商 intel、AMD此处列出多个部件是为了后面讲解工厂方法、抽象工厂方法时使用同一个模拟场景。本章讲简单工厂方法暂时不需要涉及这么多部件所以仅以硬盘这一个部件为例进行讲解。3、实现的思路硬盘就是要创建的对象即产品。为了让不同供应商提供的硬盘可以通用要定义一个硬盘产品类并让不同供应商的硬盘都继承硬盘产品类的接口。还需要定义一个创建硬盘对象的类即工厂。工厂类根据参数决定创建哪家供应商的硬盘对象。4、实现硬盘对象创建参与者:1Product: HardDisk 定义硬盘对象的接口2Concrete Product: SeagateHardDisk, ToshibaHardDisk 实现不同供应商的硬盘3SimpleFactory: HardDiskFactory 根据参数创建不同供应商的硬盘对象在公众号【C语言中文社区】回复“C语言”三个字免费领取500G精品编程资料UMLHardDisk代码示例hard_disk.h#ifndef HARD_DISK_H
#define HARD_DISK_Hstruct HardDisk {void (*Operation)(struct HardDisk *this);
};#endifSeagateHardDisk代码示例seagate_hard_disk.h#ifndef SEAGATE_HARD_DISK_H
#define SEAGATE_HARD_DISK_H#include hard_disk.hstruct SeagateHardDisk {struct HardDisk hardDisk;
};// 构造函数
void SeagateHardDisk(struct SeagateHardDisk *this);// 析构函数
void _SeagateHardDisk(struct SeagateHardDisk *this);#endifseagate_hard_disk.c#include seagate_hard_disk.h
#include stdio.hvoid SeagateOperation(struct SeagateHardDisk *this)
{printf(这是 Seagate 硬盘\n);
}void SeagateHardDisk(struct SeagateHardDisk *this)
{this-hardDisk.Operation (void(*)(struct HardDisk *))SeagateOperation;
}void _SeagateHardDisk(struct SeagateHardDisk *this)
{this-hardDisk.Operation NULL;
}ToshibaHardDisk代码示例toshiba_hard_disk.h#ifndef TOSHIBA_HARD_DISK_H
#define TOSHIBA_HARD_DISK_H#include hard_disk.hstruct ToshibaHardDisk {struct HardDisk hardDisk;
};// 构造函数
void ToshibaHardDisk(struct ToshibaHardDisk *this);// 析构函数
void _ToshibaHardDisk(struct ToshibaHardDisk *this);#endiftoshiba_hard_disk.c#include toshiba_hard_disk.h
#include stdio.hvoid ToshibaOperation(struct ToshibaHardDisk *this)
{printf(这是 Toshiba 硬盘\n);
}void ToshibaHardDisk(struct ToshibaHardDisk *this)
{this-hardDisk.Operation (void(*)(struct HardDisk *))ToshibaOperation;
}void _ToshibaHardDisk(struct ToshibaHardDisk *this)
{this-hardDisk.Operation NULL;
}HardDiskFactory代码示例hard_disk_factory.h#ifndef HARD_DISK_FACTORY_H
#define HARD_DISK_FACTORY_H#include hard_disk.henum HARD_DISK_SUPPLIER_E {HARD_DISK_SUPPLIER_SEAGATE,HARD_DISK_SUPPLIER_TOSHIBA
};struct HardDiskFactory {struct HardDisk* (*Create)(struct HardDiskFactory *this, enum HARD_DISK_SUPPLIER_E supplier);void (*Destroy)(struct HardDiskFactory *this, struct HardDisk* hardDisk);
};// 构造函数
void HardDiskFactory(struct HardDiskFactory *this);// 析构函数
void _HardDiskFactory(struct HardDiskFactory *this);#endifhard_disk_factory.cC语言学习资源汇总【最新版】#include hard_disk_factory.h
#include seagate_hard_disk.h
#include toshiba_hard_disk.h
#include stdio.h
#include stdlib.hstruct HardDisk *Create(struct HardDiskFactory *this, enum HARD_DISK_SUPPLIER_E supplier)
{switch (supplier) {case HARD_DISK_SUPPLIER_SEAGATE:{struct SeagateHardDisk *seagateHardDisk NULL;if ((seagateHardDisk malloc(sizeof(struct SeagateHardDisk))) NULL) {printf(fail in malloc\n);return NULL;}SeagateHardDisk(seagateHardDisk);return (struct HardDisk *)seagateHardDisk;}case HARD_DISK_SUPPLIER_TOSHIBA:{struct ToshibaHardDisk *toshibaHardDisk NULL;if ((toshibaHardDisk malloc(sizeof(struct ToshibaHardDisk))) NULL) {printf(fail in malloc\n);return NULL;}ToshibaHardDisk(toshibaHardDisk);return (struct HardDisk *)toshibaHardDisk;}default:printf(未知的供应商\n);return NULL;}
}void Destroy(struct HardDiskFactory *this, struct HardDisk* hardDisk)
{if (hardDisk ! NULL) {free(hardDisk);}
}// 构造函数
void HardDiskFactory(struct HardDiskFactory *this)
{this-Create Create;this-Destroy Destroy;
}// 析构函数
void _HardDiskFactory(struct HardDiskFactory *this)
{this-Create NULL;this-Destroy NULL;
}客户端代码示例#include hard_disk.h
#include hard_disk_factory.h
#include stddef.hvoid main()
{struct HardDisk *hardDisk NULL;struct HardDiskFactory hardDiskFactory;HardDiskFactory(hardDiskFactory);// 创建 seagate 硬盘对象hardDisk hardDiskFactory.Create(hardDiskFactory, HARD_DISK_SUPPLIER_SEAGATE);// 使用 seagate 硬盘对象hardDisk-Operation(hardDisk); // 销毁 seagate 硬盘对象hardDiskFactory.Destroy(hardDiskFactory, hardDisk); // 创建 toshiba 硬盘对象hardDisk hardDiskFactory.Create(hardDiskFactory, HARD_DISK_SUPPLIER_TOSHIBA);// 使用 seagate 硬盘对象hardDisk-Operation(hardDisk);// 销毁 toshiba 硬盘对象hardDiskFactory.Destroy(hardDiskFactory, hardDisk); _HardDiskFactory(hardDiskFactory);
}客户端显示示例./hard_disk
这是 Seagate 硬盘
这是 Toshiba 硬盘如果你年满18周岁以上又觉得学【C语言】太难想尝试其他编程语言那么我推荐你学Python现有价值499元Python零基础课程限时免费领取限10个名额▲扫描二维码-免费领取戳“阅读原文”我们一起进步