1、簡介
簡單工廠方法定義一個用于創建對象的類,該類接受一個參數,通過參數決定創建不同的對象。
GOF并沒有把簡單工廠方法定義為23種設計模式之一,可以認為簡單工廠方法是工廠方法的簡化形式。
為了體現簡單工廠方法和工廠方法的區別和聯系,此處把簡單工廠方法先單獨講一下。
2、模擬場景
假設你要生產電腦,電腦由硬盤、內存條、CPU、主板的部件組成。你為了保證供應鏈可靠,每種部件都選擇了至少兩家供應商。比如:
硬盤供應商 seagate、Toshiba
內存條供應商 SAMSUNG、Crucial
主板供應商 intel、AMD
此處列出多個部件是為了后面講解工廠方法、抽象工廠方法時使用同一個模擬場景。本章講簡單工廠方法暫時不需要涉及這么多部件,所以僅以硬盤這一個部件為例進行講解。
3、實現的思路
硬盤就是要創建的對象(即:產品)。為了讓不同供應商提供的硬盤可以通用,要定義一個硬盤產品類,并讓不同供應商的硬盤都繼承硬盤產品類的接口。
還需要定義一個創建硬盤對象的類(即:工廠)。工廠類根據參數決定創建哪家供應商的硬盤對象。
4、實現硬盤對象創建
參與者:
(1)Product: HardDisk 定義硬盤對象的接口
(2)Concrete Product: SeagateHardDisk, ToshibaHardDisk 實現不同供應商的硬盤
(3)SimpleFactory: HardDiskFactory 根據參數,創建不同供應商的硬盤對象
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;
};
//構造函數
voidSeagateHardDisk(structSeagateHardDisk*this);
//析構函數
void_SeagateHardDisk(structSeagateHardDisk*this);
#endif
seagate_hard_disk.c:
#include"seagate_hard_disk.h"
#include"stdio.h"
voidSeagateOperation(structSeagateHardDisk*this)
{
printf("這是Seagate硬盤
");
}
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;
};
//構造函數
voidToshibaHardDisk(structToshibaHardDisk*this);
//析構函數
void_ToshibaHardDisk(structToshibaHardDisk*this);
#endif
toshiba_hard_disk.c:
#include"toshiba_hard_disk.h"
#include"stdio.h"
voidToshibaOperation(structToshibaHardDisk*this)
{
printf("這是Toshiba硬盤
");
}
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);
};
//構造函數
voidHardDiskFactory(structHardDiskFactory*this);
//析構函數
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("未知的供應商
");
returnNULL;
}
}
voidDestroy(structHardDiskFactory*this,structHardDisk*hardDisk)
{
if(hardDisk!=NULL){
free(hardDisk);
}
}
//構造函數
voidHardDiskFactory(structHardDiskFactory*this)
{
this->Create=Create;
this->Destroy=Destroy;
}
//析構函數
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);
//創建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硬盤
-
硬盤
+關注
關注
3文章
1317瀏覽量
57494 -
對象
+關注
關注
1文章
38瀏覽量
17419
原文標題:C語言 | 簡單工廠方法模式實現例子
文章出處:【微信號:FANYPCB,微信公眾號:凡億PCB】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論