大龙的博客
C++博客
|
首页
|
发新随笔
|
发新文章
|
联系
|
聚合
|
管理
常见设计模式的解析和实现(C++)之十六-Strategy模式
作用:
定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换.本模式使得算法可独立于使用它的客户而变化.
UML结构图:
解析:
简而言之一句话,Strategy模式是对算法的封装.处理一个问题的时候可能有多种算法,这些算法的接口(输入参数,输出参数等)都是一致的,那么可以考虑采用Strategy模式对这些算法进行封装,在基类中定义一个函数接口就可以了.
实现:
1)Strategy.h
/**/
/*
*******************************************************************
created: 2006/08/06
filename: Strategy.h
author: 李创
http://www.cppblog.com/converse/
purpose: Strategy模式的演示代码
********************************************************************
*/
#ifndef STRATEGY_H
#define
STRATEGY_H
class
Strategy;
class
Context
{
public
:
Context(Strategy
*
pStrategy);
~
Context();
void
ContextInterface();
private
:
Strategy
*
m_pStrategy;
}
;
class
Strategy
{
public
:
virtual
~
Strategy()
{}
virtual
void
AlgorithmInterface()
=
0
;
}
;
class
ConcreateStrategyA
:
public
Strategy
{
public
:
virtual
~
ConcreateStrategyA()
{}
virtual
void
AlgorithmInterface();
}
;
#endif
2)Strategy.cpp
/**/
/*
*******************************************************************
created: 2006/08/06
filename: Strategy.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Strategy模式的演示代码
********************************************************************
*/
#include
<
iostream
>
#include
"
Strategy.h
"
Context::Context(Strategy
*
pStrategy)
: m_pStrategy(pStrategy)
{
}
Context::
~
Context()
{
delete m_pStrategy;
m_pStrategy
=
NULL;
}
void
Context::ContextInterface()
{
if
(NULL
!=
m_pStrategy)
{
m_pStrategy
->
AlgorithmInterface();
}
}
void
ConcreateStrategyA::AlgorithmInterface()
{
std::cout
<<
"
AlgorithmInterface Implemented by ConcreateStrategyA\n
"
;
}
3)Main.cpp
/**/
/*
*******************************************************************
created: 2006/08/06
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
purpose: Strategy模式的测试代码
********************************************************************
*/
#include
"
Strategy.h
"
int
main()
{
Strategy
*
pStrategy
=
new
ConcreateStrategyA();
Context
*
pContext
=
new
Context(pStrategy);
pContext
->
ContextInterface();
delete pContext;
return
0
;
}
本文来源:http://www.cppblog.com/converse/archive/2006/08/06/10899.aspx
发表于 2006-09-22 18:17
大龙1
阅读(45)
评论(0)
编辑
收藏
引用
标题
姓名
主页
验证码
*
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
随笔:281 文章:2 评论:82 引用:0
<
2006年9月
>
日
一
二
三
四
五
六
27
28
29
30
31
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
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(6)
给我留言
查看公开留言
查看私人留言
随笔档案
2008年11月 (6)
2008年10月 (4)
2008年9月 (11)
2008年8月 (18)
2008年7月 (8)
2008年6月 (19)
2008年5月 (9)
2008年4月 (6)
2008年3月 (2)
2008年2月 (4)
2008年1月 (21)
2007年12月 (14)
2007年11月 (4)
2007年10月 (7)
2007年9月 (17)
2007年8月 (10)
2007年7月 (20)
2007年6月 (11)
2007年5月 (8)
2007年4月 (13)
2007年3月 (2)
2007年2月 (6)
2007年1月 (14)
2006年12月 (23)
2006年11月 (15)
2006年10月 (5)
2006年9月 (4)
文章档案
2007年11月 (1)
2006年12月 (1)
收藏夹
ps
(rss)
搜索
最新评论
1. re: Windows XP DDK 的有效下载地址
非常感谢楼主的无私奉献
--阿辉
2. re: Windows XP DDK 的有效下载地址
找了几天了,不是半天下不来,就是下下来不能用。
希望这个能用。
谢了先!
--bbc
3. re: Windows XP DDK 的有效下载地址
谢谢楼主!本人一直想希望能直接控制XP下的硬件,但觉得驱动程序真太难了。
--besidelake
4. re: Windows XP DDK 的有效下载地址
thank you !
--liuxmzc
5. re: 多态性----vptr----vtable
了解了VPTR,反而又更深入地了解了虚函数的实现了,多谢!
--langzilingqi
阅读排行榜
1. Windows XP DDK 的有效下载地址(5432)
2. hostent结构体(4977)
3. WaitForSingleObject(2419)
4. WaitForMultipleObjects用法探索(2147)
5. setsockopt()用法(2106)
评论排行榜
1. Windows XP DDK 的有效下载地址(17)
2. 在Cygwin上安装编辑器vim (12)
3. 经典好书 (6)
4. hostent结构体(4)
5. C++嵌套类(4)
60天内阅读排行
1. ubuntu默认root密码是什么?(197)
2. ubuntu下mp3播放及中文乱码 ------- 转(113)
3. C++中结构体的字节对齐问题 ---- 转(105)
4. C/C++中利用空指针简化代码,提高效率 ----- 转(75)
5. 在linux下实现windows查找文件和查找文件中内容的功能(67)