设计模式:6 装饰模式

来源:互联网 发布:2017淘宝摇一摇在哪里 编辑:程序博客网 时间:2024/06/06 10:08

装饰模式:

动态地给一个对象添加一些额外职责。增加功能比生成子类更加灵活。

模式图:

Component:定义对象接口,给对象动态添加职责

ConcreteComponent:定义具体对象,给对象添加职责

Decorator:装饰抽象类,继承自Component,从外类来扩展Coponent类的功能,Component无需知道Decorator的存在

ConcreteDecorator:具体的装饰对象,能给Component添加职责

 

本质:

装饰模式利用setComponent来对对象进行包装,每个装饰对象的实现就和如何使用这个对象分离开,每个装饰对象只需要关注自己的功能,不需要关心如何被添加到对象链中

 

技巧:

如果只有一个ConcreteComponent类,而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。

如果只有一个ConcreteDecorator类,那么没有必要建立一个单独的Decorator类,可以把Decorator类和ConcreteDecorator类的责任合并成一个类。

 

因此:这里只有一个具体装饰类即,服装类,那么就将唯一具体装饰类:服装类作为抽象装饰类和具体装饰类合并成一个类。

只有一个具体组件类:人,没有抽象组件类,就让装饰类成为具体组件类:人的子类

 

总结:

装饰模式是为已有功能动态添加更多功能,新加入的东西只是为了满足某特定情况下才会执行的行为,它把每个需要装饰的功能放在单独的类中,并用这个类包装他要装饰的对象。

 

把类的核心职责和装饰功能区分开,去除相关类中重复的装饰逻辑。

 

 main.cpp

#include <iostream>#include <stdlib.h>#include "ConcreteComponent.h"#include "ConcreteDecoratorA.h"#include "ConcreteDecoratorB.h"using namespace std;void process(){ConcreteComponent c;ConcreteDecoratorA d1;ConcreteDecoratorB d2;/*装饰的方法:先调用ConcreteComponnet实例化对象c,然后用ConcreteDecoratorA的实例化对象d1来包装c,再用ConcreteDecoratorB的对象d2包装d1,最终执行d2的operation*/d1.setComponent(c);d2.setComponent(d1);d2.operation();}int main(int argc,char* argv[]){process();system("pause");return 0;}


Component.h

#pragma onceclass Component{public:Component(void);virtual ~Component(void);//virtual void operation()=0;virtual void operation(){}};


ConcreteComponent.h

#pragma once#include "Component.h"class ConcreteComponent :public Component{public:ConcreteComponent(void);~ConcreteComponent(void);void operation();};

 

ConcreteComponent.cpp

#include "ConcreteComponent.h"#include <iostream>ConcreteComponent::ConcreteComponent(void){}ConcreteComponent::~ConcreteComponent(void){}void ConcreteComponent::operation(){std::cout << "具体对象的操作" << std::endl;}


Decorator.h

#pragma once#include "component.h"class Decorator : public Component{public: Decorator(void); virtual ~Decorator(void); void setComponent(Component& component); void operation();protected: Component _component;};


Decorator.cpp

#include "Decorator.h"Decorator::Decorator(void){}Decorator::~Decorator(void){}void Decorator::operation(){_component.operation();}void Decorator::setComponent(Component& component){_component = component;}

 

ConcreteDecoratorA.h

#pragma once#include "decorator.h"#include <string>class ConcreteDecoratorA :public Decorator{public:ConcreteDecoratorA(void);~ConcreteDecoratorA(void);void operation();private:std::string _addedState;//本类的独有功能,用于区别ConcreteDecoratorB};


ConcreteDecoratorA.cpp

#include "ConcreteDecoratorA.h"#include <iostream>ConcreteDecoratorA::ConcreteDecoratorA(void){}ConcreteDecoratorA::~ConcreteDecoratorA(void){}void ConcreteDecoratorA::operation(){_component.operation();//首先运行原来Component的operation,再执行本类的功能,相当于对原有的Component进行类装饰_addedState = "新状态";std::cout << "具体装饰对象A的操作" << std::endl;}


ConcreteDecoratorB.h

#pragma once#include "decorator.h"class ConcreteDecoratorB :public Decorator{public:ConcreteDecoratorB(void);~ConcreteDecoratorB(void);void operation();private:void addedBehavior();};


ConcreteDecoratorB.cpp

#include "ConcreteDecoratorB.h"#include <iostream>ConcreteDecoratorB::ConcreteDecoratorB(void){}ConcreteDecoratorB::~ConcreteDecoratorB(void){}void ConcreteDecoratorB::operation(){_component.operation();addedBehavior();std::cout << "具体装饰对象B的操作" << std::endl;}void ConcreteDecoratorB::addedBehavior(){}



 

 


 

0 0
原创粉丝点击