// 所有原創文章轉載請注明作者及連結
// blackboycpp(AT)gmail.com
// QQ群: 135202158
類圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuMTOtpWO5YzM2MDMwMTMfBzLcdTMvw1MwETMwIzLcRnbl1GajFGd0F2LcRXZu5ibkN3YukGavw1LcpDc0RHaiojIsJye.gif)
代碼實作:
類聲明:
/********************************************************************
File: decorator.h
Author: blackboy [email protected]
Purpose: 裝飾模式
Created: 2011-03-16
Modified: 2011-03-16 19:48
*********************************************************************/
#ifndef __DECORATOR_H__
#define __DECORATOR_H__
namespace blackboycpp
{
//
class Component
{
public:
virtual void Operation() = 0;
};
class ConcreteComponent : public Component
{
public:
virtual void Operation();
};
//
class Decorator : public Component
{
public:
Decorator();
Decorator(Component*);
virtual void Operation();
private:
Component* _component;
};
class ConcreteDecoratorA : public Decorator
{
public:
ConcreteDecoratorA();
ConcreteDecoratorA(Component*);
void Operation();
private:
void AddedState();
};
class ConcreteDecoratorB : public Decorator
{
public:
ConcreteDecoratorB();
ConcreteDecoratorB(Component*);
void Operation();
private:
void AddedBehavior();
};
}
#endif
類實作:
#include <iostream>
#include "decorator.h"
namespace blackboycpp
{
//
void ConcreteComponent::Operation()
{
std::cout << "ConcreteComponent::Operation()/n";
}
//
Decorator::Decorator()
{
std::cout << "Decorator::Decorator()/n";
_component = 0;
}
Decorator::Decorator(Component* p)
{
std::cout << "Decorator::Decorator(Component*)/n";
_component = p;
}
void Decorator::Operation()
{
std::cout << "Decorator::Operation()/n";
_component->Operation();
}
//
ConcreteDecoratorA::ConcreteDecoratorA() : Decorator()
{
std::cout << "ConcreteDecoratorA::ConcreteDecoratorA()/n";
}
ConcreteDecoratorA::ConcreteDecoratorA(Component* p) : Decorator(p)
{
std::cout << "ConcreteDecoratorA::ConcreteDecoratorA(Component*)/n";
}
void ConcreteDecoratorA::Operation()
{
std::cout << "ConcreteDecoratorA::Operation()/n";
Decorator::Operation();
AddedState();
}
void ConcreteDecoratorA::AddedState()
{
std::cout << "ConcreteDecoratorA::AddedState()/n";
}
//
ConcreteDecoratorB::ConcreteDecoratorB() : Decorator()
{
std::cout << "ConcreteDecoratorB::ConcreteDecoratorB()/n";
}
ConcreteDecoratorB::ConcreteDecoratorB(Component* p) : Decorator(p)
{
std::cout << "ConcreteDecoratorB::ConcreteDecoratorB(Component*)/n";
}
void ConcreteDecoratorB::Operation()
{
std::cout << "ConcreteDecoratorB::Operation()/n";
Decorator::Operation();
AddedBehavior();
}
void ConcreteDecoratorB::AddedBehavior()
{
std::cout << "ConcreteDecoratorB::AddedBehavior()/n";
}
}
main():
#include <cstdlib>
#include "decorator.h"
using namespace blackboycpp;
int main(int argc, char** argv)
{
Component* component = new ConcreteComponent;
ConcreteDecoratorA* decoratorA = new ConcreteDecoratorA(component);
ConcreteDecoratorB* decoratorB = new ConcreteDecoratorB(decoratorA);
decoratorB->Operation();
delete decoratorB;
delete decoratorA;
delete component;
system("pause");
return 0;
}