您好,欢迎访问一九零五行业门户网

C++设计模式浅识装饰模式

装饰模式:动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。
装饰模式的四个角色:
组件类:component
具体组件类:coneretecomponent
装饰类:decorator(从外类来扩展component类的功能,但对于component来说是无需知道decorator存在的。)
具体装饰类:concretedecorator(起到给component添加职责的功能)
在此,以人们穿衣修饰为例实现装饰模式。
测试用例:
[code]int main(){ //初始化person(人)组件类component,如果只有一个coneretecomponent类而不需要抽象的component类,那么可以直接让decorator继承具体的component类。 concreteperson *cp = new concreteperson("jarrett"); //初始化具体服饰类(装饰类decorate) concretetshirts *tshirts = new concretetshirts; concretetrouser *trouser = new concretetrouser; concreteshoes *shoe = new concreteshoes; //concretesuit *suit = new concretesuit; concretehat *hat = new concretehat; //依次进行装饰 tshirts->decorate(cp); trouser->decorate(tshirts); shoe->decorate(trouser); //suit->decorate(shoe); //显示结果 shoe->show(); std::cout << std::endl; //再装饰一次查看效果 hat->decorate(shoe); hat->show(); return 0; }
装饰模式实现:
[code]//主类 class concreteperson{ private: std::string name;//人名 public: concreteperson(){} concreteperson(std::string n):name(n){} //构造方式 virtual void show()const{ std::cout << name << "'s dress: "; } }; //服饰类(装饰类主类) class finery: public concreteperson{ protected: concreteperson *cperson;//重点是维护一个主体类对象 public: void decorate(concreteperson *cp){ cperson = cp; } void show()const{ if(cperson != null) cperson->show(); } }; //具体服饰类tshirts class concretetshirts: public finery{ public: void show()const{ finery::show(); //调用基类方法 std::cout << "tshirts "; //用此来修饰 } }; //具体服饰类tshirts class concretetrouser: public finery{ public: void show()const{ finery::show(); //调用基类方法 std::cout << "trouser "; //用此来修饰 } }; //具体服饰类tshirts class concreteshoes: public finery{ public: void show()const{ finery::show(); //调用基类方法 std::cout << "shoe "; //用此来修饰 } }; //具体服饰类tshirts class concretesuit: public finery{ public: void show()const{ finery::show(); //调用基类方法 std::cout << "suit "; //用此来修饰 } }; //具体服饰类tshirts class concretehat: public finery{ public: void show()const{ finery::show(); //调用基类方法 std::cout << "hat "; //用此来修饰 } };
总结:
装饰模式是为已有功能动态地添加更多的功能的一种方式。
应该在什么时候使用呢?
如果在一个已有程序中添加的功能仅仅是为了满足只在某些特定情况下才会执行的特殊行为的需要,若在主类中添加代码会增加主类复杂度。这时就需要装饰模式把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象。
因为,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择的按顺序地使用装饰功能包装对象。
装饰模式的意思就是你自己的主体类和具体类该怎么写就怎么写,我想额外地增加特殊功能时,就使用额外的类来增加功能,这样做比生成子类更加灵活,不需要修改原主体类和具体类的代码。
这样将类中的装饰功能从主类搬移去除,可以简化原有的类。有效的把类的核心职责和装饰功能区分开来,而且可以去除相关类中重复的装饰逻辑。
以上就是c++设计模式浅识装饰模式的内容。
其它类似信息

推荐信息