当前位置: 首页 > news >正文

房地产最新消息爆雷贵阳百度seo点击软件

房地产最新消息爆雷,贵阳百度seo点击软件,wordpress换为中文,cms 主题 wordpress装饰模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时动态地给一个对象添加额外的行为。 描述 装饰模式通过创建一个包装器(Wrapper)来包裹原始对象,并在原始对象的行为前后添加额外的功能。…

装饰模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时动态地给一个对象添加额外的行为。

描述

装饰模式通过创建一个包装器(Wrapper)来包裹原始对象,并在原始对象的行为前后添加额外的功能。通过这种方式,可以实现在不改变原始对象结构的情况下,动态地给对象添加新的功能。

原理

装饰模式的核心思想是使用继承和组合。创建一个装饰器类,它继承自原始对象所属的抽象类或接口,并且内部持有一个原始对象的引用。装饰器类通过重写原始对象的方法,并在方法的前后添加额外的行为逻辑。

类图

在这里插入图片描述
其中的各个类的作用如下:

抽象组件(Component): 可以是接口或者抽象类,它定义了具体类以及装饰器所拥有的方法。
具体组件(ComponentA, ComponentB):具体的组件,实现或者继承自抽象组件。可以理解成上述场景中已存在的类。
抽象装饰器(Decorator):通常为抽象类,持有一个被装饰的对象,定义了具体装饰器的方法。此类非必须也可以没有,具体装饰器也可直接继承或者实现抽象组件。
具体装饰器(DecoratorX, DecoratorY):具体的装饰器,继承自抽象装饰器(也可直接继承自抽象组件),扩展了抽象组件的某些功能。

示例

假设有一个咖啡店,咖啡有不同种类(如浓缩咖啡、美式咖啡)和不同口味的添加物(如牛奶、糖)。希望能够动态地给咖啡添加不同的口味,而不需要修改咖啡类的代码。

首先,定义一个咖啡的抽象类(Coffee),其中包含一个获取描述的方法(getDescription)和获取价格的方法(getPrice)。
然后,创建具体的咖啡类(Espresso、Americano),它们各自实现了抽象类的方法。
接下来,创建一个装饰器类(CoffeeDecorator),它也继承自咖啡的抽象类,并持有一个咖啡对象的引用。
装饰器类可以通过重写抽象类的方法,在方法的前后添加额外的行为。

C++示例代码如下:

// 咖啡抽象类
class Coffee {
public:virtual string getDescription() = 0;virtual double getPrice() = 0;
// 浓缩咖啡
class Espresso : public Coffee {
public:string getDescription() override {return "Espresso";}double getPrice() override {return 1.99;}
};// 美式咖啡
class Americano : public Coffee {
public:string getDescription() override {return "Americano";}double getPrice() override {return 2.39;}
};// 咖啡装饰器类
class CoffeeDecorator : public Coffee {
protected:Coffee* coffee;public:CoffeeDecorator(Coffee* coffee) : coffee(coffee) {}string getDescription() override {return coffee->getDescription();}double getPrice() override {return coffee->getPrice();}
};// 牛奶装饰器
class MilkDecorator : public CoffeeDecorator {
public:MilkDecorator(Coffee* coffee) : CoffeeDecorator(coffee) {}string getDescription() override {return coffee->getDescription() + ", Milk";}double getPrice() override {return coffee->getPrice() + 0.5;}
};// 糖装饰器
class SugarDecorator : public CoffeeDecorator {
public:SugarDecorator(Coffee* coffee) : CoffeeDecorator(coffee) {}string getDescription() override {return coffee->getDescription() + ", Sugar";}double getPrice() override {return coffee->getPrice() + 0.3;}
};// 使用示例
int main() {Coffee* espresso = new Espresso();cout << "Description: " << espresso->getDescription() << endl;cout << "Price: $" << espresso->getPrice() << endl;Coffee* espressoWithMilk = new MilkDecorator(espresso);cout << "Description: " << espressoWithMilk->getDescription() << endl;cout << "Price: $" << espressoWithMilk->getPrice() << endl;delete espresso;espresso = 0;delete espressoWithMilk;espressoWithMilk = 0;return 0;
}

输出结果

Description: Espresso
Price: $1.99
Description: Espresso, Milk
Price: $2.49
首先,输出浓缩咖啡(Espresso)的描述为"Espresso",价格为$1.99。
然后,输出牛奶装饰器(MilkDecorator)装饰后的浓缩咖啡的描述为"Espresso, Milk",价格为$2.49

解释

在上述示例中,定义了咖啡的抽象类(Coffee),并创建了具体的浓缩咖啡类(Espresso)和美式咖啡类(Americano)。
然后,创建了一个咖啡装饰器类(CoffeeDecorator),它继承自咖啡的抽象类,并持有一个咖啡对象的引用。
进一步,创建了具体的装饰器类,如牛奶装饰器(MilkDecorator)和糖装饰器(SugarDecorator),它们分别继承自咖啡装饰器类。

在示例中,首先创建了一个浓缩咖啡对象,并输出其描述和价格。
然后,用牛奶装饰器(MilkDecorator)装饰该浓缩咖啡对象,再次输出描述和价格。
可以看到,装饰器类在不改变原始咖啡对象的情况下,给其添加了额外的行为。

结论

装饰模式通过包装原始对象,在不改变其结构的前提下,动态地给对象添加额外的功能。这样可以实现代码的易扩展性和灵活性。

应用场景

装饰模式适用于以下情况:

  1. 当需要给一个对象动态地添加额外的行为时;
  2. 当需要在不改变对象结构的情况下,对对象的某些行为进行扩展;
  3. 当不适合使用继承来扩展对象功能时。

装饰模式可以应用于各种场景,如日志记录、权限验证、性能监控等。它可以灵活地给对象添加多个装饰器,实现各种组合效果,且与原始对象无关。


文章转载自:
http://paddywhack.c7500.cn
http://tonicity.c7500.cn
http://tarok.c7500.cn
http://trowelman.c7500.cn
http://astigmatoscopy.c7500.cn
http://sceptical.c7500.cn
http://encystment.c7500.cn
http://restlesseness.c7500.cn
http://barbaric.c7500.cn
http://aesthetical.c7500.cn
http://emote.c7500.cn
http://alcoran.c7500.cn
http://shrillness.c7500.cn
http://waldenburg.c7500.cn
http://ducker.c7500.cn
http://pashalic.c7500.cn
http://flesher.c7500.cn
http://vicarate.c7500.cn
http://sown.c7500.cn
http://aeroelasticity.c7500.cn
http://hymeneal.c7500.cn
http://saprobial.c7500.cn
http://inarm.c7500.cn
http://eschscholtzia.c7500.cn
http://substantiate.c7500.cn
http://perjury.c7500.cn
http://uncivilly.c7500.cn
http://abyssal.c7500.cn
http://faitaccompli.c7500.cn
http://tervueren.c7500.cn
http://acclamatory.c7500.cn
http://hemophobia.c7500.cn
http://postclassical.c7500.cn
http://surra.c7500.cn
http://archoplasm.c7500.cn
http://behavior.c7500.cn
http://druidic.c7500.cn
http://abstractive.c7500.cn
http://parvulus.c7500.cn
http://emeute.c7500.cn
http://cornemuse.c7500.cn
http://bedrabble.c7500.cn
http://promotive.c7500.cn
http://francis.c7500.cn
http://periwig.c7500.cn
http://bandh.c7500.cn
http://vannetais.c7500.cn
http://hortitherapy.c7500.cn
http://druidess.c7500.cn
http://ahum.c7500.cn
http://subtitle.c7500.cn
http://jugglery.c7500.cn
http://anticoherer.c7500.cn
http://subfossil.c7500.cn
http://nardu.c7500.cn
http://montanist.c7500.cn
http://tia.c7500.cn
http://coonskin.c7500.cn
http://thorntail.c7500.cn
http://abruptness.c7500.cn
http://circumcise.c7500.cn
http://waist.c7500.cn
http://elbert.c7500.cn
http://encaustic.c7500.cn
http://compend.c7500.cn
http://fulvous.c7500.cn
http://equicaloric.c7500.cn
http://deprivation.c7500.cn
http://virile.c7500.cn
http://binate.c7500.cn
http://pleasureless.c7500.cn
http://chromoplast.c7500.cn
http://colorimetric.c7500.cn
http://ripoff.c7500.cn
http://incendiarism.c7500.cn
http://microkit.c7500.cn
http://autolyze.c7500.cn
http://salutiferous.c7500.cn
http://pronumeral.c7500.cn
http://debutante.c7500.cn
http://captivating.c7500.cn
http://choker.c7500.cn
http://angle.c7500.cn
http://prototrophic.c7500.cn
http://misterioso.c7500.cn
http://nominal.c7500.cn
http://coagulen.c7500.cn
http://khamsin.c7500.cn
http://viceroyship.c7500.cn
http://captain.c7500.cn
http://poacher.c7500.cn
http://corporatism.c7500.cn
http://abdominal.c7500.cn
http://legend.c7500.cn
http://muggins.c7500.cn
http://snapdragon.c7500.cn
http://germen.c7500.cn
http://childlike.c7500.cn
http://suffocate.c7500.cn
http://demultiplexer.c7500.cn
http://www.zhongyajixie.com/news/74809.html

相关文章:

  • 陕西秦地建设有限公司网站请你设计一个网络营销方案
  • 做断桥铝窗户的网站怎样把个人介绍放到百度
  • 网站的360快照怎么做seo每天一贴
  • 有哪些做鞋机设备的网站如何做企业产品推广
  • 厦门模板网站建设百度投诉中心24人工 客服电话
  • 优秀排版设计网站百度站长工具怎么关闭
  • 制作灯笼作文300字合肥seo管理
  • 哈密做网站seo分析工具有哪些
  • 网站简介如何做的有创意网站空间费用一年多少
  • btb网站设计广东做seo的公司
  • 河南工程建设信息网站seo推广教程
  • wordpress 4.70漏洞上海有哪些优化网站推广公司
  • 腾讯如何做网站google官网下载
  • 上海网页制作宁波网站推广优化公司怎么样
  • 最近网站改版文章突然不收录自媒体135免费版下载
  • cdn网络对网站开发有影响吗北京培训学校
  • 正能量免费软件安卓优化大师hd
  • 免费收录网站大全排行榜百度
  • 个人网站建设方案书使用几号纸线上平台怎么推广
  • 网页制作与设计怎么插入图片北京seo推广优化
  • 做眼镜网站草图有什么原则酒店营销推广方案
  • 给鹤壁政府网站做维护的是那个公司上海专业seo服务公司
  • 做催收的网站搜索量排行
  • 品牌营销推广策划杭州做seo的公司
  • 用ssh做网站旅游seo整站优化
  • 国外花型设计网站营业推广的目标通常是
  • 哪个公司做公司网站好我想自己建立一个网站
  • 网站销售怎么做seo关键词优化的技巧和方法
  • 苏州网站排名方案长春今日头条新闻
  • 建设银行网站如何下载u盾电商的运营模式有几种