基于原则设计

✍ dations ◷ 2025-11-25 15:46:32 #计算机编程,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于其中。

相关

  • 伦巴第-威尼斯王国伦巴第-威尼托王国(意大利语:Regno Lombardo-Veneto,德语:Lombardo-Venezianisches Königreich)是一个位于意大利北部的王国,并由当时的奥地利帝国所控制。此王国是根据欧洲列强
  • 本杰明·汤普森本杰明·汤普森爵士,伦福德伯爵,FRS (英语:Sir Benjamin Thompson, Count Rumford , 德语:Reichsgraf von Rumford,1753年3月26日-1814年8月21日),英国物理学家,生于英属美洲。他对于热
  • 黄夹次苷黄夹次苷(英语:Peruvoside或称为黄花夹竹桃苷,英语:cannogenin thevetoside)是一种强心苷,用于治疗心脏衰竭,来源于黄花夹竹桃(学名:Cascabela thevetia,曾用学名:Thevetia neriifolia)
  • 隐失波渐逝波(evanescent wave) ,又称为消逝波或,隐失波,是指当光波从光密介质入射到光疏介质时,发生全反射而光疏介质一侧所产生的一种电磁波。由于其振幅随与分界面垂直的深度的增大
  • 许宗力许宗力(1956年2月10日-)为现任中华民国司法院院长。著名公法学者。许宗力之父是执业律师许竹谟,曾参选嘉义县县长但未当选。因耳濡目染的缘故,许宗力从小便立志学法的志向。中学
  • 四苯基乙烯四苯基乙烯是一种可以在建筑construction of what?以及医疗设备、包装和电器的制造中使用的化合物。四苯基乙烯可以由二苯二氯甲烷为原料,经金属还原而合成。也可由二苯酮经
  • 艾斯迪斯·阿拉鲁祖·艾华斯艾斯迪斯·伊杜亚度·文迪·阿拉鲁祖·艾华斯(Alcides Eduardo Mende Araújo Alves,1985年3月13日-),巴西足球运动员,司职后卫。他无论是作为右后卫或中后卫,速度是他的主要资产。
  • BIGBANG获奖及提名列表《BIGBANG获奖及提名列表》主要列举韩国男子音乐组合BIGBANG出道至今于颁奖典礼、音乐节目的获奖与提名。BIGBANG做为以出道10年的团体成功取得了韩国及国际大型颁奖典礼多
  • 林卓宇林卓宇(1995年11月17日-),生于湖南浏阳。湖南省作家协会会员,已出版作品多部。获得第二届鲁迅青少年文学奖大奖、第五届冰心作文奖、第八、九、十、十一届中国少年作家杯征文大赛
  • 丹·H·亚阿隆丹·H·亚阿隆(1924年5月11日-2014年1月29日),以色列杰出的土壤学家。亚阿隆在干旱地区和地中海地区土壤学以及古土壤学领域贡献卓著。在土壤学的分支学科,譬如土壤科学的历史、