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外部函数,带来的一 ...
随机推荐
- 【图文教程】WebStorm下使用Github下载以及上传代码
1.从一个git路径下,下载代码到本地,选择VCS->Checkout from Version Control ->GitHub. 2.可能会弹出需要设置上传代码的密码,这 ...
- 翻转数字最后n位
#include<stdio.h> int turn_n(int ,int); int main(void) { ,b=; printf("%x\n%d\n%x\n", ...
- MySQL 日期计算
MySQL提供了几个函数,可以用来计算日期,常用的例子就是,计算年龄或提取日期部分. 1. 计算年龄: mysql中要想计算一个人的年龄,相当于当前日期的年和出生日期之间的差.如果当前日期的日历年比出 ...
- 对于REST中无状态(stateless)的一点认识(转)
在请求中传递SessionID被普遍认为是unRESTful的,而将用户的credentials包含在每个请求里又是一种非常RESTful的做法.这样一个问题经常会造成困扰.本文就REST的一些概念进 ...
- 补充下.net知识
问题1: public int getvalue(int a) { try { a = a + ; ; } catch (Exception) { throw; } finally { a = a + ...
- tar -cvzf a.tar.gz a --remove-files,tar命令执行原理
tar -cvzf a.tar.gz a --remove-files [root@nfs01 backup]# tar -zcvf 88.tar.gz --remove-files /b ...
- Hibernate- QBC-基本查询
01.环境搭建 02.基本查询 1.方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt &g ...
- ApplicationEventMulticaster not initialized - call 'refresh' before
https://stackoverflow.com/questions/45318618/applicationeventmulticaster-not-initialized-call-refres ...
- 深入浅出Cache
章节 ① 什么是Cache? Cache的目标? ② Caching住哪些内容? ③ 我们想要的Cache产品 ④ Cache使用方式 ⑤ 对于总体系统的提高 ⑥ 关于Sharding ⑦ Cache ...
- Cocos2d-x模版卸载及安装
卸载:将隐藏的模板文件删除掉 首先打开你mac终端,然后输入如下命令:显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -b ...