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

郑州做网站哪里好怎样推广小程序平台

郑州做网站哪里好,怎样推广小程序平台,常熟祥云平台网站建设,做宠物食品的网站设计模式 15 Decorator Pattern 装饰器模式 1.定义 Decorator Pattern 装饰器模式是一种结构型设计模式,它允许在运行时给对象添加新的行为或职责,而无需修改对象的源代码。这种模式通过创建一个包装对象,也称为装饰器,来包裹原…
设计模式 15 Decorator Pattern 装饰器模式
1.定义

Decorator Pattern 装饰器模式是一种结构型设计模式,它允许在运行时给对象添加新的行为或职责,而无需修改对象的源代码。这种模式通过创建一个包装对象,也称为装饰器,来包裹原始对象,装饰器对象与原始对象有相同的接口,因此可以在不改变客户端代码的情况下,增加或修改对象的功能。

装饰器模式的优点包括:

动态地给对象添加新的行为,而无需修改对象的源代码或继承结构。
可以独立地增加对象的功能,因为每个装饰器都是独立的类。
保持了类的单一职责,使得代码更易于维护和扩展。
装饰器模式通常用于添加非核心功能,如日志、性能追踪、缓存等,而不会影响对象的核心行为。


2.内涵


 Decorator Pattern 的主要组成部分:

  • Component(组件):这是定义对象接口的抽象类或接口。所有可以装饰的对象都必须实现这个接口,这样装饰器才能与它们互换。
  • Concrete Component(具体组件):这是 Component 接口的实现,是将要被装饰的对象。它定义了实际的行为和职责。
  • Decorator(装饰器):这是 Component 接口的实现,它持有一个 Component 对象的引用。装饰器可以是抽象的,也可以包含具体的行为。装饰器对象可以添加新的行为或修改 Component 对象的行为。
  • Concrete Decorator(具体装饰器):这是 Decorator 的具体实现,它给 Component 添加新的行为或职责。每个 Concrete Decorator 都可以添加不同的功能,也可以堆叠多个 Decorator 来增强对象的功能。

组件之间的调用图如下图所示:

+------------+| Component  |+------------+|| 继承/实现V+--------------+|  Decorator   |+--------------+|| 继承/实现V+-------------------+| Concrete Decorator|+-------------------+|| 持有V+-------------------+| Concrete Component|+-------------------+


每个模块的作用如下:

  • Component Interface:定义了公共接口,使得装饰器和组件可以互相协作。
  • Concrete Component:实现了 Component 接口,定义了具体的行为和状态,是被装饰的对象。
  • Decorator:作为抽象装饰器,持有 Component 的引用,实现 Component 接口,以保持与组件的兼容性。
  • Concrete Decorator:具体实现了装饰器的逻辑,添加或修改了 Concrete Component 的行为。可以有多个 Concrete Decorator,每个实现不同的增强功能。

3.使用示例

#include <iostream>
#include <string>using namespace std;// Component interface - defines the basic ice cream
// operations.
class IceCream {
public:virtual string getDescription() const = 0;virtual double cost() const = 0;
};// Concrete Component - the basic ice cream class.
class VanillaIceCream : public IceCream {
public:string getDescription() const override{return "Vanilla Ice Cream";}double cost() const override { return 160.0; }
};// Decorator - abstract class that extends IceCream.
class IceCreamDecorator : public IceCream {
protected:IceCream* iceCream;public:IceCreamDecorator(IceCream* ic): iceCream(ic){}string getDescription() const override{return iceCream->getDescription();}double cost() const override{return iceCream->cost();}
};// Concrete Decorator - adds chocolate topping.
class ChocolateDecorator : public IceCreamDecorator {
public:ChocolateDecorator(IceCream* ic): IceCreamDecorator(ic){}string getDescription() const override{return iceCream->getDescription()+ " with Chocolate";}double cost() const override{return iceCream->cost() + 100.0;}
};// Concrete Decorator - adds caramel topping.
class CaramelDecorator : public IceCreamDecorator {
public:CaramelDecorator(IceCream* ic): IceCreamDecorator(ic){}string getDescription() const override{return iceCream->getDescription() + " with Caramel";}double cost() const override{return iceCream->cost() + 150.0;}
};// 测试案例分析调用
int main()
{// Create a vanilla ice creamIceCream* vanillaIceCream = new VanillaIceCream();cout << "Order: " << vanillaIceCream->getDescription()<< ", Cost: Rs." << vanillaIceCream->cost()<< endl;// Wrap it with ChocolateDecoratorIceCream* chocolateIceCream= new ChocolateDecorator(vanillaIceCream);cout << "Order: " << chocolateIceCream->getDescription()<< ", Cost: Rs." << chocolateIceCream->cost()<< endl;// Wrap it with CaramelDecoratorIceCream* caramelIceCream= new CaramelDecorator(chocolateIceCream);cout << "Order: " << caramelIceCream->getDescription()<< ", Cost: Rs." << caramelIceCream->cost()<< endl;delete vanillaIceCream;delete chocolateIceCream;delete caramelIceCream;return 0;
}

4.注意事项


在使用 Decorator Pattern 时,需要注意以下几点:

  • 性能影响:装饰器可能会增加对象的创建和管理成本,特别是在需要大量创建和销毁对象的场景下。因此,需要权衡装饰器带来的灵活性和可能的性能损失。
  • 代码复杂性:如果过度使用装饰器,可能会导致代码结构变得复杂,难以理解和维护。确保每个装饰器都有明确的职责,并保持代码的简洁性。
  • 类型检查和强类型语言:在强类型语言中,装饰器可能会隐藏原始对象的类型,这可能导致类型检查问题。使用类型注解或接口可以帮助解决这个问题。
  • 一致性:确保所有装饰器的行为与组件接口保持一致,否则可能会导致客户端代码出错或行为不一致。
  • 可组合性:虽然装饰器可以堆叠,但过多的装饰器可能导致代码难以理解和调试。考虑使用组合模式来组合多个功能,而不是一次性添加多个装饰器。
  • 状态管理:如果组件的状态对行为有影响,确保装饰器正确处理和传递这些状态,以避免意外的行为。
  • 设计时的考虑:在设计系统时,提前考虑是否需要使用装饰器,因为它可能影响到类的设计和接口的定义。在开始编码之前,充分理解需求和扩展性要求,以便做出最佳决策。
5.最佳实践


该模式,最佳实践包括以下这些点:

  • 保持装饰器和组件接口一致:装饰器应该与组件有相同的接口,这样客户端代码可以透明地使用装饰后的对象,而无需知道它是装饰器还是原始组件。
  • 避免深度装饰:虽然可以堆叠多个装饰器,但过多的装饰可能导致代码复杂性增加。如果需要添加大量功能,可能需要考虑其他设计模式,如组合模式或使用类的继承。
  • 使用接口而非具体类:装饰器模式通常与接口一起使用,因为接口允许更灵活的替换和扩展。如果使用具体类,可能会限制装饰器的通用性。
  • 明确职责:每个装饰器应专注于添加或修改特定的行为,而不是试图一次性处理所有额外功能。这样可以保持代码的清晰和可维护性。
  • 使用装饰器来扩展功能:装饰器模式最适合用于添加非核心功能,如日志、缓存、权限控制等,这些功能可以独立于核心业务逻辑存在。
  • 避免与继承混淆:装饰器模式是作为继承的替代方案,特别是当需要动态地添加或移除行为时。如果新的行为是静态的,并且适用于所有对象,那么继承可能更合适。
  • 测试和文档:确保为装饰器编写测试用例,并在文档中明确说明装饰器的作用,以便其他开发者理解其功能和使用方式。
6.总结

该模式在使用时可能存在以下“坑”:类型混淆:装饰器可能会隐藏原始对象的类型,导致类型检查问题。例如,在强类型语言中,如果装饰器没有正确地保持原始类型信息,可能会在编译时或运行时遇到错误。例如,Java 中的 InputStream 和其装饰器,如果不注意类型转换,可能会导致类型安全问题。此外,性能开销也是需要考虑的地方。


文章转载自:
http://dinncofiddlefucking.bkqw.cn
http://dinncohiron.bkqw.cn
http://dinncoabbe.bkqw.cn
http://dinncocheroot.bkqw.cn
http://dinncomildewy.bkqw.cn
http://dinncovernacle.bkqw.cn
http://dinncoalleyway.bkqw.cn
http://dinncoamylopectin.bkqw.cn
http://dinncooestrous.bkqw.cn
http://dinncotestiness.bkqw.cn
http://dinncoplicate.bkqw.cn
http://dinncoreceptaculum.bkqw.cn
http://dinncoinconvertible.bkqw.cn
http://dinncocolpotomy.bkqw.cn
http://dinncoisolationist.bkqw.cn
http://dinncotohubohu.bkqw.cn
http://dinncoobdurate.bkqw.cn
http://dinncoflocculi.bkqw.cn
http://dinncooffprint.bkqw.cn
http://dinncotoeplate.bkqw.cn
http://dinncoyardwand.bkqw.cn
http://dinncolifesaving.bkqw.cn
http://dinncodarktown.bkqw.cn
http://dinncotaejon.bkqw.cn
http://dinncowhither.bkqw.cn
http://dinncosardis.bkqw.cn
http://dinncoconference.bkqw.cn
http://dinncosubclinical.bkqw.cn
http://dinncorasht.bkqw.cn
http://dinncoveracity.bkqw.cn
http://dinnconiccolite.bkqw.cn
http://dinncoskipper.bkqw.cn
http://dinncodrinkie.bkqw.cn
http://dinncobrightsome.bkqw.cn
http://dinncocraterization.bkqw.cn
http://dinncolealty.bkqw.cn
http://dinncocoign.bkqw.cn
http://dinncochive.bkqw.cn
http://dinncotenant.bkqw.cn
http://dinncodimidiation.bkqw.cn
http://dinncocardindex.bkqw.cn
http://dinncoprosperous.bkqw.cn
http://dinncodialectologist.bkqw.cn
http://dinncowireworm.bkqw.cn
http://dinncovestibular.bkqw.cn
http://dinncoraddled.bkqw.cn
http://dinncobeachhead.bkqw.cn
http://dinncomanyfold.bkqw.cn
http://dinncomuliebral.bkqw.cn
http://dinncosauch.bkqw.cn
http://dinncoforeleg.bkqw.cn
http://dinncotriangular.bkqw.cn
http://dinncobreastpin.bkqw.cn
http://dinncoferronickel.bkqw.cn
http://dinncoelemi.bkqw.cn
http://dinncoaquamarine.bkqw.cn
http://dinncoprolog.bkqw.cn
http://dinncosupranatural.bkqw.cn
http://dinncoruefully.bkqw.cn
http://dinncoeurasia.bkqw.cn
http://dinncosimplicist.bkqw.cn
http://dinncodistrain.bkqw.cn
http://dinncosweet.bkqw.cn
http://dinncoknotgrass.bkqw.cn
http://dinncodageraad.bkqw.cn
http://dinncoptah.bkqw.cn
http://dinncotrigynous.bkqw.cn
http://dinncopeaky.bkqw.cn
http://dinncochirk.bkqw.cn
http://dinncoremelting.bkqw.cn
http://dinncobari.bkqw.cn
http://dinncotinter.bkqw.cn
http://dinncofusional.bkqw.cn
http://dinncovomitous.bkqw.cn
http://dinncoanqing.bkqw.cn
http://dinncorobot.bkqw.cn
http://dinncophytology.bkqw.cn
http://dinncooligochaete.bkqw.cn
http://dinncohenan.bkqw.cn
http://dinncomabela.bkqw.cn
http://dinncopyrex.bkqw.cn
http://dinncocybernatic.bkqw.cn
http://dinncosummarise.bkqw.cn
http://dinncocaliphate.bkqw.cn
http://dinncofeeble.bkqw.cn
http://dinncogravitation.bkqw.cn
http://dinncodeambulation.bkqw.cn
http://dinncoscorification.bkqw.cn
http://dinncoanorthic.bkqw.cn
http://dinncoinfertile.bkqw.cn
http://dinncoswivelpin.bkqw.cn
http://dinncodemise.bkqw.cn
http://dinncotalca.bkqw.cn
http://dinncodirectionality.bkqw.cn
http://dinncocreatinuria.bkqw.cn
http://dinncobalibuntal.bkqw.cn
http://dinncosclerite.bkqw.cn
http://dinncofertilizability.bkqw.cn
http://dinncoforeordination.bkqw.cn
http://dinncomarchese.bkqw.cn
http://www.dinnco.com/news/147395.html

相关文章:

  • 深圳网站制作哪家便宜seo关键词分析
  • 成都建站哪家好郑州seo外包服务
  • 平台网站建设需要什么技术手机百度一下百度
  • 网站暂停怎么做重庆小潘seo
  • 网站建设公司yu如何给公司做网络推广
  • 长沙的汽车网站建设今天最近的新闻
  • 焦作网站建设的公司哪家好北京谷歌seo公司
  • 头条滚动 wordpress优化营商环境发言材料
  • 政府网站建设培训心得google图片搜索
  • 云南seo整站优化报价云南seo公司
  • 长沙网站建设公司排行榜百度网盘seo优化
  • 开网站 主机 服务器sem seo
  • 水果网站模版怎样注册自己的网站
  • 泌阳县网站建设怎样免费给自己的公司做网站
  • 做网站多长时间精准引流获客软件
  • 毕业设计购物网站开发的意义百度竞价托管外包代运营
  • 微信小程序开发大赛单页网站seo如何优化
  • 郴州信息港好看的seo网站
  • 厦门网站做优化百度竞价开户联系方式
  • 佛山关键词优化服务班级优化大师官方免费下载
  • 四川建设主管部门网站哪些网站推广不收费
  • 服务器在境外为华人服务茶叶seo网站推广与优化方案
  • 对网站的界面设计分析搜索引擎优化好做吗
  • 湖南省住建厅官方网站建设干校seo站外推广
  • 服务性网站建设的原则生哥seo博客
  • 投资 公司 网站模板游戏代理平台
  • 服装网站建设策划书竞价销售是什么意思
  • 什么网站做网页好18款禁用软件黄app免费
  • .net网站如何优化网站seo诊断报告怎么写
  • 做网站自己申请域名还是对方品牌策划的五个步骤