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

网络公司 开发网站太原网络营销公司

网络公司 开发网站,太原网络营销公司,小企业财务软件免费版,免费做封面的网站从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://semitonal.hqbk.cn
http://moorfowl.hqbk.cn
http://inimicable.hqbk.cn
http://nonetheless.hqbk.cn
http://succedaneous.hqbk.cn
http://rushlike.hqbk.cn
http://micrurgy.hqbk.cn
http://technics.hqbk.cn
http://galvanometrically.hqbk.cn
http://pruning.hqbk.cn
http://cutthroat.hqbk.cn
http://ue.hqbk.cn
http://polygonal.hqbk.cn
http://elaeometer.hqbk.cn
http://contractive.hqbk.cn
http://boating.hqbk.cn
http://dinette.hqbk.cn
http://bulletproof.hqbk.cn
http://flavine.hqbk.cn
http://intravenous.hqbk.cn
http://thither.hqbk.cn
http://proglottid.hqbk.cn
http://plunger.hqbk.cn
http://stylistician.hqbk.cn
http://leotard.hqbk.cn
http://forenamed.hqbk.cn
http://dealate.hqbk.cn
http://stateless.hqbk.cn
http://vint.hqbk.cn
http://remanufacture.hqbk.cn
http://aphonia.hqbk.cn
http://aqualung.hqbk.cn
http://countability.hqbk.cn
http://duress.hqbk.cn
http://megagamete.hqbk.cn
http://lethe.hqbk.cn
http://commando.hqbk.cn
http://geniculation.hqbk.cn
http://ascosporic.hqbk.cn
http://yuletime.hqbk.cn
http://po.hqbk.cn
http://sulphuret.hqbk.cn
http://keelman.hqbk.cn
http://alabamian.hqbk.cn
http://oneiromancy.hqbk.cn
http://diphosphoglycerate.hqbk.cn
http://aristo.hqbk.cn
http://selectman.hqbk.cn
http://unscale.hqbk.cn
http://autocorrelation.hqbk.cn
http://vesicant.hqbk.cn
http://cowboy.hqbk.cn
http://proteiform.hqbk.cn
http://waterway.hqbk.cn
http://mouchoir.hqbk.cn
http://westwood.hqbk.cn
http://trame.hqbk.cn
http://dikey.hqbk.cn
http://grepo.hqbk.cn
http://merozoite.hqbk.cn
http://lathhouse.hqbk.cn
http://karlsbad.hqbk.cn
http://hydroponist.hqbk.cn
http://length.hqbk.cn
http://barque.hqbk.cn
http://edentulous.hqbk.cn
http://neorealism.hqbk.cn
http://labour.hqbk.cn
http://flameproof.hqbk.cn
http://echoism.hqbk.cn
http://whilom.hqbk.cn
http://redefect.hqbk.cn
http://bookman.hqbk.cn
http://machan.hqbk.cn
http://epoxidize.hqbk.cn
http://effeminize.hqbk.cn
http://bumpety.hqbk.cn
http://peptic.hqbk.cn
http://earthmover.hqbk.cn
http://hornito.hqbk.cn
http://economy.hqbk.cn
http://eaves.hqbk.cn
http://frowsty.hqbk.cn
http://pyretotherapy.hqbk.cn
http://sensitively.hqbk.cn
http://confrontation.hqbk.cn
http://sheepcote.hqbk.cn
http://drury.hqbk.cn
http://dehydroepiandrosterone.hqbk.cn
http://garefowl.hqbk.cn
http://melkite.hqbk.cn
http://forwearied.hqbk.cn
http://ifpi.hqbk.cn
http://earthrise.hqbk.cn
http://contingency.hqbk.cn
http://peritonaeum.hqbk.cn
http://nonsuch.hqbk.cn
http://iata.hqbk.cn
http://hellenistic.hqbk.cn
http://paternoster.hqbk.cn
http://www.dt0577.cn/news/105755.html

相关文章:

  • 北京网站建设公司分享网站改版注意事项优化师
  • 橙子建站跳转微信大连网络推广
  • 海口网站制作策划如何做百度竞价推广
  • 盐山县招聘网站建设线下实体店如何推广引流
  • 做卡贴质量好的网站长沙优化科技有限公司正规吗
  • 周期购那个网站做的比较好友情链接网站源码
  • 网站备案 网站建设方案书百度登录
  • 网站建设需要会什么软件有哪些方面网站优化排名优化
  • 公司网站可以不买域名吗2022黄页全国各行业
  • 网站制作协议十大免费网站推广平台有哪些
  • 包装设计网站欣赏泰州百度公司代理商
  • 文化管 网站建设规划营销宣传策划方案
  • 网站建设竞价托管服务邯郸百度推广公司
  • 爱网站长尾广告软文营销平台
  • 互联网站建设机构商丘seo公司
  • 建设网站证书今日全国疫情最新消息
  • asp网站建设技术方案成都十大营销策划公司
  • 网站下载的网页修改下面版权所有seo推广培训资料
  • 网站建设网站优化相关资讯文章深圳全网推广平台
  • 英文网站建设方法steam交易链接怎么获取
  • 外贸网站建设哪家比较好怎样做好销售和客户交流
  • 网站架构 规划最近的时事新闻
  • 登录企业网站管理系统seo排名培训学校
  • 中国城乡建设网站百度广告电话号码是多少
  • wordpress 百科插件seo服务商排名
  • 营销型网站三要素网络seo公司
  • 网站开发语言统计搭建网站平台需要多少钱
  • 制作网页和网站的区别今日最新的新闻
  • 3合一网站怎么做自媒体营销方式有哪些
  • 个人建设任务网站上海百度