基于原则设计

✍ dations ◷ 2025-11-19 09:36:55 #计算机编程,C++

基于原则设计(Policy-Based Class Design)又名policy-based class design 或 policy-based programming, 是一种基于C++计算机程序设计规范,以原则(Policy)为基础,并结合C++的模板超编程(template metaprogramming)。policy-based首见于 Andrei Alexandrescu 出版的 《Modern C++ Design》 一书以及他在C/C++ Users Journal杂志专栏 Generic<Programming> 。

Policy的意思是方针,或策略,也就是将原本复杂的系统,拆解成多个独立运作的“策略类别”(policy class),每一组policy class都只负责单纯如行为(behavior, method)或结构(structure)的某一方面。Templetes与多重继承(Multiple Inheritance)两项技术互补。多重继承由于继承自多组 Base Class,故缺乏型别消息(type information),而Templetes基于型别,拥有丰富的型别消息。多重继承容易扩张,而Templetes的特化(Specialization)不容易扩张。Policy-Based Class Design 同时使用了 Template 以及 Multiple Inheritance 两项技术,结合两者的优点。

template<         class Policy1,         class Policy2,         class Policy3>class PolicyBasedClass:public Policy1, public Policy2{public:   PolicyBasedClass(){};};

PolicyBasedClass则称为宿主类别(host class),只需要切换不同 Policy Class,就可以得到不同的需求。Policy不一定要被宿主(host)继承,只需要用委托(delegation)完成这一工作。但policies必须遵守一个隐含的constraint,接口(interface)必须一样,故参数不能有巨大改变。

Policy class有点类似回调函数(callbacks),但不同的是,callback只是一个函数,至于 policy class 则尽可能包含许多的 functions (methods), 有时还会结合状态变量(state variables)与其他各式各样的类型,如嵌套类型(nested types)。

policy 的一个重要的特征是,宿主类别(host class)经常(并不一定要)使用多重继承的机制去使用多个 policy classes. 因此在进行 policy 拆解时,必须要尽可能达成正交分解(Orthogonal Decomposition),policy彼此独立运作,不相互影响。

在 C++ 的Policy-Based Design 中,用来建构 Template 的类别参数(也就是policy class),本身亦可以是一个 Tempate 化的类别,形成所谓的 Template Template Parameter。

如果 Read()、Write() 有各种不同名目的参数时,可以利用 template 的不完全具现化 (Incomplement Instantiation) 特征检实现各个参数不同的成员函数(member function)。在host class中,可以撰写不同参数版本的 Read(...) 函数,这有赖于c++ compiler的协助。

template<	class T,	template< class > class ReadPolicy,	template< class > class WritePolicy>class ResourceManager	:	public ReadingPolicy< T >,	public WritingPolicy< T >{public:   void Read();   void Write(XmlElement*);   void Write(DataSource*);};

上述的class T即是一个Template Template Parameter,这使得 Policy Class 更具扩展性与弹性,能够处理各种类型的实体(instance)。

void main(){  ResourceManager< AnimationEntity, BinaryReader, BinaryWriter > ResMgr1;  ResourceManager< ScriptEntity, TextReader, TextWriter > ResMgr2;}

示例

下例是 C++ hello world的示例,可以使用各种原则(policies)打印文字。

template<    typename output_policy,    typename language_policy>class HelloWorld  : public output_policy,    public language_policy{    using output_policy::Print;    using language_policy::Message;public:    //behaviour method    void Run()    {        //two policy methods        Print( Message() );    }};#include <iostream>class HelloWorld_OutputPolicy_WriteToCout{protected:    template< typename message_type >    void Print( message_type message )    {        std::cout << message << std::endl;    }};#include <string>class HelloWorld_LanguagePolicy_English{protected:    std::string Message()    {        return "Hello, World!";    }};class HelloWorld_LanguagePolicy_German{protected:    std::string Message()    {        return "Hallo Welt!";    }};int main(){/* example 1 */    typedef        HelloWorld<            HelloWorld_OutputPolicy_WriteToCout,            HelloWorld_LanguagePolicy_English        >            my_hello_world_type;    my_hello_world_type hello_world;    hello_world.Run(); //returns Hello World!/* example 2  * does the same but uses another policy, the language has changed */    typedef        HelloWorld<            HelloWorld_OutputPolicy_WriteToCout,            HelloWorld_LanguagePolicy_German        >            my_other_hello_world_type;    my_other_hello_world_type hello_world2;    hello_world2.Run(); //returns Hallo Welt!}

你可以更容易的撰写其他的 Output policy, 单靠你创造更新的Policy class并实现print于其中。

相关

  • 东京电力东京电力控股株式会社(日语:東京電力ホールディングス株式会社;英语译名:Tokyo Electric Power Company Holdings, Inc.),简称东京电力、东京电力HD、东电或TEPCO,是日本一家电力公
  • 第二次世界大战战役列表以下是第二次世界大战战役列表:冬季战争中唯一的一场坦克战役。统称个别 193919401941194219431944
  • 靳云鹏靳云鹏(1877年-1951年1月3日),字翼青,山东省兖州府邹县(今邹城)苗庄人,后随父母逃荒到济宁市区南马道。中华民国军事及政治人物,曾任中华民国国务总理。段祺瑞手下“四大金刚”之一(其
  • 台湾群岛本文叙述中华民国政府实际统治领域的岛屿。依中华民国台湾、澎湖、金门、马祖及其附属岛屿划分如下:(8)以下诸岛皆隶属于基隆市中正区(1)(2)(1)(1)(2)(1)(6)(2)(2)(10)(1)(4)澎湖县(90)
  • 公视台语台公视台语台是公共电视文化事业基金会的电视频道之一,前身为2004年7月1日开播的“Dimo TV”、以及2012年10月1日更名的“公视2台”,2019年7月1日改为现名,成为台湾第一个以全台
  • 铕的同位素铕(原子量:151.964(1))的同位素,其中有1个同位素是在观测上稳定的。自然形成的铕元素由两种同位素组成:151Eu和153Eu,后者的丰度为52.2%,比前者稍高。153Eu是稳定同位素,但151Eu则会
  • 国际原子时国际原子时(英语:International Atomic Time、法语:Temps Atomique International, TAI)是根据以下秒的定义的一种国际参照时标, 属于国际单位制。1967年第13届国际计量大会上通
  • 欧扎克-圣弗朗西斯国家森林欧扎克-圣弗朗西斯国家森林(英语:Ozark – St. Francis National Forest)是座美国国家森林,位于美国阿肯色州。森林两处不同的森林合并而成——欧扎克国家森林(Ozark National Fo
  • 恩斯特·奥古斯特四世恩斯特·奥古斯特四世(Ernst August IV,1914年3月18日-1987年12月9日),全名恩斯特·奥古斯特·格奥尔格·威廉·克里斯蒂安·路德维希·弗朗茨·约瑟夫·尼库劳斯·奥斯卡(Ernst A
  • 杰瑞米·艾伦·怀特杰瑞米·艾伦·怀特(英语:Jeremy Allen White,1991年2月18日-) ,美国男演员,他最知名的角色是美国电视系列剧《无耻之徒》中的Phillip "Lip" Gallagher。