博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
装饰者模式
阅读量:5291 次
发布时间:2019-06-14

本文共 3347 字,大约阅读时间需要 11 分钟。

   装饰者模式:动态地将责任附加到对象上。想要扩展功能,装饰者提供有别于继承的另一种选择。——《HEAD FIRST 设计模式》

   我的c++代码:

#ifndef DESIGN_DECORATOR_H_#define DESIGN_DECORATOR_H_#include 
class Beverage{public: virtual ~Beverage(){}public: virtual std::string getDescription(); virtual int cost();protected: std::string m_descri;};class CondimentDecorator:public Beverage{public: virtual ~CondimentDecorator(){}};////class HouseBlend:public Beverage{public: HouseBlend(); virtual ~HouseBlend(){}public: virtual std::string getDescription(); virtual int cost();};class DarkRoast:public Beverage{public: DarkRoast(); virtual ~DarkRoast(){}public: virtual std::string getDescription(); virtual int cost();};class Espresso:public Beverage{public: Espresso(); virtual ~Espresso(){}public: virtual std::string getDescription(); virtual int cost();};class Decaf:public Beverage{public: Decaf(); virtual ~Decaf(){}public: virtual std::string getDescription(); virtual int cost();};////class Milk:public CondimentDecorator{public: Milk(Beverage* b); virtual ~Milk(){}public: virtual std::string getDescription(); virtual int cost();private: Beverage* m_b;};class Mocha:public CondimentDecorator{public: Mocha(Beverage* b); virtual ~Mocha(){}public: virtual std::string getDescription(); virtual int cost();private: Beverage* m_b;};class Soy:public CondimentDecorator{ public: Soy(Beverage* b); virtual ~Soy(){} public: virtual std::string getDescription(); virtual int cost(); private: Beverage* m_b;};class Whip:public CondimentDecorator{public: Whip(Beverage* b); virtual ~Whip(){}public: virtual std::string getDescription(); virtual int cost();private: Beverage* m_b;};#endif // DESIGN_DECORATOR_H_
#include "decorator.h"std::string Beverage::getDescription(){    return this->m_descri;}int Beverage::cost(){    return 0;}HouseBlend::HouseBlend(){    this->m_descri = "HouseBlend";}std::string HouseBlend::getDescription(){    return Beverage::getDescription();}int HouseBlend::cost(){    return 1;}DarkRoast::DarkRoast(){    this->m_descri = "DarkRoast";}std::string DarkRoast::getDescription(){    return Beverage::getDescription();}int DarkRoast::cost(){    return 2;}Espresso::Espresso(){    this->m_descri = "Espresso";}std::string Espresso::getDescription(){    return Beverage::getDescription();}int Espresso::cost(){    return 3;}Decaf::Decaf(){    this->m_descri = "Decaf";}std::string Decaf::getDescription(){    return Beverage::getDescription();}int Decaf::cost(){    return 4;}Milk::Milk(Beverage* b){    this->m_b = b;}std::string Milk::getDescription(){    return this->m_b->getDescription() + ",Milk";}int Milk::cost(){    return this->m_b->cost() + 1;}Mocha::Mocha(Beverage* b){    this->m_b = b;}std::string Mocha::getDescription(){    return this->m_b->getDescription() + ",Mocha";}int Mocha::cost(){    return this->m_b->cost() + 2;}Soy::Soy(Beverage* b){    this->m_b = b;}std::string Soy::getDescription(){    return this->m_b->getDescription() + ",Soy";}int Soy::cost(){    return this->m_b->cost() + 3;}Whip::Whip(Beverage* b){    this->m_b = b;}std::string Whip::getDescription(){    return this->m_b->getDescription() + ",Whip";}int Whip::cost(){    return this->m_b->cost() + 4;}
         
个人感悟:待留。

转载于:https://www.cnblogs.com/foolbread/p/4450719.html

你可能感兴趣的文章
C语言程序设计II—第九周教学
查看>>
C# 获取系统时间及时间格式转换
查看>>
WCF、WebAPI、WCFREST、WebService之间的区别
查看>>
2018-2019-2-20175332-实验四《Android程序设计》实验报告
查看>>
全栈12期的崛起之捡点儿有用的说说
查看>>
基础类型
查看>>
属性动画
查看>>
标识符
查看>>
Swift 常量&变量
查看>>
Sqli labs系列-less-4 这关好坑!!!
查看>>
路由跟踪工具0trace
查看>>
给大家分享一张CSS选择器优选级图谱 !
查看>>
Win7中不能调试windows service
查看>>
T-SQL触发器,限制一次只能删除一条数据
查看>>
boost库使用:vs2013下boost::container::vector编译出错解决
查看>>
通过httplib2 探索的学习的最佳方式
查看>>
理解运算符重载 4
查看>>
快来熟练使用 Mac 编程
查看>>
第二周
查看>>
断言简介
查看>>