设计模式学习笔记(二):策略模式

《大话设计模式》学习笔记,参考链接 伍迷的小菜编程成长记

  1 //strategy.h
2 #pragma once
3
4 //抽象策略类
5 class CAbstractStrategy
6 {
7 public:
8 CAbstractStrategy(void);
9 ~CAbstractStrategy(void);
10 virtual void AlgorithmOperation(void) = 0;
11 };
12
13 //strategy.cpp
14 #include "strategy.h"
15
16 CAbstractStrategy::CAbstractStrategy(void)
17 {
18 }
19
20 CAbstractStrategy::~CAbstractStrategy(void)
21 {
22 }
23
24 //concretestrategy.h
25 #pragma once
26
27 #include "strategy.h"
28
29 //具体策略A
30 class CConcreteStrategyA : public CAbstractStrategy
31 {
32 public:
33 CConcreteStrategyA(void);
34 ~CConcreteStrategyA(void);
35 void AlgorithmOperation(void);
36 };
37
38 //具体策略B
39 class CConcreteStrategyB : public CAbstractStrategy
40 {
41 public:
42 CConcreteStrategyB(void);
43 ~CConcreteStrategyB(void);
44 void AlgorithmOperation(void);
45 };
46
47 //具体策略C
48 class CConcreteStrategyC : public CAbstractStrategy
49 {
50 public:
51 CConcreteStrategyC(void);
52 ~CConcreteStrategyC(void);
53 void AlgorithmOperation(void);
54 };
55
56 //concretestrategy.cpp
57 #include <iostream>
58 #include "concretestrategy.h"
59
62 CConcreteStrategyA::CConcreteStrategyA(void)
63 {
64 }
65
66 CConcreteStrategyA::~CConcreteStrategyA(void)
67 {
68 }
69
70 void CConcreteStrategyA::AlgorithmOperation(void)
71 {
72 std::cout << "运算操作A实现\n" << std::endl;
73 }
74
75 CConcreteStrategyB::CConcreteStrategyB(void)
76 {
77 }
78
79 CConcreteStrategyB::~CConcreteStrategyB(void)
80 {
81 }
82
83 void CConcreteStrategyB::AlgorithmOperation(void)
84 {
85 std::cout << "运算操作B实现\n" << std::endl;
86 }
87
88 CConcreteStrategyC::CConcreteStrategyC(void)
89 {
90 }
91
92 CConcreteStrategyC::~CConcreteStrategyC(void)
93 {
94 }
95
96 void CConcreteStrategyC::AlgorithmOperation(void)
97 {
98 std::cout << "运算操作C实现\n" << std::endl;
99 }
100
101 //context.h
102 #pragma once
103
104 #include "strategy.h"
105
106 //上下文类
107 class CContext
108 {
109 public:
110 CContext(CAbstractStrategy *pStrategy);
111 ~CContext(void);
112 void ExecuteAlgorithm(void);
113
114 private:
115 CAbstractStrategy *m_pStrategy;
116 };
117
118 //context.cpp
119 #include "context.h"
120
121 CContext::CContext(CAbstractStrategy *pStrategy)
122 {
123 this->m_pStrategy = pStrategy;
124 }
125
126 CContext::~CContext(void)
127 {
128 delete this->m_pStrategy;
129 }
130
131 void CContext::ExecuteAlgorithm(void)
132 {
133 this->m_pStrategy->AlgorithmOperation();
134 }
135
136 //main.cpp
137 #include <iostream>
138 #include "context.h"
139 #include "concretestrategy.h"
140
143 int main(void)
144 {
145 CContext *pContext;
146
147 std::cout << "执行算法A\n" << std::endl;
148 pContext = new CContext(new CConcreteStrategyA());
149 pContext->ExecuteAlgorithm();
150 delete pContext;
151
152 std::cout << "执行算法B\n" << std::endl;
153 pContext = new CContext(new CConcreteStrategyB());
154 pContext->ExecuteAlgorithm();
155 delete pContext;
156
157 std::cout << "执行算法C\n" << std::endl;
158 pContext = new CContext(new CConcreteStrategyC());
159 pContext->ExecuteAlgorithm();
160 delete pContext;
161
162 return 0;
163 }

 

posted on 2011-11-30 15:29  会说话的哑巴  阅读(964)  评论(0编辑  收藏  举报