论坛
门户
内部优惠
喜欢
话题
VIP会员
搜索
新浪微博
登录
注册
100%
100%
首页
>
软件开发
>
C/C++语言
>
用代码和UML图化解设计模式之《装饰模式》
回复
« 返回列表
灯火互联
管理员
注册日期
2011-07-27
发帖数
41778
QQ
火币
41290枚
粉丝
1086
关注
100
加关注
写私信
打招呼
阅读:
3098
回复:
0
[C++技术]
用代码和UML图化解设计模式之《装饰模式》
楼主
#
更多
只看楼主
倒序阅读
发布于:2012-10-30 10:30
保存
100%
100%
[]
1
这个模式看了两次,因为我有点不太理解,其实到现在也不太理解。
通过写代码,自我理解就是把对象重新装饰了一遍。通过继承同一个基类。而不用添加额外的类了。。。。
上图吧
通过修饰类达到我们想要的效果。修饰类通常初始化了基类。
[cpp]
// Decorator.cpp : 定义控制台应用程序的入口点。
//************************************************************************/
/* @filename Decorator.cpp
@author wallwind
@createtime 2012/10/29 22:42
@function 命令模式
@email */
/************************************************************************/
#include "stdafx.h"
#include <
ios
tream>
using namespace std;
class Widget
{
public:
Widget(){}
virtual ~Widget(){}
virtual void show()=0;
};
class TextField:public Widget
{
public:
TextField(int ix,int iy)
:x(ix),y(iy)
{
}
~TextField(){}
void show()
{
cout<<"x:"<<x<<endl;
cout<<"y:"<<y<<endl;
}
private:
int x;
int y;
};
class Decorator:public Widget
{
public:
Decorator(Widget* widget)
:m_widget(widget)
{}
virtual ~Decorator()
{
delete m_widget;
}
void show()
{
m_widget->show();
cout<<"Decorator:show()"<<endl;
}
private:
Widget* m_widget;
};
class BorderDecorator :public Decorator
{
public:
BorderDecorator(Widget* widget)
:Decorator(widget)
{
}
void show()
{
Decorator::show();
cout<<"BorderDecorator:show()"<<endl;
}
};
class ScrollDecorator :public Decorator
{
public:
ScrollDecorator(Widget* widget)
:Decorator(widget)
{
}
void show()
{
Decorator::show();
cout<<"ScrollDecorator:show()"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Widget* twidget = new TextField(2,3);
Widget *sd = new ScrollDecorator(twidget);
Widget *br = new BorderDecorator(twidget);
sd->show();
br->show();
return 0;
}
喜欢
0
评分
0
最新喜欢:
淘宝天猫隐藏优惠券地址
回复
100%
发帖
回复
« 返回列表
普通帖
您需要登录后才可以回帖,
登录
或者
注册
100%
返回顶部
关闭
最新喜欢