c++?創(chuàng)建型設(shè)計(jì)模式工廠方法Factory?Method示例詳解
簡(jiǎn)介
工廠方法中,每一個(gè)具體工廠類(lèi)都對(duì)應(yīng)創(chuàng)建一個(gè)具體產(chǎn)品類(lèi),所有具體工廠類(lèi)都實(shí)現(xiàn)抽象工廠,所有具體產(chǎn)品類(lèi)都實(shí)現(xiàn)抽象產(chǎn)品。
抽象工廠定義了創(chuàng)建抽象產(chǎn)品的方法簽名,具體工廠類(lèi)各自實(shí)現(xiàn)各自邏輯,來(lái)創(chuàng)建具體的產(chǎn)品。
角色
抽象工廠 Abstract Factory
定義創(chuàng)建產(chǎn)品的方法簽名,即Factory Method
抽象產(chǎn)品 Abstract Product
定義產(chǎn)品的基本屬性
具體工廠 Concrete Factory
實(shí)現(xiàn)自抽象工廠,并實(shí)現(xiàn) Factory Method,實(shí)現(xiàn)如何創(chuàng)建具體產(chǎn)品。
具體產(chǎn)品 Concrete Product
實(shí)現(xiàn)具體產(chǎn)品基本屬性
類(lèi)圖
如圖所示,Dialog抽象工廠可以創(chuàng)建Button抽象產(chǎn)品,WindowsDialog和WebDialog都是具體工廠,負(fù)責(zé)創(chuàng)建WindownsButton和HTMLButton。
代碼
abstract class Creator { abstract public function factoryMethod(): Product; public function someOperation(): string { $product = $this->factoryMethod(); $result = "Creator: The same creator's code has just worked with " . $product->operation(); return $result; } } class ConcreteCreator1 extends Creator { public function factoryMethod(): Product { return new ConcreteProduct1(); } } class ConcreteCreator2 extends Creator { public function factoryMethod(): Product { return new ConcreteProduct2(); } } interface Product { public function operation(): string; } class ConcreteProduct1 implements Product { public function operation(): string { return "{Result of the ConcreteProduct1}"; } } class ConcreteProduct2 implements Product { public function operation(): string { return "{Result of the ConcreteProduct2}"; } } function clientCode(Creator $creator) { echo "Client: I'm not aware of the creator's class, but it still works.\n" . $creator->someOperation() . "\n"; } echo "App: Launched with the ConcreteCreator1.\n"; clientCode(new ConcreteCreator1()); echo "App: Launched with the ConcreteCreator2.\n"; clientCode(new ConcreteCreator2());
output
App: Launched with the ConcreteCreator1.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of the ConcreteProduct1}
App: Launched with the ConcreteCreator2.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of the ConcreteProduct2}
以上就是c++ 創(chuàng)建型設(shè)計(jì)模式工廠方法Factory Method示例詳解的詳細(xì)內(nèi)容,更多關(guān)于c++ Factory Method的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實(shí)現(xiàn)LeetCode(37.求解數(shù)獨(dú))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(37.求解數(shù)獨(dú)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++?ffmpeg實(shí)現(xiàn)將視頻幀轉(zhuǎn)換成jpg或png等圖片
有時(shí)播放實(shí)時(shí)流的時(shí)候有截圖的需求,需要將解碼出來(lái)的圖片保存本地或上傳服務(wù)器,這時(shí)就需要將avframe中的數(shù)據(jù)編碼成png、jpg等格式的圖片,我們使用ffmpeg的相關(guān)編碼器就可以實(shí)現(xiàn)功能,下面就來(lái)講講具體實(shí)現(xiàn)方法吧2023-03-03C++數(shù)據(jù)結(jié)構(gòu)繼承的概念與菱形繼承及虛擬繼承和組合
今天我要給大家介紹C++中更深入的內(nèi)容了。C++這門(mén)語(yǔ)言為了使代碼不冗余,做了些什么操作呢?C++的繼承就很好地實(shí)現(xiàn)了類(lèi)層次的代碼復(fù)用,今天我就要來(lái)和大家好好聊一聊它了2022-02-02詳解C++中動(dòng)態(tài)內(nèi)存管理和泛型編程
這篇文章主要為大家詳細(xì)介紹了C++中動(dòng)態(tài)內(nèi)存管理和泛型編程的相關(guān)資料,文中示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++具有一定幫助,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一2022-10-10