C++編程中使用設(shè)計模式中的policy策略模式的實(shí)例講解
在看《C++設(shè)計新思維》的時候,發(fā)現(xiàn)在一開始就大篇幅的介紹策略模式(policy),策略模式不屬于經(jīng)典設(shè)計模式中的一種,但是其實(shí)在我們?nèi)粘5拈_發(fā)中是必不可少的。policy,策略,方針,這里的意思是指把復(fù)雜功能的類盡量的拆分為功能單一的簡單類的組合,簡單的類只負(fù)責(zé)單純行為或結(jié)構(gòu)的某一方面。增加程序庫的彈性,可復(fù)用性,可擴(kuò)展性。policy是一個虛擬的概念,他定義了某一類class的一些接口規(guī)范,并不與C++語法的關(guān)鍵字對應(yīng),只是一個抽象的概念。
實(shí)例1:
//policy模式的常見使用實(shí)例smartptr, template < class T, template <class> class CheckingPolicy, template <class> class ThreadingModel > class SmartPtr : public CheckingPolicy<T> , public ThreadingModel<SmartPtr> { T* operator->() { typename ThreadingModel<SmartPtr>::Lock guard(*this); CheckingPolicy<T>::Check(pointee_); return pointee_; } private: T* pointee_; };
實(shí)例2,比如說:我們定義一個policy,他是一個帶有參數(shù)T的一個模版,他必須有一個Create函數(shù),且返回T類型指針。對于這個定義,我們可以有不同的實(shí)現(xiàn),從而滿足不同用戶的不同的需求。
template <class T> struct OpNewCreator { static T* Create() { return new T; } }; template <class T> struct MallocCreator { static T* Create() { void* buf = std::malloc(sizeof(T)); if (!buf) return 0; return new(buf) T; } }; template <class T> struct PrototypeCreator { PrototypeCreator(T* pObj = 0) :pPrototype_(pObj) {} T* Create() { return pPrototype_ ? pPrototype_->Clone() : 0; } T* GetPrototype() { return pPrototype_; } void SetPrototype(T* pObj) { pPrototype_ = pObj; } private: T* pPrototype_; }; //test class class Widget { }; //調(diào)用方法一: template <class CreationPolicy> class WidgetManager : public CreationPolicy { }; void main() { typedef WidgetManager< OpNewCreator<Widget> > MyWidgetMgr; } //調(diào)用方法二:因為一般Manager是特定于某一類的class,所以在Manager中就指定要處理的class類型。 template <template <class Created> class CreationPolicy> class WidgetManager : public CreationPolicy<Widget> { }; void main() { // Application code typedef WidgetManager<OpNewCreator> MyWidgetMgr; }
對于上面一個策略有3中不同的實(shí)現(xiàn),從而就可以滿足不同的客戶的需求。
但是對于上面的使用,我們還可以有更好的修改:因為Policy的實(shí)現(xiàn)class一般會被繼承,所以我們要考慮他的析構(gòu),一般的我們使析構(gòu)函數(shù)virtual,但是這里會影響template的靜態(tài)編譯特性,影響效率,所以我們使用protected或private的析構(gòu)函數(shù),既不影響繼承類對基類的析構(gòu),也不影響使用。
如修改如下:
template <class T> struct OpNewCreator { static T* Create() { return new T; } protected: ~OpNewCreator() {} };
我們還可以修改上面的manger,實(shí)現(xiàn)creator policy的switch:
template <template <class> class CreationPolicy> class WidgetManager : public CreationPolicy<Widget> { void SwitchPrototype(Widget* pNewPrototype) { CreationPolicy<Widget>& myPolicy = *this; delete myPolicy.GetPrototype(); myPolicy.SetPrototype(pNewPrototype); } };
policy 模式對我們創(chuàng)建可復(fù)用,可擴(kuò)展的庫的開發(fā)有非常重要的作用,是OO的基本的設(shè)原則式之一。
總的說來策略模式:
優(yōu)點(diǎn):
1、 使用策略模式可以避免使用多重條件轉(zhuǎn)移語句。多重轉(zhuǎn)移語句不易維護(hù)。
2、 策略模式讓你可以動態(tài)的改變對象的行為,動態(tài)修改策略
缺點(diǎn):
1、客戶端必須知道所有的策略類,并自行決定使用哪一個策略類。
2、類過多---策略模式造成很多的策略類,每個具體策略類都會產(chǎn)生一個新類。(這點(diǎn)可以通過享元模式來克服類過多)
相關(guān)文章
C++實(shí)現(xiàn)棧的操作(push和pop)
這篇文章主要介紹了C++實(shí)現(xiàn)棧的操作(push和pop),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件操作)
這篇文章主要介紹了C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),增加了文件操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06C語言創(chuàng)建數(shù)組實(shí)現(xiàn)函數(shù)init,empty,reverse
這篇文章主要介紹了C語言創(chuàng)建數(shù)組實(shí)現(xiàn)函數(shù)init,empty,reverse,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07