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 ...
随机推荐
- log4j.xml 精选的log4j.xml文档,比较详细,网上的版本很多,这个版本相对而言比较完整
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration PUB ...
- java中等待所有线程都执行结束
转自:http://blog.csdn.net/liweisnake/article/details/12966761 今天看到一篇文章,是关于java中如何等待所有线程都执行结束,文章总结得很好,原 ...
- zabbix监控流程图
- 常见的网络命令--ping.hostname
hostname命令 作用:显示以及设置主机名 一. 显示系统主机名 第一种方式:hostname 第二种方式:cat /etc/sysconfig/ntework 使用举例: 从上面可以看到我的系 ...
- POJ 1985 Cow Marathon (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- 算法导论 第六章 2 优先队列(python)
优先队列: 物理结构: 顺序表(典型的是数组){python用到list} 逻辑结构:似完全二叉树 使用的特点是:动态的排序..排序的元素会增加,减少#和快速排序对比 快速一次排完 增 ...
- 【java基础 4】树形结构数据呈现的非递归算法(循环)实现
一.基本概况 上一篇博客介绍到用递归实现树结构数据的查找,那么这篇博客,我就结合自己对于树的理解,然后用一种非递归的方式进行树结构数据的处理.首先,改造数据库表设计,加入度的概念: 首先,layer的 ...
- hihoCoder#1141 二分·归并排序之逆序对
原题地址 又是一道WA成狗的题,最后发现原来是结果溢出了.. 代码: #include <iostream> #include <cstring> using namespac ...
- Java多线程干货系列—(二)synchronized
原文地址:http://tengj.top/2016/05/03/threadsynchronized2/ <h1 id="前言"><a href="# ...
- poj1376 bfs,机器人
开始时候有点怕, 感觉什么也不会,不过静下来思考思考也就想出来了,一个简单的BFS即可,但是由于队列没有重判,一直爆队列(MLE!)下次一定要注意! (bfs第一次到达便最优?) #include&l ...