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/

时间: 2024-10-15 18:23:10

Policy-based design设计模式的相关文章

[Cisco] Policy Based Routing

在某些情況下,會希望指定特定的來源及目的走特定的出口,卻又不是全部的網段都希望套用,這時就可以透過PBR來達成這個需求. 如以下拓樸 1.1.1.0/24的網路往5.5.5.0的封包需要指定e0/1當出口,往6.6.6.0的封包需要走e0/2. 1. 首先指定兩條ACL access-list 100 permit ip 1.1.1.0 0.0.0.255 5.5.5.0 0.0.0.255 access-list 101 permit ip 1.1.1.0 0.0.0.255 6.6.6.0

模板类与类模板、函数模板与模板函数等的区别

在C++中有好几个这样的术语,但是我们很多时候用的并不正确,几乎是互相替换混淆使用.下面我想彻底辨清几个术语,这样就可以避免很多概念上的混淆和使用上的错误.这几个词是: 函数指针——指针函数 数组指针——指针数组 类模板——模板类 函数模板——模板函数 最终在使用中,我们就可以让它们实至名归,名正言顺. 1.函数指针——指针函数   函数指针的重点是指针.表示的是一个指针,它指向的是一个函数,例子: int   (*pf)(); 指针函数的重点是函数.表示的是一个函数,它的返回值是指针.例子:

bullet HashMap 内存紧密的哈希表

last modified time:2014-11-9 14:07:00 bullet 是一款开源物理引擎,它提供了碰撞检测.重力模拟等功能,很多3D游戏.3D设计软件(如3D Mark)使用它作为物理引擎. 作为物理引擎,对速度的要求是非常苛刻的:bullet项目之所以能够发展到今天,很大程度取决于它在速度上优异的表现. 翻阅bullet的源码就能看到很多源码级别的优化,本文将介绍的HashMap就是一个典例. bullet项目首页:http://bulletphysics.org/ 注:b

读-《c++设计新思维-泛型编程与设计模式之应用》经典记录(英文书名:《modern c++ design》)

1.以设计为目标的程序库都必须帮助使用者完毕静止的设计.以实现使用者自己的constraints,而不是实现预先定义好的constraints. 2.Anything that can be done in more than one way should be identified and migrated from the class to a policy 3.Design is choice. Most often, the struggle is not that there is n

系统开发之设计模式

Control plane和data plane别离 这两个概念简直是networks 101的入门概念.Juniper上世纪末兴起的主要原因之一即是严厉区别界定control plane和data plane,然后用ASIC完结data plane.Data plane是指一个网络设备用于报文转发的component,它的功率决议全部设备的功率,通常会由硬件完结.Control plane是指一个设备协议有关的部分,能够没有数据转发那么高效. 当你翻开浏览器拜访谷歌时,internet上面的网

Design Patterns Example Code (in C++)

Overview Design patterns are ways to reuse design solutions that other software developers have created for common and recurring problems. The design patterns on this page are from the book Design Patterns, Elements of Reusable Object-Oriented Softwa

springmvc4开发rest

Spring MVC 4 RESTFul Web Services CRUD Example+RestTemplate Created on: August 11, 2015 | Last updated on: March 12, 2017 websystiqueadmin In this post we will write a CRUD Restful WebService using Spring MVC 4, and write a REST client with RestTempl

General-Purpose Operating System Protection Profile

1 Protection Profile Introduction ? This document defines the security functionality expected to be provided by a general-purpose operating system capable of operating in a networked environment. It also provides a set of assurance components that de

Smart internet of things services

A method and apparatus enable Internet of Things (IoT) services based on a SMART IoT architecture by integrating connectivity, content, cognition, context, cloud, and collaboration. Joint optimization of a combination of any of connectivity, content,