http://en.wikipedia.org/wiki/Policy-based_design(第一分代码的来源)
这本书以前看过,是关于template 很好的书,关于policy-based wiki 用继承实现:
列出了3个方法,还有很多衍生版本,总结如下:
通过继承,与static 有同样的使用接口
继承:对代码改动最小:using ..., 适用于对代码已经写好,但用于追加功能,修改功能的时候
static: 代码改动较继承大,需要在每个修改的地方都加上类名,适用于:代码设计阶段。优点是代码清晰明了(相对其它,最清晰)
指针构造:功能最完善,适用于极其复杂的功能设计。扩展性强。缺点是,代码改动大,只能在代码设计阶段使用。
#include "inheritance.h" #include "static.h" #include "pointer.h" int main() { AAA::inheritance_t(); BBB::static_t(); CCC::pointer_t(); }1.通过继承
#pragma once #include <iostream> #include <string> namespace AAA{ template <typename OutputPolicy, typename LanguagePolicy> class HelloWorld : private OutputPolicy, private LanguagePolicy { using OutputPolicy::print; using LanguagePolicy::message; public: void run() const { 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!"; } }; void inheritance_t() { /* Example 1 */ typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyEnglish> HelloWorldEnglish; HelloWorldEnglish hello_world; hello_world.run(); // prints "Hello, World!" #if 1 /* Example 2 * Does the same, but uses another language policy */ typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyGerman> HelloWorldGerman; HelloWorldGerman hello_world2; hello_world2.run(); // prints "Hallo Welt!" #endif } }
便于查看
继承:对代码改动最小:using ..., 适用于对代码已经写好,但用于追加功能,修改功能的时候
static: 代码改动较继承大,需要在每个修改的地方都加上类名,适用于:代码设计阶段。优点是代码清晰明了(相对其它,最清晰)
指针构造:功能最完善,适用于极其复杂的功能设计。缺点是,代码改动大,只能在代码设计阶段使用。
2.通过static函数
#pragma once #include <iostream> #include <string> namespace BBB{ template <typename OutputPolicy, typename LanguagePolicy> class HelloWorld { public: // Behaviour method static void run() /*const*/ { OutputPolicy::print(LanguagePolicy::message()); } }; class OutputPolicyWriteToCout { //protected: public: template<typename MessageType> static void print(MessageType const &message) /*const*/ { std::cout << message << std::endl; } }; class LanguagePolicyEnglish { //protected: public: static std::string message() /*const*/ { return "Hello, World!"; } }; class LanguagePolicyGerman { //protected: public: static std::string message() /*const*/ { return "Hallo Welt!"; } }; void static_t() { /* 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!" } }
便于查看
继承:对代码改动最小:using ..., 适用于对代码已经写好,但用于追加功能,修改功能的时候
static: 代码改动较继承大,需要在每个修改的地方都加上类名,适用于:代码设计阶段。优点是代码清晰明了(相对其它,最清晰)
指针构造:功能最完善,适用于极其复杂的功能设计。缺点是,代码改动大,只能在代码设计阶段使用。
3.通过构造函数指针
//pointer.h
#pragma once #include <iostream> #include <string> namespace CCC{ template <typename OutputPolicy, typename LanguagePolicy> class HelloWorld { const OutputPolicy & _policy; const LanguagePolicy & _lang; public: HelloWorld(const OutputPolicy& policy, const LanguagePolicy &lang) :_policy(policy), _lang(lang) { } void run() const { _policy.print(_lang.message()); } }; class OutputPolicyWriteToCout { //protected: public: template<typename MessageType> void print(MessageType const &message) const { std::cout << message << std::endl; } }; class LanguagePolicyEnglish { //protected: public: std::string message() const { return "Hello, World!"; } }; class LanguagePolicyGerman { //protected: public: std::string message() const { return "Hallo Welt!"; } }; void pointer_t() { /* Example 1 */ OutputPolicyWriteToCout out; LanguagePolicyEnglish langEng; LanguagePolicyGerman langGerm; { typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyEnglish> HelloWorldEnglish; HelloWorldEnglish outlang(out, langEng); outlang.run(); } /* Example 2 * Does the same, but uses another language policy */ { typedef HelloWorld<OutputPolicyWriteToCout, LanguagePolicyGerman> HelloWorldGerman; HelloWorldGerman outlang(out, langGerm); outlang.run(); } } }
便于查看
继承:对代码改动最小:using ..., 适用于对代码已经写好,但用于追加功能,修改功能的时候
static: 代码改动较继承大,需要在每个修改的地方都加上类名,适用于:代码设计阶段。优点是代码清晰明了(相对其它,最清晰)
指针构造:功能最完善,适用于极其复杂的功能设计。缺点是,代码改动大,只能在代码设计阶段使用。
用C++ 模板做代码设计的三类方法及代码——基于策略Policy-based design,布布扣,bubuko.com