特点:就是自己继承自己,在继承的过程中丰富自己。
作用:1.通过继承丰富机能
2.可以无限制的丰富机能(多维度的,与桥接相比桥接只有两个维度,这个模式和桥接相比就是可以桥接自己)
https://download.csdn.net/download/xie__jin__cheng/10962740(这里由该模式的实践举例)
https://download.csdn.net/download/xie__jin__cheng/10964696(这里有装载模式实现的代码)
因为现在系统将下载自动设置为了5分,所以将代码拷贝在下面
需求
代码
#include
using namespace std;
//////////////////////////////////////////////////////////////////////////
// ---------------------------------抽象层------------------------------
//////////////////////////////////////////////////////////////////////////
class Tank
{
public:
virtual void Shot(){}
virtual void Run(){}
};
class Decorator: public Tank
{
private:
Tank& tank;
public:
Decorator(Tank& tank_object):tank(tank_object){}
virtual void Shot()
{
tank.Shot();
}
};
//////////////////////////////////////////////////////////////////////////
// ------------------------------实施层----------------------------------
//////////////////////////////////////////////////////////////////////////
//Tank---------------------------------------------------------------
class TankB70: public Tank
{
public:
virtual void Shot()
{
cout<<“70 gong li she cheng”<<"\n";
}
};
class TankB90: public Tank
{
public:
virtual void Shot()
{
cout<<“90 gong li she cheng”<<"\n";
}
};
//Decorator---------------------------------------------------------------
class DecoratorA: public Decorator
{
public:
DecoratorA(Tank& tank_object):Decorator(tank_object)
{
}
void HongWaixian()
{
cout<<"hong wai xian"<<"\n";
}
public:
virtual void Shot()
{
Decorator::Shot();
HongWaixian();
}
};
class DecoratorB: public Decorator
{
private:
void ShuiLuLiangXi()
{
cout<<“shui lu liang xi”<<"\n";
}
public:
DecoratorB(Tank& tank_object):Decorator(tank_object)
{
}
virtual void Shot()
{
Decorator::Shot();
ShuiLuLiangXi();
}
};
class DecoratorC: public Decorator
{
private:
void WeiXingDingWei()
{
cout<<“wei xing ding wei”<<"\n";
}
public:
DecoratorC(Tank& tank_object):Decorator(tank_object)
{
}
virtual void Shot()
{
Decorator::Shot();
WeiXingDingWei();
}
};
//////////////////////////////////////////////////////////////////////////
// 客户端(调用层)
//////////////////////////////////////////////////////////////////////////
int main()
{
Tank* tank_90 = new TankB90();
Decorator* Decorator_a = new DecoratorA(*tank_90);
Decorator* Decorator_b = new DecoratorB(*Decorator_a);
Decorator_b->Shot();
int a;
cin>>a;
return 0;
}