C++設計模式之工廠模式(1)

關于設計模式的作用:

幫助我們將應用組織成容易了解,容易維護,具有彈性的架構,建立可維護的OO系統,要訣在于隨時想到系統以后可能需要的變化以及應付變化的原則。

具體可參考:https://www.cnblogs.com/linkstar/p/7859980.html

由于Caffe中的solver產生一系列的solver子類以及建立一系列layer類時就用到了工廠模式(參考:https://blog.csdn.net/fangjin_kl/article/details/54019066)。所以這里首先介紹一下第一種設計模式-----工廠模式

參考博客:https://blog.csdn.net/wuzhekai1985/article/details/6660462

工廠模式有三種:

(1)簡單工廠模式

(2)工廠方法模式

(3)抽象工廠模式

1、簡單工廠模式

這么描述:現在有一個工廠Factory類,專門生成核core,現在要生產兩種產品core_A 和 core_B,代碼如下:

enum CTYPE {COREA, COREB};     
class SingleCore    
{    
public:    virtual void Show() = 0;  
};    
//單核A    
class SingleCoreA: public SingleCore    
{    
public:    void Show() { cout<<"SingleCore A"<<endl; }    
};    
//單核B    
class SingleCoreB: public SingleCore    
{    
public:    void Show() { cout<<"SingleCore B"<<endl; }    
};    
//唯一的工廠,可以生產兩種型號的處理器核,在內部判斷    
class Factory    
{    
public:     SingleCore* CreateSingleCore(enum CTYPE ctype)    {    if(ctype == COREA) //工廠內部判斷    return new SingleCoreA(); //生產核A    else if(ctype == COREB)    return new SingleCoreB(); //生產核B    else    return NULL;    }    
};    

?

當然這么做也有一個缺點:當Factory要生產一個新的產品時,就要修改factory類,然而這樣就違背了開放封閉原則,即軟件實體(類、函數、模塊)不可修改,只能擴展;所以為了通過擴展來實現可以生產新的產品,引入了工廠方法模式;

2、工廠方法模式

這么來描述:通過類的繼承來實現擴展功能,即從父類factory中派生出factory_A類專門用來生產core_A 產品 , 派生出favtory_B類從來生產core_B產品。這樣子如果味蕾要生產新的產品時,只需要派生出新的工廠子類就行了。代碼如下:

class SingleCore    
{    
public:    virtual void Show() = 0;  
};    
//單核A    
class SingleCoreA: public SingleCore    
{    
public:    void Show() { cout<<"SingleCore A"<<endl; }    
};    
//單核B    
class SingleCoreB: public SingleCore    
{    
public:    void Show() { cout<<"SingleCore B"<<endl; }    
};    
class Factory    
{    
public:    virtual SingleCore* CreateSingleCore() = 0;  
};    
//生產A核的工廠    
class FactoryA: public Factory    
{    
public:    SingleCoreA* CreateSingleCore() { return new SingleCoreA; }    
};    
//生產B核的工廠    
class FactoryB: public Factory    
{    
public:    SingleCoreB* CreateSingleCore() { return new SingleCoreB; }    
};    

?

當然上面這么做也有一個缺點,就是每生產一種新的類別時就需要建立一個新的工廠,那么就存在這樣一種情況,依然core_A類,可能我們要對A生產一系列產品,比如單核的A和多核的A,如果還是用工廠方法模式就太繁瑣了,所以引入了抽象工廠模式;

3、抽象工廠模式

這么描述:它提供了一個創建一系列相關或相互依賴對象的接口,而無需指定它們具體的類,就是說父類factory只指定要生產單核類和多核類,而不需要指定A、B、C類等等。

代碼如下:

//單核    
class SingleCore     
{    
public:    virtual void Show() = 0;  
};    
class SingleCoreA: public SingleCore      
{    
public:    void Show() { cout<<"Single Core A"<<endl; }    
};    
class SingleCoreB :public SingleCore    
{    
public:    void Show() { cout<<"Single Core B"<<endl; }    
};    
//多核    
class MultiCore      
{    
public:    virtual void Show() = 0;  
};    
class MultiCoreA : public MultiCore      
{    
public:    void Show() { cout<<"Multi Core A"<<endl; }    };    
class MultiCoreB : public MultiCore      
{    
public:    void Show() { cout<<"Multi Core B"<<endl; }    
};    
//工廠    
class CoreFactory      
{    
public:    virtual SingleCore* CreateSingleCore() = 0;  //只指定要生產單核類和多核類,而不需要指定要生產A還是b類等等virtual MultiCore* CreateMultiCore() = 0;  
};    
//工廠A,專門用來生產A型號的處理器    
class FactoryA :public CoreFactory    
{    
public:    SingleCore* CreateSingleCore() { return new SingleCoreA(); }    MultiCore* CreateMultiCore() { return new MultiCoreA(); }    
};    
//工廠B,專門用來生產B型號的處理器    
class FactoryB : public CoreFactory    
{    
public:    SingleCore* CreateSingleCore() { return new SingleCoreB(); }    MultiCore* CreateMultiCore() { return new MultiCoreB(); }    
};   

?

轉載于:https://www.cnblogs.com/zf-blog/p/9120628.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280193.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280193.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280193.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

共享沒有權限訪問權限_如何與家人共享SmartThings訪問權限

共享沒有權限訪問權限If you have multiple people in your household and want them all to have access to SmartThings from their phones, here’s how to share access to SmartThings with anyone you want. 如果您的家庭中有多個人&#xff0c;并且希望他們所有人都可以…

PABX

自動用戶小交換機;&#xff3b;私用自動交換分機&#xff3d; A private telephone exchange that automatically connects internal “branch” lines to the external circuits of a telephone system. 一種自動地將內部用戶線連接到電話系統外線的專用電話交換機。 Private …

使用jquery+css實現瀑布流布局

雖然可以直接使用css實現瀑布流布局&#xff0c;但顯示的方式有點問題&#xff0c;所以這兒就直接使用jquerycss來實現瀑布流布局&#xff0c;最終效果如下&#xff1a; 思路是通過將每個小塊的position設置為relative&#xff0c;然后計算出在當前選擇的列下應該上移的距離&am…

geek_How-To Geek正在尋找安全作家

geekThink you have the perfect combination of geek knowledge and writing skills? We’re looking for an experienced, security-focused writer to join our team. 認為您將怪胎知識和寫作技能完美結合了嗎&#xff1f; 我們正在尋找經驗豐富&#xff0c;注重安全性的作…

AAC 文件解析及解碼流程

OUTLINE&#xff1a; &#xff0a; AAC概述 &#xff0a; AAC規格簡述 &#xff0a; AAC特點 &#xff0a; AAC音頻文件解析 ——ADIF&#xff06;ADTS格式 ——ADIF&#xff06;ADTS頭信息 ——ADIF&#xff06;ADTS數據信息 ——AAC文件處理流程 &#xff0a; AAC解碼流程…

JDK8之Stream新特性

/***JDK8 Stream特性* Created by chengbx on 2018/5/27.* Java 8 中的 Stream 是對集合&#xff08;Collection&#xff09;對象功能的增強&#xff0c;它專注于對集合對象進行各種非常便利、高效的聚合操作&#xff08;aggregate operation&#xff09;&#xff0c;* 或者大…

雞蛋學運維-2:Rsync同步配置步驟

說明&#xff1a;系統環境CentOS release 6.5 (Final) 2.6.32-431.el6.x86_64rsync server:配置步驟1、vi /etc/rsyncd.conf#Rsync server#created by lijianfeng 18:26 2017-9-24#rsyncd.conf start#uid rsyncgid rsyncuse chroot nomax connections 2000timeout 600pid…

IntelliJ IDEA代碼分屏顯示

轉載于:https://www.cnblogs.com/EasonJim/p/9124809.html

vscode重置應用程序_如何在Windows 10上重置應用程序的數據

vscode重置應用程序With Windows 10’s Anniversary Update, you can now reset an app’s data without actually uninstalling and reinstalling the app. This can fix problems when an app has gotten into a bad state, or just quickly restore an app to its default s…

程序報錯與提示

2019獨角獸企業重金招聘Python工程師標準>>> 我們在開發中, 為了程序的規范性,把報錯級別,調的比較高NOTICE級別的也報出來,有助于我們快速定位錯誤和代碼規范&#xff0c;但是,在產品上線后,網站運營過程中,就不宜報這么多錯. 1:這種錯誤給客戶的印象不好 2:在報錯…

【codevs1230】元素查找

problem 給出n個正整數&#xff0c;然后有m個詢問詢問該整數是否在n個正整數中出現過solution 哈希表&#xff1f; 當然是set水洛 codes #include<iostream> #include<set> using namespace std; set<int>s; int main(){int n, m;cin>>n>>m;for…

dock怎么自定義_如何自定義和調整Mac的Dock

dock怎么自定義The macOS dock normally appears at the bottom of your screen, but it doesn’t have to. The dock is customizable in quite a few ways you might not be aware of, especially if you’re a new Mac user. macOS塢站通常顯示在屏幕底部&#xff0c;但不是…

ios 輕掃手勢_輕掃即可快速刪除iOS計算器中的數字

ios 輕掃手勢iOS’ built-in calculator is a basic, simple-to-use calculator that’s very handy for doing some quick calculations, such as calculating the tip on your restaurant bill. It’s also useful for longer, more complicated calculations. However, ther…

游戲安全資訊精選 2017年第十期 英國彩票網遭遇DDoS攻擊,中斷90分鐘 DNSMASQ多高危漏洞公告 阿里云協助警方破獲國內最大黑客攻擊案,攻擊峰值690G...

【本周游戲行業DDoS攻擊態勢】 國慶期間&#xff0c;針對游戲行業的DDoS攻擊放緩&#xff0c;攻擊者也在放“小長假”&#xff0c;10月8日超過500G的攻擊可視作攻擊猛烈度恢復的表現。 【游戲安全動態】 英國彩票網遭遇DDoS攻擊&#xff0c;中斷90分鐘 點擊查看原文 點評&#…

02 jmeter 簡單發送http請求

一、新建http請求模板1、測試計劃2、右鍵Threads(users)-線程組3、右鍵sample-http請求4、右鍵監聽器-查看結果樹5、右鍵監聽器-查看聚合報告二、編輯http請求內容三、設置并發用戶1&#xff1a;虛擬用戶數&#xff1b; 2&#xff1a;加載用戶時間&#xff1b;3、每個用戶循環次…

java調用siri 語言_如何更改Siri的聲音,口音,性別和語言

java調用siri 語言Most of us are familiar with Siri as an American female voice. What you may not realize is that you can actually change Siri to have a different accent, gender, and language. 我們大多數人都熟悉Siri&#xff0c;這是一種美國女性聲音。 您可能沒…

高手與菜鳥,思想與技術

這是個嚴肅的話題。同樣的問題&#xff0c;高手和菜鳥的看法是不同&#xff0c;怎么樣不同呢&#xff1f;我們是高手還菜鳥呢&#xff1f;看看以下問題&#xff1a;對于AJAX&#xff1a;菜鳥看到的是一種新技術&#xff0c;趨之若騖&#xff1b;高手看到的是javascript的一種巧…

玩轉 React(四)- 創造一個新的 HTML 標簽

在第二篇文章 《新型前端開發方式》 中有說到 React 有很爽的一點就是給我們一種創造 HTML 標簽的能力&#xff0c;那么今天這篇文章就詳細講解下 React 是如何提供這種能力的&#xff0c;作為前端開發者如何來運用這種能力。 在第三篇文章 《JavaScript代碼里寫HTML一樣可以很…

mac word 設置語言_如何更改Mac的語言和區域設置

mac word 設置語言If you want to use your Mac in a different language, or you’re live in a different region, then you can change it in OS X. When you do, it’ll display everything in your preferred language, currency, date format, and more. 如果您想以其他語…

【Luogu3931】SAC E#1 - 一道難題 Tree

problem solution codes //樹形DP //f[u]:割掉u和u子樹中所有的葉子節點所需要的最小代價 #include<iostream> #include<vector>using namespace std; typedef long long LL; const int N (int)1e510, inf 1e9;int n, S;struct node{LL to, v;node(LL to, LL v):…