Policy-based design设计模式
书在4年前看过。今天重温一下:
一直觉得这是最好的设计模式,大牛Andrei
Alexandrescu 专门写了书,可见他的重要性
http://en.wikipedia.org/wiki/Policy-based_design
#include <iostream>
#include <string> template <typename OutputPolicy, typename LanguagePolicy>
class HelloWorld : private OutputPolicy, private LanguagePolicy
{
using OutputPolicy::print;
using LanguagePolicy::message; public:
// Behaviour method
void run() const
{
// Two policy methods
print(message());
}
}; class OutputPolicyWriteToCout
{
protected:
template<typename MessageType>
void print(MessageType const &message) const
{
std::cout << message << std::endl;
}
}; class LanguagePolicyEnglish
{
protected:
std::string message() const
{
return "Hello, World!";
}
}; class LanguagePolicyGerman
{
protected:
std::string message() const
{
return "Hallo Welt!";
}
}; int main()
{
/* Example 1 */
typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyEnglish> HelloWorldEnglish; HelloWorldEnglish hello_world;
hello_world.run(); // prints "Hello, World!" /* Example 2
* Does the same, but uses another language policy */
typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyGerman> HelloWorldGerman; HelloWorldGerman hello_world2;
hello_world2.run(); // prints "Hallo Welt!"
}
补充:
Templates as Interfaces
The C++ idiom to express the Talkative interface discussed in Venners's article would look something like this: template <class T>
class Talkative
{
T const & t;
public:
Talkative(T const & obj) : t(obj) { }
void talk() const { t.talk(); }
};
对照strategy:An example implementation of the Strategy design pattern in C++
http://r3dux.org/2011/07/an-example-implementation-of-the-strategy-design-pattern-in-c/
Policy-based design设计模式的更多相关文章
- 策略模式 Strategy 政策Policy 行为型 设计模式(二十五)
策略模式 Strategy 与策略相关的常见词汇有:营销策略.折扣策略.教学策略.记忆策略.学习策略.... “策略”意味着分情况讨论,而不是一概而论 面对不同年龄段的人,面对不同的商品,必然将会 ...
- bullet HashMap 内存紧密的哈希表
last modified time:2014-11-9 14:07:00 bullet 是一款开源物理引擎,它提供了碰撞检測.重力模拟等功能,非常多3D游戏.3D设计软件(如3D Mark)使用它作 ...
- 读-《c++设计新思维-泛型编程与设计模式之应用》经典记录(英文书名:《modern c++ design》)
1.以设计为目标的程序库都必须帮助使用者完毕静止的设计.以实现使用者自己的constraints,而不是实现预先定义好的constraints. 2.Anything that can be done ...
- 【翻译】设计模式学习系列1---【Design Patterns Simplified: Part 1【设计模式简述:第一部分】】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part1/ Design Pattern ...
- Java设计模式学习资源汇总
本文记录了Java设计模式学习书籍.教程资源.此分享会持续更新: 1. 设计模式书籍 在豆瓣上搜索了一把,发现设计模式贯穿了人类生活的方方面面.还是回到Java与程序设计来吧. 打算先归类,再浏览,从 ...
- 强化学习(十三) 策略梯度(Policy Gradient)
在前面讲到的DQN系列强化学习算法中,我们主要对价值函数进行了近似表示,基于价值来学习.这种Value Based强化学习方法在很多领域都得到比较好的应用,但是Value Based强化学习方法也有很 ...
- .NET异常处理的动作策略(Action Policy)
SQL Server 2008基于策略的管理,基于策略的管理(Policy Based Management),使DBA们可以制定管理策略,并将这些策略应用到服务器.数据库以及数据环境中的其他对象上去 ...
- Design Patterns Example Code (in C++)
Overview Design patterns are ways to reuse design solutions that other software developers have crea ...
- 深度增强学习--Policy Gradient
前面都是value based的方法,现在看一种直接预测动作的方法 Policy Based Policy Gradient 一个介绍 karpathy的博客 一个推导 下面的例子实现的REINFOR ...
随机推荐
- 暑假集训 || bitset
bitset是一个存储0和1的数组 可以快速的把两个bitset的每一位对应做与或啥的 在可以用01串表示某个状态的时候可以应用到它 就是有两个集合,求它们的交集 bitset <> a, ...
- 32位和64位系统下 int、char、long、double所占的内存
32位和64位系统下 int.char.long.double所占内存
- eclipse android SDK代理跟新
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- 总结Java开发者经常会犯的前十种错误
[导读] 在Java中,有些事物如果不了解的话,很容易就会用错,如数组转换为数组列表.元素删除.Hashtable和HashMap.ArrayList和LinkedList.Super和Sub构造函数 ...
- ftp上传文件、删除文件、下载文件的操作
FavFTPUtil.Java 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 31 ...
- 读懂CommonJS的模块加载
叨叨一会CommonJS Common这个英文单词的意思,相信大家都认识,我记得有一个词组common knowledge是常识的意思,那么CommonJS是不是也是类似于常识性的,大家都理解的意思呢 ...
- [LOJ] 分块九题 1
https://loj.ac/problem/6277 区间修改,单点查询. //Stay foolish,stay hungry,stay young,stay simple #include< ...
- [CF] 950B Intercepted Message
B. Intercepted Message time limit per test1 second memory limit per test512 megabytes inputstandard ...
- laravel使用总结(一)
安装 composer create-project laravel/laravel learnlaravel5 --prefer-dist v5.3.* 安装成功之后会自动生成一个key > ...
- json串转成list
List<BaseStaEmpEntity> baseStaEmpEntities = new ArrayList<>(); //json转List<baseStaEmp ...