Effective C++ Item 35 Consider alternatives to virtual functions
考虑你正在为游戏人物设计一个继承体系, 人物有一个函数叫做 healthValue, 他会返回一个整数, 表示人物的健康程度. 由于不同的人物拥有不同的方式计算他们的健康指数, 将 healthValue 声明成一个 virtual 似乎是再合适不过的了
class GameCharacter {
public:
virtual int healthValue() const;
...
};
那么, 它还有没有其他的实现形式呢?
1. 藉由 Non-Virtual Interface 手法实现 Template Method 模式
class GameCharacter {
public:
virtual int healthValue() const {
...
int retVal = doHealthValue();
...
return retVal;
} private:
virtual int doHealthValue() {
...
}
};
NVI 手法的一个隐身优点是上述代码注释"做一些事前工作", "做一些事后工作". 这意味着 wrapper 确保得以在一个 virtual 函数被调用之前设置好适合场景, 并在调用之后清理场景.
另外, doHealthValue 没必要是 private
2. 藉由 Function Pointer 实现 Strategy 模式
具体实现是
class GameCharacter {
public:
typedef int (*HealthCalcFunc)(const GameCharacter&); explicit GameCharacter(HealthCalcFunc hcf) {
healthFunc = hcf;
} int healthValue() const {
return healthFunc(*this);
}
...
private:
HealthCalcFunc healthFunc;
};
从上面的代码可以看出 内部策略函数的框架是 int (*pf) (const GameCharacter)
函数唯一的参数是 GameCharacter, 这意味着血量的计算通过 GC(GameCharacter) 的 public 借口即可完成计算, 不需要设计其 private 的部分. 而假如不得不引用 private 部分, 那么 "弱化封装" 是唯一的解法.
3. 藉由 tr1::function 完成 Strategy 模式
从 Java 转过来的程序员可能对函数指针这个东西会产生莫名的恐惧, 额, 我就是其中之一, 能不能将策略模式的核心转换成类似对象的东西呢? 答案是肯定的, 这也构成了第(3), (4) 小节的题目
class GameCharacter {
public:
typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc;
... // 其他部分完全与函数指针方法兼容
}
tr1::function 充当了一个泛化的函数, 其依然能够接受函数指针, 能够接受函数对象, 甚至使用 bind
short calcHealth(const GameCharacter&);
struct HealtheCalculator {
int operator()(const GameCharacter&) const {
...
}
}; class GameLevel {
public:
float health(const GameCharacter&) const;
...
}; class EvilBadGuy:public GameCharacter {...};
class EyeCandyCharater: public GameCharacter{...}; EvilBadGuy ebg1(calcHealth); // 接受一个函数
EyeCandyCharater ecc1(HealtheCalculator());// 接受函数对象
GameLevel currentLevel;
EvilBadGuy( // 接受某一个成员函数
std::tr1::bind(&GameLevel::health, currentLevel, _1)
);
GameLevel::health 函数有两个参数, bind 则指定 currentLevel 作为调用 health 的对象
4. 古典 Strategy 模式
正如上面 策略模式的图解一样, 古典策略模式会将健康计算函数成为一个分离继承体系中的 virtual 函数
Effective C++ Item 35 Consider alternatives to virtual functions的更多相关文章
- 读书笔记 effective c++ Item 35 考虑虚函数的替代者
1. 突破思维——不要将思维限定在面向对象方法上 你正在制作一个视频游戏,你正在为游戏中的人物设计一个类继承体系.你的游戏处在农耕时代,人类很容易受伤或者说健康度降低.因此你决定为其提供一个成员函数, ...
- Effective JavaScript Item 35 使用闭包来保存私有数据
本系列作为EffectiveJavaScript的读书笔记. JavaScript的对象系统从其语法上而言并不鼓舞使用信息隐藏(Information Hiding).由于当使用诸如this.name ...
- 读书笔记 effective c++ Item 37 永远不要重新定义继承而来的函数默认参数值
从一开始就让我们简化这次的讨论.你有两类你能够继承的函数:虚函数和非虚函数.然而,重新定义一个非虚函数总是错误的(Item 36),所以我们可以安全的把这个条款的讨论限定在继承带默认参数值的虚函数上. ...
- 读书笔记 effective c++ Item 39 明智而谨慎的使用private继承
1. private 继承介绍 Item 32表明C++把public继承当作”is-a”关系来对待.考虑一个继承体系,一个类Student public 继承自类Person,如果一个函数的成功调用 ...
- 读书笔记 effective c++ Item 54 让你自己熟悉包括TR1在内的标准库
1. C++0x的历史渊源 C++标准——也就是定义语言的文档和程序库——在1998被批准.在2003年,一个小的“修复bug”版本被发布.然而标准委员会仍然在继续他们的工作,一个“2.0版本”的C+ ...
- Standard C++ Programming: Virtual Functions and Inlining
原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
- Effective C++ -----条款35:考虑virtual函数以外的其他选择
virtual函数的替代方案包括NVI手法及Strategy设计模式的多种手法.NVI手法自身是一个特殊形式的Template Method设计模式. 将机能从成员函数移到class外部函数,带来的一 ...
随机推荐
- python2 UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 7: ordinal not in range(128)
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...
- Accumulator
Accumulator简介 Accumulator是spark提供的累加器,顾名思义,该变量只能够增加. 只有driver能获取到Accumulator的值(使用value方法),Task只能对其做增 ...
- SCR文件的关联被AutoCAD所取代的解决办法
SCR文件的关联被AutoCAD所取代的解决办法 --------转载自:http://hi.baidu.com/ygq366/item/45dceee3df47d2b52e140be9 SCR文件的 ...
- PHP——语句和时间函数
语句 1.分支语句 (1)if例子:$a=9;$b=5;if($a>$b){ echo $a."比".$b."大"; }else{ echo $a.&qu ...
- JavaScriptSerializer 时间格式化
时间格式化 Model m = , Dt = DateTime.Now }; JavaScriptSerializer js = new JavaScriptSerializer(); string ...
- copy src remote_src false表示本地,true在远程
文件组装模块-assemble assemble主要是将多份配置文件组装为一份配置文件. 参数 必填 默认 选项 说明 Backup 否 No Yes/no 是否创建备份文件,使用时间戳 Delimi ...
- java-事务-案例
项目结构: 数据库: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - threadlocal ************** ...
- 上手并过渡到PHP7(1)——基于Homestead的PHP7和XDdebug环境
PHP7 up and running 泊学实操视频泊学原文链接PHP7, Xdebug and Homestead 在经历了13个RC版本之后,PHP 7终于来了.在我们上手评估PHP 7的新特性之 ...
- .NET操作Excel笔记
如果你新建一个项目的话,首先要添加Microsoft.Office.Core 与Microsoft.Office.Interop.Exce这两个应用,然后就能很方便的操作了,示例代码(只实现了简单的读 ...
- 【这特么是个坑。。。】iOS 10.3下解决Charles抓包ssl证书信任问题
针对近期iOS 10.3以上的系统charles抓https信任问题 前言 最近iPhone系统更新到ios 10.3后,在公司里用Charles抓包竟然出现了一些问题,https的请求都会失败,提示 ...