1 簡(jiǎn)介
簡(jiǎn)單工廠方法定義一個(gè)用于創(chuàng)建對(duì)象的類,該類接受一個(gè)參數(shù),通過參數(shù)決定創(chuàng)建不同的對(duì)象。
GOF并沒有把簡(jiǎn)單工廠方法定義為23種設(shè)計(jì)模式之一,可以認(rèn)為簡(jiǎn)單工廠方法是工廠方法的簡(jiǎn)化形式。
為了體現(xiàn)簡(jiǎn)單工廠方法和工廠方法的區(qū)別和聯(lián)系,此處把簡(jiǎn)單工廠方法先單獨(dú)講一下。
2 模擬場(chǎng)景
假設(shè)你要生產(chǎn)電腦,電腦由硬盤、內(nèi)存條、CPU、主板的部件組成。你為了保證供應(yīng)鏈可靠,每種部件都選擇了至少兩家供應(yīng)商。比如: 硬盤供應(yīng)商 seagate、Toshiba 內(nèi)存條供應(yīng)商 SAMSUNG、Crucial CPU供應(yīng)商 intel、AMD 主板供應(yīng)商 intel、AMD 此處列出多個(gè)部件是為了后面講解工廠方法、抽象工廠方法時(shí)使用同一個(gè)模擬場(chǎng)景。本章講簡(jiǎn)單工廠方法暫時(shí)不需要涉及這么多部件,所以僅以硬盤這一個(gè)部件為例進(jìn)行講解。
3 實(shí)現(xiàn)的思路
硬盤就是要?jiǎng)?chuàng)建的對(duì)象(即:產(chǎn)品)。為了讓不同供應(yīng)商提供的硬盤可以通用,要定義一個(gè)硬盤產(chǎn)品類,并讓不同供應(yīng)商的硬盤都繼承硬盤產(chǎn)品類的接口。
還需要定義一個(gè)創(chuàng)建硬盤對(duì)象的類(即:工廠)。工廠類根據(jù)參數(shù)決定創(chuàng)建哪家供應(yīng)商的硬盤對(duì)象。
4 實(shí)現(xiàn)硬盤對(duì)象創(chuàng)建
參與者: (1)Product: HardDisk 定義硬盤對(duì)象的接口 (2)Concrete Product: SeagateHardDisk, ToshibaHardDisk 實(shí)現(xiàn)不同供應(yīng)商的硬盤 (3)SimpleFactory: HardDiskFactory 根據(jù)參數(shù),創(chuàng)建不同供應(yīng)商的硬盤對(duì)象
UML:
HardDisk代碼示例:
hard_disk.h:
#ifndef HARD_DISK_H
#define HARD_DISK_H
struct HardDisk {
void (*Operation)(struct HardDisk *this);
};
#endif
SeagateHardDisk代碼示例:seagate_hard_disk.h:
#ifndef SEAGATE_HARD_DISK_H
#define SEAGATE_HARD_DISK_H
#include "hard_disk.h"
struct SeagateHardDisk {
struct HardDisk hardDisk;
};
// 構(gòu)造函數(shù)
void SeagateHardDisk(struct SeagateHardDisk *this);
// 析構(gòu)函數(shù)
void _SeagateHardDisk(struct SeagateHardDisk *this);
#endif
seagate_hard_disk.c:
#include "seagate_hard_disk.h"
#include "stdio.h"
void SeagateOperation(struct SeagateHardDisk *this)
{
printf("這是 Seagate 硬盤
");
}
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.h"
struct ToshibaHardDisk {
struct HardDisk hardDisk;
};
// 構(gòu)造函數(shù)
void ToshibaHardDisk(struct ToshibaHardDisk *this);
// 析構(gòu)函數(shù)
void _ToshibaHardDisk(struct ToshibaHardDisk *this);
#endif
toshiba_hard_disk.c:
#include "toshiba_hard_disk.h"
#include "stdio.h"
void ToshibaOperation(struct ToshibaHardDisk *this)
{
printf("這是 Toshiba 硬盤
");
}
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.h"
enum 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);
};
// 構(gòu)造函數(shù)
void HardDiskFactory(struct HardDiskFactory *this);
// 析構(gòu)函數(shù)
void _HardDiskFactory(struct HardDiskFactory *this);
#endif
hard_disk_factory.c:
#include "hard_disk_factory.h"
#include "seagate_hard_disk.h"
#include "toshiba_hard_disk.h"
#include "stdio.h"
#include "stdlib.h"
struct 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
");
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
");
return NULL;
}
ToshibaHardDisk(toshibaHardDisk);
return (struct HardDisk *)toshibaHardDisk;
}
default:
printf("未知的供應(yīng)商
");
return NULL;
}
}
void Destroy(struct HardDiskFactory *this, struct HardDisk* hardDisk)
{
if (hardDisk != NULL) {
free(hardDisk);
}
}
// 構(gòu)造函數(shù)
void HardDiskFactory(struct HardDiskFactory *this)
{
this->Create = Create;
this->Destroy = Destroy;
}
// 析構(gòu)函數(shù)
void _HardDiskFactory(struct HardDiskFactory *this)
{
this->Create = NULL;
this->Destroy = NULL;
}
客戶端代碼示例:
#include "hard_disk.h"
#include "hard_disk_factory.h"
#include "stddef.h"
void main()
{
struct HardDisk *hardDisk = NULL;
struct HardDiskFactory hardDiskFactory;
HardDiskFactory(&hardDiskFactory);
// 創(chuàng)建 seagate 硬盤對(duì)象
hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_SEAGATE);
// 使用 seagate 硬盤對(duì)象
hardDisk->Operation(hardDisk);
// 銷毀 seagate 硬盤對(duì)象
hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);
// 創(chuàng)建 toshiba 硬盤對(duì)象
hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_TOSHIBA);
// 使用 seagate 硬盤對(duì)象
hardDisk->Operation(hardDisk);
// 銷毀 toshiba 硬盤對(duì)象
hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);
_HardDiskFactory(&hardDiskFactory);
}
客戶端顯示示例:
./hard_disk 這是 Seagate 硬盤 這是 Toshiba 硬盤
審核編輯:湯梓紅
-
amd
+關(guān)注
關(guān)注
25文章
5684瀏覽量
139983 -
cpu
+關(guān)注
關(guān)注
68文章
11279瀏覽量
225022 -
接口
+關(guān)注
關(guān)注
33文章
9521瀏覽量
157048 -
硬盤
+關(guān)注
關(guān)注
3文章
1361瀏覽量
59895 -
C語言
+關(guān)注
關(guān)注
183文章
7644瀏覽量
145609
發(fā)布評(píng)論請(qǐng)先 登錄
單片機(jī)C語言編程實(shí)例
單片機(jī)C語言實(shí)例有哪些
C語言與MATLAB接口編程與實(shí)例
單片機(jī)C語言編程與實(shí)例
C語言程序設(shè)計(jì)及應(yīng)用實(shí)例
C語言入門教程之C語言編程實(shí)例源代碼資料免費(fèi)下載
使用單片機(jī)點(diǎn)亮多個(gè)LED燈的方法C語言程序實(shí)例免費(fèi)下載
設(shè)計(jì)模式:簡(jiǎn)單工廠模式——基于C語言
C語言簡(jiǎn)單工廠方法實(shí)例
評(píng)論