1、簡(jiǎn)介
簡(jiǎn)單工廠方法定義一個(gè)用于創(chuàng)建對(duì)象的類,該類接受一個(gè)參數(shù),通過(guò)參數(shù)決定創(chuàng)建不同的對(duì)象。
GOF并沒(méi)有把簡(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)電腦,電腦由硬盤(pán)、內(nèi)存條、CPU、主板的部件組成。你為了保證供應(yīng)鏈可靠,每種部件都選擇了至少兩家供應(yīng)商。比如:
硬盤(pán)供應(yīng)商 seagate、Toshiba
內(nèi)存條供應(yīng)商 SAMSUNG、Crucial
主板供應(yīng)商 intel、AMD
此處列出多個(gè)部件是為了后面講解工廠方法、抽象工廠方法時(shí)使用同一個(gè)模擬場(chǎng)景。本章講簡(jiǎn)單工廠方法暫時(shí)不需要涉及這么多部件,所以僅以硬盤(pán)這一個(gè)部件為例進(jìn)行講解。
3、實(shí)現(xiàn)的思路
硬盤(pán)就是要?jiǎng)?chuàng)建的對(duì)象(即:產(chǎn)品)。為了讓不同供應(yīng)商提供的硬盤(pán)可以通用,要定義一個(gè)硬盤(pán)產(chǎn)品類,并讓不同供應(yīng)商的硬盤(pán)都繼承硬盤(pán)產(chǎn)品類的接口。
還需要定義一個(gè)創(chuàng)建硬盤(pán)對(duì)象的類(即:工廠)。工廠類根據(jù)參數(shù)決定創(chuàng)建哪家供應(yīng)商的硬盤(pán)對(duì)象。
4、實(shí)現(xiàn)硬盤(pán)對(duì)象創(chuàng)建
參與者:
(1)Product: HardDisk 定義硬盤(pán)對(duì)象的接口
(2)Concrete Product: SeagateHardDisk, ToshibaHardDisk 實(shí)現(xiàn)不同供應(yīng)商的硬盤(pán)
(3)SimpleFactory: HardDiskFactory 根據(jù)參數(shù),創(chuàng)建不同供應(yīng)商的硬盤(pán)對(duì)象
UML:
HardDisk代碼示例:
hard_disk.h:
#ifndefHARD_DISK_H
#defineHARD_DISK_H
structHardDisk{
void(*Operation)(structHardDisk*this);
};
#endif
SeagateHardDisk代碼示例:
seagate_hard_disk.h:
#ifndefSEAGATE_HARD_DISK_H
#defineSEAGATE_HARD_DISK_H
#include"hard_disk.h"
structSeagateHardDisk{
structHardDiskhardDisk;
};
//構(gòu)造函數(shù)
voidSeagateHardDisk(structSeagateHardDisk*this);
//析構(gòu)函數(shù)
void_SeagateHardDisk(structSeagateHardDisk*this);
#endif
seagate_hard_disk.c:
#include"seagate_hard_disk.h"
#include"stdio.h"
voidSeagateOperation(structSeagateHardDisk*this)
{
printf("這是Seagate硬盤(pán)
");
}
voidSeagateHardDisk(structSeagateHardDisk*this)
{
this->hardDisk.Operation=(void(*)(structHardDisk*))SeagateOperation;
}
void_SeagateHardDisk(structSeagateHardDisk*this)
{
this->hardDisk.Operation=NULL;
}
ToshibaHardDisk代碼示例:
toshiba_hard_disk.h:
#ifndefTOSHIBA_HARD_DISK_H
#defineTOSHIBA_HARD_DISK_H
#include"hard_disk.h"
structToshibaHardDisk{
structHardDiskhardDisk;
};
//構(gòu)造函數(shù)
voidToshibaHardDisk(structToshibaHardDisk*this);
//析構(gòu)函數(shù)
void_ToshibaHardDisk(structToshibaHardDisk*this);
#endif
toshiba_hard_disk.c:
#include"toshiba_hard_disk.h"
#include"stdio.h"
voidToshibaOperation(structToshibaHardDisk*this)
{
printf("這是Toshiba硬盤(pán)
");
}
voidToshibaHardDisk(structToshibaHardDisk*this)
{
this->hardDisk.Operation=(void(*)(structHardDisk*))ToshibaOperation;
}
void_ToshibaHardDisk(structToshibaHardDisk*this)
{
this->hardDisk.Operation=NULL;
}
HardDiskFactory代碼示例:
hard_disk_factory.h:
#ifndefHARD_DISK_FACTORY_H
#defineHARD_DISK_FACTORY_H
#include"hard_disk.h"
enumHARD_DISK_SUPPLIER_E{
HARD_DISK_SUPPLIER_SEAGATE,
HARD_DISK_SUPPLIER_TOSHIBA
};
structHardDiskFactory{
structHardDisk*(*Create)(structHardDiskFactory*this,
enumHARD_DISK_SUPPLIER_Esupplier);
void(*Destroy)(structHardDiskFactory*this,
structHardDisk*hardDisk);
};
//構(gòu)造函數(shù)
voidHardDiskFactory(structHardDiskFactory*this);
//析構(gòu)函數(shù)
void_HardDiskFactory(structHardDiskFactory*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"
structHardDisk*Create(structHardDiskFactory*this,
enumHARD_DISK_SUPPLIER_Esupplier)
{
switch(supplier){
caseHARD_DISK_SUPPLIER_SEAGATE:
{
structSeagateHardDisk*seagateHardDisk=NULL;
if((seagateHardDisk=malloc(sizeof(structSeagateHardDisk)))==NULL){
printf("failinmalloc
");
returnNULL;
}
SeagateHardDisk(seagateHardDisk);
return(structHardDisk*)seagateHardDisk;
}
caseHARD_DISK_SUPPLIER_TOSHIBA:
{
structToshibaHardDisk*toshibaHardDisk=NULL;
if((toshibaHardDisk=malloc(sizeof(structToshibaHardDisk)))==NULL){
printf("failinmalloc
");
returnNULL;
}
ToshibaHardDisk(toshibaHardDisk);
return(structHardDisk*)toshibaHardDisk;
}
default:
printf("未知的供應(yīng)商
");
returnNULL;
}
}
voidDestroy(structHardDiskFactory*this,structHardDisk*hardDisk)
{
if(hardDisk!=NULL){
free(hardDisk);
}
}
//構(gòu)造函數(shù)
voidHardDiskFactory(structHardDiskFactory*this)
{
this->Create=Create;
this->Destroy=Destroy;
}
//析構(gòu)函數(shù)
void_HardDiskFactory(structHardDiskFactory*this)
{
this->Create=NULL;
this->Destroy=NULL;
}
客戶端代碼示例:
#include"hard_disk.h"
#include"hard_disk_factory.h"
#include"stddef.h"
voidmain()
{
structHardDisk*hardDisk=NULL;
structHardDiskFactoryhardDiskFactory;
HardDiskFactory(&hardDiskFactory);
//創(chuàng)建seagate硬盤(pán)對(duì)象
hardDisk=hardDiskFactory.Create(&hardDiskFactory,HARD_DISK_SUPPLIER_SEAGATE);
//使用seagate硬盤(pán)對(duì)象
hardDisk->Operation(hardDisk);
//銷(xiāo)毀seagate硬盤(pán)對(duì)象
hardDiskFactory.Destroy(&hardDiskFactory,hardDisk);
//創(chuàng)建toshiba硬盤(pán)對(duì)象
hardDisk=hardDiskFactory.Create(&hardDiskFactory,HARD_DISK_SUPPLIER_TOSHIBA);
//使用seagate硬盤(pán)對(duì)象
hardDisk->Operation(hardDisk);
//銷(xiāo)毀toshiba硬盤(pán)對(duì)象
hardDiskFactory.Destroy(&hardDiskFactory,hardDisk);
_HardDiskFactory(&hardDiskFactory);
}
客戶端顯示示例:
./hard_disk
這是Seagate硬盤(pán)
這是Toshiba硬盤(pán)
原文標(biāo)題:C語(yǔ)言 | 簡(jiǎn)單工廠方法模式實(shí)現(xiàn)例子
文章出處:【微信公眾號(hào):凡億PCB】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
審核編輯:湯梓紅
-
硬盤(pán)
+關(guān)注
關(guān)注
3文章
1338瀏覽量
58445 -
對(duì)象
+關(guān)注
關(guān)注
1文章
38瀏覽量
17551
原文標(biāo)題:C語(yǔ)言 | 簡(jiǎn)單工廠方法模式實(shí)現(xiàn)例子
文章出處:【微信號(hào):FANYPCB,微信公眾號(hào):凡億PCB】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
抽象工廠模式的創(chuàng)建
如何用java映射創(chuàng)建java對(duì)象和調(diào)用java對(duì)象呢
解讀rtt的c的面向對(duì)象的對(duì)象創(chuàng)建及其啟動(dòng)初始化流程
java如何創(chuàng)建對(duì)象的分析
實(shí)例分析Java中創(chuàng)建對(duì)象
JavaScript創(chuàng)建對(duì)象的方式

使用JavaScript創(chuàng)建對(duì)象的方法和案例
詳解ADI智能工廠方案
在JavaScript中動(dòng)態(tài)的創(chuàng)建QML對(duì)象
使用蜂鳴器創(chuàng)建警報(bào)的最簡(jiǎn)單方法

C語(yǔ)言簡(jiǎn)單工廠方法實(shí)例
C語(yǔ)言簡(jiǎn)單工廠方法編程案例解析
Java反射技術(shù)實(shí)現(xiàn)對(duì)象的創(chuàng)建

評(píng)論