Policy-based design
来源:互联网 发布:windows gcc下载 编辑:程序博客网 时间:2024/05/22 12:03
One problem which often arises during programming is how to build a base set of functionality which can be extended by the user, while still being modular enough to make it easy to replace only certain parts of an implementation without having to resort to copy & paste techniques. I guess everybody of us has faced this problem at least once, and came up with different solutions. There is a powerful and elegant technique called policy-based design for solving this kind of problem.
Simple example
#include<iostream>#include<string>using namespace std;template<typename output_policy,typename language_policy>class HelloWorld:public output_policy,public language_policy{ using output_policy::Print; using language_policy::Message;public: void Run(){Print(Message()); }};class HelloWorld_OutputPolicy_WriteToCout{protected:template<typename message_type> void Print(message_type message) {std::cout << message << std::endl; }};class HelloWorld_LangguagePolicy_English{protected:std::string Message() { return "Hello World!"; }};class HelloWorld_LanguagePolicy_German{protected:std::string Message() { return "Hello Policy!"; }};int main(){ typedef HelloWorld<HelloWorld_OutputPolicy_WriteToCout,HelloWorld_LangguagePolicy_English> myPolicyClass; myPolicyClass hello1; hello1.Run(); typedef HelloWorld<HelloWorld_OutputPolicy_WriteToCout,HelloWorld_LanguagePolicy_German> myOtherPolicyClass; myOtherPolicyClass hello2; hello2.Run();}
Makefile
all:policy# which compilerCC=g++ # Where are include file keptINCLUDE = .# Where to installINSTDIR = /usr/local/bin# Options for developmentCFLAGS = -g -Wall -ansipolicy:policy.o$(CC) -o policy policy.opolicy.o:policy.cpp#$(CC) -I$(INCLUDE) $(CFLAGS) -c msgqueue.c#$(CC) -D_REENTRANT -c msgqueue.c -lpthread$(CC) -c policy.cpp -o policy.oclean:-rm policy.o policyinstall:policy@if [-d $(INSTDIR) ];\ then \ cp policy $(INSTDIR);\ chmod a+x $(INSTDIR)/policy;\ chmod og-w $(INSTDIR)/policy;\ echo "Install in $(INSTDIR)";\ else \ echo "Sorry,$(INSTDIR) does not exist";\ fi
Runing result:
Hello World
Hello Policy
- policy-based design
- Policy-Based Class Design
- Policy-based design
- c++ Policy-based design
- Policy-based design设计模式
- 如何用C#实践Policy based design
- 第一章:Policy-Based Class Design 读书笔记
- Modern C++ Design 学习笔记之Policy Based Class Design
- 关于Policy Based Class Design--《Modern C++ Design》读后感一
- [GP][Modern C++ Design]Policy-Based Class Design
- Modern C++ Design 笔记 第一章 Policy-Based Class Design
- Modern C++ Design 学习笔记之Policy Based Class Design
- Modern C++ Design 第一章 Policy-Based Class Design
- Reading Notes: Chapter 1. Policy-Based Class Design(Modern C++ Design By Andrei Alexandrescu)
- 学习 Policy based design - 读C++设计新思维-泛型编程与设计模式的应用
- Zoned-Based policy FW
- 用C++ 模板做代码设计的三类方法及代码——基于策略Policy-based design
- Multimedia-based Instructional Design
- C++结构体实例和类实例的初始化
- C++的显示转换
- C/C++中枚举类型(enum)的使用解惑
- 完了,来csdn这么久了,竟然不会发帖子!
- Ogre角色控制类(源码)
- Policy-based design
- sed, a stream editor
- 性能分析
- java IO操作:IO的一般使用原则
- Poem: I Loved You
- 数据结构(c语言版)1000个问题与解答-数组
- CENTOS VIM 配置
- 树状数组简单题(HDU1166)
- 树状数组简单题(HDU1166)