软考设计模式——装饰模式(C++)
来源:互联网 发布:网络爬虫php 编辑:程序博客网 时间:2024/05/21 23:14
装饰模式就是在不改变原来接口的情况下,给类添加功能。
举个例子,在游戏里玩一个英雄,可以拿装备,拿的这些个装备就相当于是在装饰自己。
其实有一个诀窍,就是这些类里有一个方法名字都一样,然后这些类先从外到内,然后从里面开始一层层向外嵌套调用,相当于一直给这个方法里加东西
#include <iostream>#include <string>using namespace std;class Hero{ //基类Hero类public:string hero_name;Hero(){};Hero(string name):hero_name(name){} //为英雄添加一个名字virtual void showHero()=0; //设置纯虚函数,每个继承者都必须重写这个函数 };class NintendoHero:public Hero{public:string hero_name;NintendoHero(string name):hero_name(name),Hero(name){} //初始化,并把name传给Hero,以便后面调用void showHero(){cout<<"创建任天堂英雄:"<<hero_name<<endl;}};class BlizzardHero:public Hero{public:string hero_name;BlizzardHero(string name):hero_name(name),Hero(name){}void showHero(){cout<<"创建暴雪英雄:"<<hero_name<<endl;}};
接下来设计装饰类,装饰谁就继承谁
class Item:public Hero{ //设计道具装饰抽象类,来装饰英雄private:Hero *item_hero;public:Item(Hero *hero):item_hero(hero),Hero(hero->hero_name){}virtual void showHero(){item_hero->showHero();}};class HandItem :public Item{ //手持武器的具体装饰类private:Hero *hero;public:HandItem(Hero *hero):Item(hero){this->hero = hero;}void showHero(){this->hero->showHero(); //也可以使用Item::showHero()this->add(this->hero);}void add(Hero *hero){cout<<hero->hero_name<<"装备了一把剑"<<endl;}};class BodyItem :public Item{ //盔甲的具体装饰类private:Hero *hero;public:BodyItem(Hero *hero):Item(hero){this->hero = hero;}void showHero(){this->hero->showHero();this->add(this->hero);}void add(Hero *hero){cout<<hero->hero_name<<"装备了一套盔甲"<<endl;}};
客户实现:
int main(){Hero *hero = new NintendoHero("塞尔达");Hero *hand_item = new HandItem(hero);Hero *body_item = new BodyItem(hand_item);body_item->showHero(); //用最后一个对象来调用showHero()getchar();return 0;}
很多人可能不明白程序是怎么调用的,这里说一下。
1.在调用body_item的showHero()时,进入Body::showHero,注意这里用的是this->hero(传入的*hero参数),而this->hero指得是HandItem这个类的对象
2.然后进入HandItem,发现他的this->hero调用的方法其实是NintendoHero这个类的
3.进入NintendoHero,然后输出创建了英雄。这里嵌套的最内层算是执行完了
4.进入HandItem,添加武器,再往外跳一层
5.进入BodyItem,已经是最外层,再执行add添加盔甲,程序执行完毕
阅读全文
0 0
- 软考设计模式——装饰模式(C++)
- 【软考学习】设计模式——装饰模式
- 软考总结——装饰模式
- 设计模式——装饰者模式(c++)
- 设计模式——装饰者模式(C++)
- 设计模式—装饰模式(三)
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式—装饰模式
- 设计模式——装饰设计模式
- 设计模式——装饰设计模式
- 设计模式——装饰设计模式
- 设计模式(一)——装饰设计模式
- 【软考】——重温设计模式☞命令模式
- 【软考学习】设计模式——单例模式
- 【软考学习】设计模式——建造者模式
- AndroidVideoCache-视频边播放边缓存的代理策略
- C#类
- SSH用法
- 矩阵的逆,余子式,代数余子式,增广矩阵
- Lambda 表达式有何用处?如何使用?
- 软考设计模式——装饰模式(C++)
- 阿里史上首款AI硬件设备,为何如此“听话”?
- mysql格式化日期
- 套接字中connect阻塞与非阻塞问题
- 安卓中Rxjava(观察者模式、异步)的简单使用(1) 观察者与被观察者线程之间的关系
- 安卓自定义view
- android TextView滚动和转义换行等相关问题
- python I/O编程小笔记
- windows系统 忘记mysql密码解决方案