設(shè)計模式_工廠方法模式_C語言實(shí)現(xiàn)
簡介
我們接簡單工廠方法模式_C語言實(shí)現(xiàn)中生產(chǎn)電腦的模擬場景來看。
在簡單工廠方法中,不同供應(yīng)商的硬盤都由HardDiskFactory創(chuàng)建出來。這種做法存在幾個問題:
1、當(dāng)供應(yīng)商很多時,HardDiskFactory就會成為一個“過大類”
2、假設(shè)現(xiàn)在要新引入一家硬盤供應(yīng)商,那就需要在HardDiskFactory的Create方法中增加一個case分支。在Create方法中增加case分支的做法違反了“開閉原則”。
我們現(xiàn)在要講的“工廠方法”可以解決上述問題:
工廠方法針對每一個供應(yīng)商的硬盤提供一個工廠。通過不同工廠對象來創(chuàng)建不同的產(chǎn)品對象。也就是說,工廠方法不直接定義用于創(chuàng)建對象的類,而是定義一個創(chuàng)建硬盤對象的接口,讓子類決定實(shí)例化哪一個供應(yīng)商的硬盤。
當(dāng)新增一個硬盤供應(yīng)商時,只需要新增一個工廠對象。不需要在原有工廠中增加代碼,所以不違反“開閉原則”,也不會因?yàn)椴粩嘣黾庸?yīng)商而產(chǎn)生“過大類”。
硬盤對象創(chuàng)建
參與者
1、Product: HardDisk
定義硬盤對象的接口(與簡單工廠方法相同)
2、Concrete Product: SeagateHardDisk, ToshibaHardDisk
實(shí)現(xiàn)不同供應(yīng)商的硬盤(與簡單工廠方法相同)
3、Factory: HardDiskFactory
聲明硬盤工廠方法,具體硬盤對象的創(chuàng)建交給其子類 ConcreteFactory
4、ConcreteFactory: SeagateHardDiskFactory, ToshibaHardDiskFactory
創(chuàng)建具體硬盤對象
UML
HardDisk、SeagateHardDisk、ToshibaHardDisk示例代碼與簡單工廠方法完全一致,此處不再重復(fù)
HardDiskFactory代碼示例:
hard_disk_factory.h:
#ifndef HARD_DISK_FACTORY_H #define HARD_DISK_FACTORY_H #include "hard_disk.h" struct HardDiskFactory { struct HardDisk* (*Create)(struct HardDiskFactory *this); void (*Destroy)(struct HardDiskFactory *this,
struct HardDisk* hardDisk);
}; #endif
SeagateHardDiskFactory代碼示例:
seagate_hard_disk_factory.h:
#define SEAGATE_HARD_DISK_FACTORY_H #include "hard_disk_factory.h" struct SeagateHardDiskFactory { struct HardDiskFactory hardDiskFactory; }; // 構(gòu)造函數(shù) void SeagateHardDiskFactory(struct SeagateHardDiskFactory *this); // 析構(gòu)函數(shù) void _SeagateHardDiskFactory(struct SeagateHardDiskFactory *this); #endif
seagate_hard_disk_factory.c:
#include "seagate_hard_disk_factory.h" #include "seagate_hard_disk.h" #include "stdio.h" #include "stdlib.h" struct HardDisk* SeagateCreate(struct HardDiskFactory *this) { struct SeagateHardDisk *seagateHardDisk = NULL; if ((seagateHardDisk = malloc(sizeof(struct SeagateHardDisk))) == NULL) { printf("fail in malloc\n"); return NULL;
}
SeagateHardDisk(seagateHardDisk); return (struct HardDisk*)seagateHardDisk;
} void SeagateDestroy(struct HardDiskFactory *this, struct HardDisk *hardDisk) { if (hardDisk != NULL) { free(hardDisk);
hardDisk = NULL;
}
} // 構(gòu)造函數(shù) void SeagateHardDiskFactory(struct SeagateHardDiskFactory *this) { this->hardDiskFactory.Create = SeagateCreate; this->hardDiskFactory.Destroy = SeagateDestroy;
} // 析構(gòu)函數(shù) void _SeagateHardDiskFactory(struct SeagateHardDiskFactory *this)
{ this->hardDiskFactory.Create = NULL; this->hardDiskFactory.Destroy = NULL;
}
ToshibaHardDiskFactory代碼示例:
toshiba_hard_disk_factory.h:
#ifndef TOSHIBA_HARD_DISK_FACTORY_H #define TOSHIBA_HARD_DISK_FACTORY_H #include "hard_disk_factory.h" struct ToshibaHardDiskFactory { struct HardDiskFactory hardDiskFactory; }; // 構(gòu)造函數(shù) void ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this); // 析構(gòu)函數(shù) void _ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this); #endif
toshiba_hard_disk_factory.c:
#include "toshiba_hard_disk_factory.h" #include "toshiba_hard_disk.h" #include "stdio.h" #include "stdlib.h" struct HardDisk* ToshibaCreate(struct HardDiskFactory *this) { struct ToshibaHardDisk *toshibaHardDisk = NULL; if ((toshibaHardDisk = malloc(sizeof(struct ToshibaHardDisk))) == NULL) { printf("fail in malloc\n"); return NULL;
}
ToshibaHardDisk(toshibaHardDisk); return (struct HardDisk*)toshibaHardDisk;
} void ToshibaDestroy(struct HardDiskFactory *this, struct HardDisk *hardDisk) { if (hardDisk != NULL) { free(hardDisk);
hardDisk = NULL;
}
} // 構(gòu)造函數(shù) void ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this) { this->hardDiskFactory.Create = ToshibaCreate; this->hardDiskFactory.Destroy = ToshibaDestroy;
} // 析構(gòu)函數(shù) void _ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this)
{ this->hardDiskFactory.Create = NULL; this->hardDiskFactory.Destroy = NULL;
}
客戶端代碼示例:
#include "hard_disk.h" #include "hard_disk_factory.h" #include "seagate_hard_disk_factory.h" #include "toshiba_hard_disk_factory.h" #include "stddef.h" void main() { struct HardDisk *hardDisk = NULL; struct HardDiskFactory *hardDiskFactory; struct SeagateHardDiskFactory seagateHardDiskFactory; SeagateHardDiskFactory(&seagateHardDiskFactory);
hardDiskFactory = (struct HardDiskFactory *)&seagateHardDiskFactory; // 創(chuàng)建 seagate 硬盤對象 hardDisk = hardDiskFactory->Create(hardDiskFactory); // 使用 seagate 硬盤對象 hardDisk->Operation(hardDisk); // 銷毀 seagate 硬盤對象 hardDiskFactory->Destroy(hardDiskFactory, hardDisk);
_SeagateHardDiskFactory(&seagateHardDiskFactory); struct ToshibaHardDiskFactory toshibaHardDiskFactory; ToshibaHardDiskFactory(&toshibaHardDiskFactory);
hardDiskFactory = (struct HardDiskFactory *)&toshibaHardDiskFactory; // 創(chuàng)建 toshiba 硬盤對象 hardDisk = hardDiskFactory->Create(hardDiskFactory); // 使用 seagate 硬盤對象 hardDisk->Operation(hardDisk); // 銷毀 toshiba 硬盤對象 hardDiskFactory->Destroy(hardDiskFactory, hardDisk);
_ToshibaHardDiskFactory(&toshibaHardDiskFactory);
}
客戶端顯示示例:
./hard_disk
這是 Seagate 硬盤
這是 Toshiba 硬盤
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點(diǎn),不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!