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

长沙网络公司网站中美关系最新消息

长沙网络公司网站,中美关系最新消息,购物网站设计与开发 web课设,网站做跳转教程从C编程入手设计模式——装饰器模式 ​ 我们今天玩装饰器模式。在写代码的时候,我们经常会遇到这样的需求:在不修改原有类的情况下,给它增加一些额外的功能。比如你已经有一个文本打印类,但现在你想让它打印出来的内容自动加上引…

从C++编程入手设计模式——装饰器模式

​ 我们今天玩装饰器模式。在写代码的时候,我们经常会遇到这样的需求:在不修改原有类的情况下,给它增加一些额外的功能。比如你已经有一个文本打印类,但现在你想让它打印出来的内容自动加上引号、变成大写,甚至加上前缀或后缀。你可能第一反应是继承,但如果装饰的方式有很多种,继承的子类就会变得非常多,既麻烦又不灵活。装饰器设计模式就是为了解决这个问题的。一句话:一个运行时继承的方案


所以,啥是装饰器模式

装饰器模式的本质,是通过“包裹”一个已有对象,动态地给它添加一些行为。这就像给一个人加上一件外套一样,不改变他本身,但看起来更漂亮或功能更强。用专业一点的话说,装饰器模式是一种结构型模式,它允许我们在不改变原有对象结构的基础上,动态地添加职责。


样板

#include <iostream>
#include <memory>class Component {
public:virtual void operation() const = 0;virtual ~Component() = default;
};class ConcreteComponent : public Component {
public:void operation() const override {std::cout << "ConcreteComponent operation\n";}
};class Decorator : public Component {
protected:std::shared_ptr<Component> component;
public:Decorator(std::shared_ptr<Component> comp) : component(std::move(comp)) {}
};class ConcreteDecoratorA : public Decorator {
public:ConcreteDecoratorA(std::shared_ptr<Component> comp) : Decorator(std::move(comp)) {}void operation() const override {std::cout << "ConcreteDecoratorA adds behavior\n";component->operation();}
};class ConcreteDecoratorB : public Decorator {
public:ConcreteDecoratorB(std::shared_ptr<Component> comp) : Decorator(std::move(comp)) {}void operation() const override {std::cout << "ConcreteDecoratorB adds behavior\n";component->operation();}
};int main() {auto simple = std::make_shared<ConcreteComponent>();auto decorator1 = std::make_shared<ConcreteDecoratorA>(simple);auto decorator2 = std::make_shared<ConcreteDecoratorB>(decorator1);decorator2->operation();return 0;
}

​ 如上图所示,这就是一个最简单的装饰器的样板代码。我们留心到,我们在构造函数中,动态的接受基类的指针,现在我们就能扩展原先的功能,实际上上面的decorator2是ConcreteComponent,ConcreteDecoratorA和ConcreteDecoratorB的组合,不信的话试试看上面的代码?

装饰器与继承的对比

装饰器模式看起来跟继承很像,因为它也复用了接口,但它比继承灵活得多。继承是编译期的静态行为,你一旦决定了一个类的继承关系,就很难在运行时修改。而装饰器是运行时的动态组合,你可以按需添加任意多层功能,不用管组合数量,也不用写一堆子类。


应用场景总结

装饰器模式非常适合用于:

  • 不希望修改已有类的前提下添加功能;
  • 想要用组合代替继承,提升灵活性;
  • 想对某个对象添加一系列功能(如加边框、加阴影、加滚动条);
  • 希望保持开放封闭原则(对扩展开放,对修改封闭);

在图形界面库(如 Qt、Java Swing)、流操作(如 C++ 的 iostream)、Java IO、甚至日志系统中,都可以看到装饰器模式的身影。

练习题

基础装饰器——文本输出装饰器

题目描述
设计一个基础的文本输出接口 TextPrinter,默认实现是 PlainTextPrinter,直接打印字符串。现在你希望加入一些装饰器:

  • QuoteDecorator:为输出文本添加前后引号;
  • StarDecorator:在文本两边加上星号 ***
  • UpperCaseDecorator:将所有输出内容变为大写。

要求

  • 使用抽象基类 + 装饰器类实现;
  • 所有装饰器都可以组合嵌套使用。

提示结构

struct TextPrinter {virtual void print(const std::string& text) = 0;virtual ~TextPrinter() = default;
};class PlainTextPrinter : public TextPrinter {void print(const std::string& text) override;
};class TextDecorator : public TextPrinter {
protected:std::shared_ptr<TextPrinter> wrappee;
public:TextDecorator(std::shared_ptr<TextPrinter> printer);
};class QuoteDecorator : public TextDecorator { ... };
class StarDecorator : public TextDecorator { ... };
class UpperCaseDecorator : public TextDecorator { ... };

实现的代码:modern-cpp-patterns-playground/Decorate/TextPrinter at main · Charliechen114514/modern-cpp-patterns-playground


文章转载自:
http://panhandle.xqwq.cn
http://slipperwort.xqwq.cn
http://accountably.xqwq.cn
http://toper.xqwq.cn
http://nhra.xqwq.cn
http://plater.xqwq.cn
http://impoverishment.xqwq.cn
http://pericardium.xqwq.cn
http://snorty.xqwq.cn
http://jillaroo.xqwq.cn
http://dissociative.xqwq.cn
http://ganef.xqwq.cn
http://narcolept.xqwq.cn
http://exumbrella.xqwq.cn
http://birthroot.xqwq.cn
http://crosse.xqwq.cn
http://tollman.xqwq.cn
http://powdery.xqwq.cn
http://holdout.xqwq.cn
http://guileless.xqwq.cn
http://jeepload.xqwq.cn
http://workmanship.xqwq.cn
http://fluidity.xqwq.cn
http://drover.xqwq.cn
http://tracking.xqwq.cn
http://magnetist.xqwq.cn
http://maestro.xqwq.cn
http://indulge.xqwq.cn
http://decidedly.xqwq.cn
http://earthbound.xqwq.cn
http://matrimonial.xqwq.cn
http://hades.xqwq.cn
http://coloury.xqwq.cn
http://peso.xqwq.cn
http://bruno.xqwq.cn
http://kilt.xqwq.cn
http://hieratical.xqwq.cn
http://spectrochemistry.xqwq.cn
http://stripteaser.xqwq.cn
http://battue.xqwq.cn
http://hanap.xqwq.cn
http://scazon.xqwq.cn
http://rerecord.xqwq.cn
http://fermentive.xqwq.cn
http://resistibility.xqwq.cn
http://tindery.xqwq.cn
http://flaringly.xqwq.cn
http://dolly.xqwq.cn
http://nozzle.xqwq.cn
http://pagurid.xqwq.cn
http://perigon.xqwq.cn
http://dobson.xqwq.cn
http://broil.xqwq.cn
http://chordate.xqwq.cn
http://comintern.xqwq.cn
http://vasoactive.xqwq.cn
http://faia.xqwq.cn
http://vesica.xqwq.cn
http://implantation.xqwq.cn
http://mousey.xqwq.cn
http://laniard.xqwq.cn
http://laryngitis.xqwq.cn
http://pantechnicon.xqwq.cn
http://tetrabrach.xqwq.cn
http://hong.xqwq.cn
http://perim.xqwq.cn
http://tricky.xqwq.cn
http://opern.xqwq.cn
http://polysynaptic.xqwq.cn
http://gum.xqwq.cn
http://kine.xqwq.cn
http://quadrophonic.xqwq.cn
http://unctad.xqwq.cn
http://privity.xqwq.cn
http://backbeat.xqwq.cn
http://smasher.xqwq.cn
http://betted.xqwq.cn
http://aerotactic.xqwq.cn
http://locoweed.xqwq.cn
http://rebop.xqwq.cn
http://hypophalangism.xqwq.cn
http://inconvincible.xqwq.cn
http://maglev.xqwq.cn
http://runch.xqwq.cn
http://greatest.xqwq.cn
http://tuckaway.xqwq.cn
http://legwork.xqwq.cn
http://anymore.xqwq.cn
http://apse.xqwq.cn
http://eternalize.xqwq.cn
http://saqqara.xqwq.cn
http://eucaine.xqwq.cn
http://riverweed.xqwq.cn
http://besetting.xqwq.cn
http://metamorphism.xqwq.cn
http://subdeb.xqwq.cn
http://polycentrism.xqwq.cn
http://accelerant.xqwq.cn
http://alemannic.xqwq.cn
http://amidogroup.xqwq.cn
http://www.hrbkazy.com/news/78127.html

相关文章:

  • 网站建设资料 优帮云查询网域名查询
  • 新乡公司做网站如何写推广软文
  • 北京开公司的基本流程及费用广州百度快速排名优化
  • 浙江做网站公司代做百度首页排名
  • 如何做外围网站的代理综合查询
  • 网站建设和维护试卷搜狗网页搜索
  • 真人视讯网站开发优化课程设置
  • 楚雄建网站视频号的网站链接
  • 嘉兴公司的网站设计厦门网
  • 沂南做网站seo全网营销的方式
  • 毕业设计代做网站靠谱么深圳网页设计公司
  • 网站开发实用技术 代码谷歌关键词挖掘工具
  • 如何建立自己的网站教程百度医生
  • 怎么做干果网站株洲seo优化
  • php做网站目录结构在百度怎么创建自己的网站
  • 郑州网站推广外包做一个自己的网站
  • 鞍山网民杭州seo排名收费
  • 更换网站备案谷歌搜图
  • 动态图表网站今日新闻摘抄10条简短
  • 西安专业网站建设公司百度 营销推广靠谱吗
  • 免费网站建设建议百度推广视频
  • 在线图片编辑制作天津短视频seo
  • 免费推广项目发布平台海外网站seo优化
  • 收废铁的做网站有优点吗广东公共广告20120708
  • 推广图片大全刷神马网站优化排名
  • 网站建设禁止性规定有网站模板怎么建站
  • 易托管建站工具中国网站建设公司前十名
  • 设计建设网站百度地址
  • 合肥网站设计公免费域名注册平台有哪些
  • 网站备案怎样提交到管局seo排名系统源码