(本博客旨在个人总结回顾)
1、详情:
装饰模式:动态地给一个对象添加一些额外的职责,就扩展功能而言,装饰模式比生成子类更为灵活。
说明:
何时使用:在不想增加很多子类的情况下扩展类。
优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
缺点:多层装饰比较复杂。
使用场景: ①扩展一个类的功能。 ②动态增加功能,动态撤销。
2.1、UML类图:
2.2、例子源码
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
// TODO: 在此处引用程序需要的其他头文件
Component.h
#pragma once
class Component
{
public:
Component();
virtual ~Component();
public:
virtual void Operation() = 0;
};
Component.cpp
#include "stdafx.h"
#include "Component.h"
Component::Component()
{
}
Component::~Component()
{
}
ConcreteComponent.h
#pragma once
#include "Component.h"
class ConcreteComponent :
public Component
{
public:
ConcreteComponent();
~ConcreteComponent();
public:
void Operation();
};
ConcreteComponent.cpp
#include "stdafx.h"
#include "ConcreteComponent.h"
ConcreteComponent::ConcreteComponent()
{
}
ConcreteComponent::~ConcreteComponent()
{
}
void ConcreteComponent::Operation()
{
cout << "具体对象操作!" << endl;
}
Decorator.h
#pragma once
#include "Component.h"
class Decorator :
public Component
{
public:
Decorator();
virtual ~Decorator();
public:
void SetComponent(Component* pComponent);
void Operation();
private:
Component* m_pComponent;
};
Decorator.cpp
#include "stdafx.h"
#include "Decorator.h"
Decorator::Decorator()
: m_pComponent(NULL)
{
}
Decorator::~Decorator()
{
if (m_pComponent != NULL)
{
delete m_pComponent;
m_pComponent = NULL;
}
}
void Decorator::SetComponent(Component* pComponent)
{
if (m_pComponent != NULL)
{
delete m_pComponent;
m_pComponent = NULL;
}
m_pComponent = pComponent;
}
void Decorator::Operation()
{
if (m_pComponent != NULL)
{
m_pComponent->Operation();
}
}
ConcreteDecoratorA.h
#pragma once
#include "Decorator.h"
class ConcreteDecoratorA :
public Decorator
{
public:
ConcreteDecoratorA();
virtual ~ConcreteDecoratorA();
public:
void Operation();
private:
string addedState;
};
ConcreteDecoratorA.cpp
#include "stdafx.h"
#include "ConcreteDecoratorA.h"
ConcreteDecoratorA::ConcreteDecoratorA()
: addedState("")
{
}
ConcreteDecoratorA::~ConcreteDecoratorA()
{
}
void ConcreteDecoratorA::Operation()
{
Decorator::Operation();
addedState = "New State";
cout << "动态添加装饰A功能" << endl;
}
ConcreteDecoratorB.h
#pragma once
#include "Decorator.h"
class ConcreteDecoratorB :
public Decorator
{
public:
ConcreteDecoratorB();
virtual ~ConcreteDecoratorB();
public:
void Operation();
private:
void AddedBehavior();
};
ConcreteDecoratorB.cpp
#include "stdafx.h"
#include "ConcreteDecoratorB.h"
ConcreteDecoratorB::ConcreteDecoratorB()
{
}
ConcreteDecoratorB::~ConcreteDecoratorB()
{
}
void ConcreteDecoratorB::AddedBehavior()
{
}
void ConcreteDecoratorB::Operation()
{
Decorator::Operation();
AddedBehavior();
cout << "动态添加装饰B功能!" << endl;
}
调用代码
DecoratorPatternMemo.cpp
// DecoratorPatternMemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ConcreteComponent.h"
#include "ConcreteDecoratorA.h"
#include "ConcreteDecoratorB.h"
int _tmain(int argc, _TCHAR* argv[])
{
ConcreteComponent* pConcreteComponent = new ConcreteComponent();
ConcreteDecoratorA* pConcreteDecoratorA = new ConcreteDecoratorA();
ConcreteDecoratorB* pConcreteDecoratorB = new ConcreteDecoratorB();
pConcreteDecoratorA->SetComponent(pConcreteComponent);
pConcreteDecoratorB->SetComponent(pConcreteComponent);
pConcreteDecoratorA->Operation();
cout << endl;
pConcreteDecoratorB->Operation();
system("pause");
return 0;
}
2.3、运行结果: