C++编程中使用设计模式中的policy策略模式的实例讲解

2025-05-29 0 59

在看《C++设计新思维》的时候,发现在一开始就大篇幅的介绍策略模式policy),策略模式不属于经典设计模式中的一种,但是其实在我们日常的开发中是必不可少的。policy,策略,方针,这里的意思是指把复杂功能的类尽量的拆分为功能单一的简单类的组合,简单的类只负责单纯行为或结构的某一方面。增加程序库的弹性,可复用性,可扩展性。policy是一个虚拟的概念,他定义了某一类class的一些接口规范,并不与C++语法的关键字对应,只是一个抽象的概念。

实例1:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
//policy模式的常见使用实例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_;

};

实例2,比如说:我们定义一个policy,他是一个带有参数T的一个模版,他必须有一个Create函数,且返回T类型指针。对于这个定义,我们可以有不同的实现,从而满足不同用户的不同的需求。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64
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

{

};

//调用方法一:

template <class CreationPolicy>

class WidgetManager : public CreationPolicy

{

};

void main()

{

typedef WidgetManager< OpNewCreator<Widget> > MyWidgetMgr;

}

//调用方法二:因为一般Manager是特定于某一类的class,所以在Manager中就指定要处理的class类型。

template <template <class Created> class CreationPolicy>

class WidgetManager : public CreationPolicy<Widget>

{

};

void main()

{

// Application code

typedef WidgetManager<OpNewCreator> MyWidgetMgr;

}

对于上面一个策略有3中不同的实现,从而就可以满足不同的客户的需求。
但是对于上面的使用,我们还可以有更好的修改:因为Policy的实现class一般会被继承,所以我们要考虑他的析构,一般的我们使析构函数virtual,但是这里会影响template的静态编译特性,影响效率,所以我们使用protected或private的析构函数,既不影响继承类对基类的析构,也不影响使用。
如修改如下:

?

1

2

3

4

5

6

7

8

9

10
template <class T>

struct OpNewCreator

{

static T* Create()

{

return new T;

}

protected:

~OpNewCreator() {}

};

我们还可以修改上面的manger,实现creator policy的switch:

?

1

2

3

4

5

6

7

8

9

10
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 模式对我们创建可复用,可扩展的库的开发有非常重要的作用,是OO的基本的设原则式之一。

总的说来策略模式
优点:

1、 使用策略模式可以避免使用多重条件转移语句。多重转移语句不易维护。
2、 策略模式让你可以动态的改变对象的行为,动态修改策略

缺点:
1、客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
2、类过多—策略模式造成很多的策略类,每个具体策略类都会产生一个新类。(这点可以通过享元模式来克服类过多)

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++编程中使用设计模式中的policy策略模式的实例讲解 https://www.kuaiidc.com/106214.html

相关文章

发表评论
暂无评论