案例:手机->贴膜->戴保护套->贴卡通贴纸->系手机挂绳
装饰模式
装饰模式定义(DecoratorPattern)
装饰模式是一种用于替代继承的技术。通过一种无须定义子类的方式给对象动态增加职责,使用对象之间的关联关系取代类之间的继承关系。装饰模式中引入了装饰类,在装饰类中既可以调用待装饰的原有对象的方法,还可以增加新的方法,以扩充原有类的功能。
组合模式和装饰模式
组合模式和装饰模式都可以用来透明的把对象包装在具有同样接口的另一个对象中。
组合模式和装饰模式都是结构型模式。组合模式着眼于把众多子对象组织为一个整体;装饰模式着眼于在不修改现有对象或从其派生子类的前提下为其增添职责,其子对象只有一个
装饰模式结构
- Component(抽象构件):是具有构件类和装饰类的共同基类,声明了在具体构件中定义的方法,客户端可以一致的对待使用装饰前后的对象
- ConcreteComponent(具体构件):具体构件定义了构件具体的方法,装饰类可以给它增加更多的功能
- Decorator(抽象装饰类):用于给具体构件增加职责,但具体职责在其子类中实现。抽象装饰类通过聚合关系定义一个抽象构件的对象,通过该对象可以调用装饰之前构件的方法,并用过其子类扩展该方法,达到装饰的目的
- ConcreteDecorator(具体装饰类):向构件增加新的功能
装饰模式示例
https://gitee.com/NiMiKiss/design-pattern.git
#ifndef _DECORATOR_PATTERN_
#define _DECORATOR_PATTERN_
#include<iostream>
class Component
{
public:
virtual void Operation() = 0;
};
class Phone :public Component
{
public:
void Operation()
{
std::cout << "Phone" << std::endl;
}
};
class Decorator :public Component
{
public:
Decorator() {}
Decorator(std::shared_ptr<Component> pComponent) :m_pComponent(pComponent) {}
std::shared_ptr<Component> GetComponent()const{ return m_pComponent; }
void SetComponent(std::shared_ptr<Component> pComponent) { m_pComponent = pComponent; }
virtual void Operation() { m_pComponent->Operation(); }
private:
std::shared_ptr<Component> m_pComponent;
};
class DecoratorFilm :public Decorator
{
public:
DecoratorFilm() {}
DecoratorFilm(std::shared_ptr<Component> pComponent) { SetComponent(pComponent); }
void Operation() { GetComponent()->Operation(); NewBehavior(); }
void NewBehavior() { std::cout << "贴膜" << std::endl; }
};
class DecoratorShell :public Decorator
{
public:
DecoratorShell() {}
DecoratorShell(std::shared_ptr<Component> pComponent) { SetComponent(pComponent); }
void Operation() { GetComponent()->Operation(); NewBehavior(); }
void NewBehavior() { std::cout << "戴保护套" << std::endl; }
};
class DecoratorSticker :public Decorator
{
public:
DecoratorSticker() {}
DecoratorSticker(std::shared_ptr<Component> pComponent) { SetComponent(pComponent); }
void Operation() { GetComponent()->Operation(); NewBehavior(); }
void NewBehavior() { std::cout << "贴卡通贴纸" << std::endl; }
};
class DecoratorRope :public Decorator
{
public:
DecoratorRope() {}
DecoratorRope(std::shared_ptr<Component> pComponent) { SetComponent(pComponent); }
void Operation() { GetComponent()->Operation(); NewBehavior(); }
void NewBehavior() { std::cout << "系手机挂绳" << std::endl; }
};
#endif // !_DECORATOR_PATTERN_
客户端调用
#include"Decorator_Pattern.h"
#include<iostream>
void Phone_1()
{
std::cout << "手机1" << std::endl;
auto spPhone_1 = std::shared_ptr<Phone>(new Phone());
auto spDecoratorFilm_1 = std::shared_ptr<DecoratorFilm>(new DecoratorFilm(spPhone_1));
auto spDecoratorShell_1 = std::shared_ptr<DecoratorShell>(new DecoratorShell(spDecoratorFilm_1));
auto spDecoratorSticker_1 = std::shared_ptr<DecoratorSticker>(new DecoratorSticker(spDecoratorShell_1));
auto spDecoratorRope_1 = std::shared_ptr<DecoratorRope>(new DecoratorRope(spDecoratorSticker_1));
spDecoratorRope_1->Operation();
}
void Phone_2()
{
std::cout << "手机2" << std::endl;
auto spPhone_2 = std::shared_ptr<Phone>(new Phone());
auto spDecoratorFilm_2 = std::shared_ptr<DecoratorFilm>(new DecoratorFilm(spPhone_2));
spDecoratorFilm_2->SetComponent(spPhone_2);
auto spDecoratorShell_2 = std::shared_ptr<DecoratorShell>(new DecoratorShell());
spDecoratorShell_2->SetComponent(spDecoratorFilm_2);
auto spDecoratorSticker_2 = std::shared_ptr<DecoratorSticker>(new DecoratorSticker());
spDecoratorSticker_2->SetComponent(spDecoratorShell_2);
auto spDecoratorRope_2 = std::shared_ptr<DecoratorRope>(new DecoratorRope());
spDecoratorRope_2->SetComponent(spDecoratorSticker_2);
spDecoratorRope_2->Operation();
}
int main()
{
Phone_1();
Phone_2();
return EXIT_SUCCESS;
}