首页 > 其他 > 详细

初学设计模式之策略模式

时间:2019-11-10 01:15:32      阅读:122      评论:0      收藏:0      [点我收藏+]

策略模式的代码例子

 1 //策略模式
 2 #include<iostream>
 3 using namespace std;
 4 
 5 class strategy
 6 {
 7 public:
 8 virtual void Do()
 9 {};
10 };
11 class strategy1:public strategy
12 {
13 public:
14     void Do()
15     {
16         cout<<"策略1"<<endl;
17     }
18 };
19 class strategy2:public strategy
20 {
21 public:
22     void Do()
23     {
24         cout<<"策略2"<<endl;
25     }
26 };
27 class Context
28 {
29 private:
30     strategy *m_strategyPtr;
31 public:
32     Context(strategy *_strategyPtr)
33     {
34         m_strategyPtr=_strategyPtr;
35     };
36     void exec()
37     {
38         m_strategyPtr->Do();
39     };
40 };
41 int main()
42 {
43    strategy1 m_Strategy1;
44    Context m_Context(&m_Strategy1);
45    m_Context.exec();
46    strategy1 m_Strategy2;
47     Context m_Context2(&m_Strategy2);
48    m_Context2.exec();
49    getchar();
50    return 0;
51 };

 

初学设计模式之策略模式

原文:https://www.cnblogs.com/wuhongjian/p/11828459.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!