基于原则设计

✍ dations ◷ 2025-02-23 21:19:16 #计算机编程,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于其中。

相关

  • 獴科(学名:Herpestidae),哺乳纲食肉目的一科,外形较像猫,有猫鼬的别称。包括獴亚科及缟獴亚科两个亚科。以下几属过去归为獴科,现在被分入食蚁狸科(Eupleridae):
  • Vsub2/subOsub5/sub五氧化二钒,IUPAC名称为氧化钒(V),是钒(V)的氧化物,化学式为V2O5。它是一种有毒的橙黄色固体,微溶于水,加热时失去氧而分解。可作化学工业中的催化剂,最重要的是对硫酸工业中二氧
  • 刘建康刘建康(1917年9月1日-2017年11月6日),中国鱼类学家、生态学家,中国淡水生态学奠基人、鱼类实验生物学主要开创者之一。江苏吴江人。1938年毕业于东吴大学生物系,获理学士学位。194
  • 脑部受损脑损伤(Brain damage)或脑部受伤(brain injury,简称BI)是指人脑细胞的受损或是退化。脑损伤可能因为一些内在或是外在的因素所造成。一般而言,会用“脑损伤”来表达一般明显的,因为
  • Enterovirus CPoliovirus脊髓灰质炎病毒(Poliovirus,或称为脊髓灰白质炎病毒)是脊髓灰质炎(小儿麻痹)的病原,又称小儿麻痹病毒。它是一个没有包膜的病毒,由一条单股RNA和蛋白质外壳组成,直径约25
  • 约翰·马瑟约翰·克伦威尔·马瑟(英语:John Cromwell Mather,1945年8月7日-),美国国家航空航天局戈达德航天中心的高级天体物理学家。他和乔治·斯穆特因发现了宇宙微波背景辐射的黑体形式和
  • 关联规则学习关联规则学习(英语:Association rule learning)是一种在大型数据库中发现变量之间的有趣性关系的方法。它的目的是利用一些有趣性的量度来识别数据库中发现的强规则。 基于强
  • 扩散性思考扩散性思考(Divergent thinking)是一种思考的过程及方法,透过讨论许多可能的处理方式,可以得到有创意的概念。扩散性思考一般是以自发性、非线性思维的方式进行,因此许多的概念可
  • 哲学咨商哲学咨商,又称作哲学辅导。即是以哲学思考化解日常生活中所遇上的烦恼。哲学咨商于一九八零年风靡欧洲,当时阿肯巴赫不满精神病理学和心理治疗把人生问题定义为疾病,他认为这样
  • 石板滩街道 (成都市)石板滩街道,原为石板滩镇,是中华人民共和国四川省成都市新都区下辖的一个乡镇级行政单位。2019年12月,撤销木兰镇、石板滩镇,设立石板滩街道,以原木兰镇和原石板滩镇所属行政区域