装饰者模式——探索之旅
2015-06-23 22:54
489 查看
装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
#pragma once
#include <iostream>
using namespace std;
struct IComponent
{
virtual void operation() = 0;
};
class ConcreteComponent :public IComponent
{
public:
void operation()
{
cout << "call ConcreteComponent operation" << endl;
}
};
class Decorator :public IComponent
{
public:
Decorator()
:_comp(nullptr)
{}
virtual ~Decorator()
{
delete _comp;
_comp = nullptr;
}
virtual void operation()
{
_comp->operation();
cout << "call Decorator operation" << endl;
}
virtual void setComponent(IComponent* comp)
{
_comp = comp;
}
protected:
IComponent* _comp;
};
class ConcreteDecoratorA :public Decorator
{
public:
virtual void operation()
{
_comp->operation();
addedState();
cout << "call ConcreteDecoratorA operation" << endl;
}
virtual void addedState()
{
cout << "call ConcreteDecoratorA addedState" << endl;
}
};
class ConcreteDecoratorB :public Decorator
{
public:
virtual void operation()
{
_comp->operation();
addedBehavior();
cout << "call ConcreteDecoratorB operation" << endl;
}
virtual void addedBehavior()
{
cout << "call ConcreteDecoratorB addedBehavior" << endl;
}
};
void decoratorTest()
{
IComponent *concreteComp = new ConcreteComponent;
Decorator *concreteDectA = new ConcreteDecoratorA;
concreteDectA->setComponent(concreteComp);
concreteDectA->operation();
Decorator *concreteDectB = new ConcreteDecoratorB;
concreteDectB->setComponent(concreteDectA);
concreteDectB->operation();
}
#pragma once
#include <iostream>
using namespace std;
struct IComponent
{
virtual void operation() = 0;
};
class ConcreteComponent :public IComponent
{
public:
void operation()
{
cout << "call ConcreteComponent operation" << endl;
}
};
class Decorator :public IComponent
{
public:
Decorator()
:_comp(nullptr)
{}
virtual ~Decorator()
{
delete _comp;
_comp = nullptr;
}
virtual void operation()
{
_comp->operation();
cout << "call Decorator operation" << endl;
}
virtual void setComponent(IComponent* comp)
{
_comp = comp;
}
protected:
IComponent* _comp;
};
class ConcreteDecoratorA :public Decorator
{
public:
virtual void operation()
{
_comp->operation();
addedState();
cout << "call ConcreteDecoratorA operation" << endl;
}
virtual void addedState()
{
cout << "call ConcreteDecoratorA addedState" << endl;
}
};
class ConcreteDecoratorB :public Decorator
{
public:
virtual void operation()
{
_comp->operation();
addedBehavior();
cout << "call ConcreteDecoratorB operation" << endl;
}
virtual void addedBehavior()
{
cout << "call ConcreteDecoratorB addedBehavior" << endl;
}
};
void decoratorTest()
{
IComponent *concreteComp = new ConcreteComponent;
Decorator *concreteDectA = new ConcreteDecoratorA;
concreteDectA->setComponent(concreteComp);
concreteDectA->operation();
Decorator *concreteDectB = new ConcreteDecoratorB;
concreteDectB->setComponent(concreteDectA);
concreteDectB->operation();
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- PropertyChangeListener简单理解
- 关于指针的一些事情
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 设计模式之创建型模式
- 浅谈设计模式的学习
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解