首页 > 其他分享 >03: 装饰模式

03: 装饰模式

时间:2024-09-16 12:13:35浏览次数:1  
标签:std 03 void 模式 public shared Operation 装饰 Decorator

1. 案例:手机->贴膜->戴保护套->贴卡通贴纸->系手机挂绳

2. 组合模式和装饰模式

组合模式和装饰模式都可以用来透明的把对象包装在具有同样接口的另一个对象中。
组合模式和装饰模式都是结构型模式。组合模式着眼于把众多子对象组织为一个整体;装饰模式着眼于在不修改现有对象或从其派生子类的前提下为其增添职责,其子对象只有一个。

3. 装饰模式结构

- Component(抽象构件):是具有构件类和装饰类的共同基类,声明了在具体构件中定义的方法,客户端可以一致的对待使用装饰前后的对象

class Component
{
public:
    virtual void Operation() = 0;
};

- Decorator(抽象装饰类):用于给具体构件增加职责,但具体职责在其子类中实现。抽象装饰类通过聚合关系定义一个抽象构件的对象,通过该对象可以调用装饰之前构件的方法,并用过其子类扩展该方法,达到装饰的目的

class Decorator:
    public Component
{
public:
    void DecoratorComponent(std::shared_ptr<Component> spComponent);
    // 通过 Component 继承
    void Operation() override;
private:
    std::shared_ptr<Component> m_spComponent;
};

void Decorator::DecoratorComponent(std::shared_ptr<Component> spComponent)
{
    m_spComponent = spComponent;
}

void Decorator::Operation()
{
    m_spComponent->Operation();
}

 - ConcreteComponent(具体构件):具体构件定义了构件具体的方法,装饰类可以给它增加更多的功能

class Phone :
    public Component
{
public:
    // 通过 Decorator 继承
    void Operation() override;
};

void Phone::Operation()
{
    std::cout << "Phone" << std::endl;
}

 - ConcreteDecorator(具体装饰类):向构件增加新的功能

class DecoratorFilm :
    public Decorator
{
public:
    // 通过 Decorator 继承
    void Operation() override;
};

void DecoratorFilm::Operation()
{
    Decorator::Operation();
    std::cout << "贴膜" << std::endl;
}
class DecoratorRope :
    public Decorator
{
public:
    // 通过 Decorator 继承
    void Operation() override;
};

void DecoratorRope::Operation()
{
    Decorator::Operation();
    std::cout << "戴保护壳" << std::endl;
}
class DecoratorShell :
    public Decorator
{
public:
    // 通过 Decorator 继承
    void Operation() override;
};

void DecoratorShell::Operation()
{
    Decorator::Operation();
    std::cout << "贴卡通贴纸" << std::endl;
}
class DecoratorSticker :
    public Decorator
{
public:
    // 通过 Decorator 继承
    void Operation() override;
};

void DecoratorSticker::Operation()
{
    Decorator::Operation();
    std::cout << "系手机挂绳" << std::endl;
}

4. 用法

    std::shared_ptr<Phone> spPhone = std::make_shared<Phone>();
    std::shared_ptr<DecoratorFilm> spDecoratorFilm = std::make_shared<DecoratorFilm>();
    std::shared_ptr<DecoratorShell> spDecoratorShell = std::make_shared<DecoratorShell>();
    std::shared_ptr<DecoratorSticker> spDecoratorSticker = std::make_shared<DecoratorSticker>();
    std::shared_ptr<DecoratorRope> spDecoratorRope = std::make_shared< DecoratorRope>();

    spDecoratorFilm->DecoratorComponent(spPhone);
    spDecoratorShell->DecoratorComponent(spDecoratorFilm);
    spDecoratorSticker->DecoratorComponent(spDecoratorShell);
    spDecoratorRope->DecoratorComponent(spDecoratorSticker);

    spDecoratorRope->Operation();

标签:std,03,void,模式,public,shared,Operation,装饰,Decorator
From: https://www.cnblogs.com/BoYuCG/p/18416166

相关文章

  • 开发指南032-调整日志级别
    1)显示SQL语句及其参数nacos里配置spring:  jpa:   show-sql:truelogback-spring.xml里配置<loggername="org.hibernate.type.descriptor.sql.BasicBinder"level="TRACE"/>2)feign显示调用过程logging:  level:   org.qlm.feign:WARN==========持续更新中====......
  • INFO20003 SQL Requesting Communications
    INFO20003S22024–ASSIGNMENT2v1.41INFO20003Semester2,2024Assignment2:SQLDue:Week8-Sunday15thSeptember2024,5:59pmMelbourneTime.Submission-ViaLMShttps://canvas.lms.unimelb.edu.au/Case:“Slarc”App“Slarc”:SuperLovelyAppfor......
  • 02策略模式
    1.案例:营业员根据客户所购买商品的单价和数量,根据不同活动向客户收费**-正常原价收费-八折收费-满300返1002.策略模式结构-抽象策略类(Stategy):声明算法的方法,抽象层的设计使上下文类可以无差别的调用不同的具体策略的方法enumCashType{NORMAL=0,RE......
  • ORA-600 16703故障再现---惜分飞
    联系:手机/微信(+8617813235971)QQ(107644445)标题:ORA-60016703故障再现作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]从第一次发现ORA-60016703(警告:互联网中有oracle介质被注入恶意程序导致—ORA-60016703)至今已经7年多时间......
  • 简单工厂模式
    1.案例:营业员根据客户所购买商品的单价和数量,根据不同活动向客户收费-正常原价收费-八折收费-满300返1002.简单工厂模式结构-抽象产品(AbstractProduct):具体产品类的基类,包含创建产品的公共方法enumCashType{NORMAL=0,REBATE,RETURN};class......
  • CF1039D You Are Given a Tree
    CF1039DYouAreGivenaTree咋其他题解都带根号?根号是坏文明,这里是俩\(\log\)做法,能够跑到\(300\)ms以内。首先考虑暴力贪心,从叶子向根合并,可以取出一条链的时候就取出来,否则就连一条尽可能长的链往上合并。具体的设\(f_{x,i}\)为当\(k=i\)时,在\(x\)处能取出的最......
  • [Whole Web] Auto check application website's updates
    Inaproductionenvironment,wewanttoprompttheuserwithamessagewhennewscriptsareavailable,askingNewscriptsareavailable.Doyouwanttoupdate?Theideaisstraightforward:periodically(e.g.,everyminute,10seconds,dependsonyourcase......
  • 总结:1037 - CSP 2021 提高级第一轮
    我的提交记录与结果以比较为基本运算,对于\(2n\)个数,同时找到最大值和最小值,最坏情况下需要的最小的比较次数为()。\(\textttA\).4n-2\(\textttB\).3n+1\(\color{#5eb95e}\texttt{C}\).3n-2\(\color{#e74c3c}\textttD\).2n+1【解析】:首先先将原数组两两分组。每组......
  • Android中的单例模式
    在Android开发中,单例模式(SingletonPattern)是一种常用的设计模式,它确保一个类只有一个实例,并提供一个全局访问点来获取这个实例。单例模式在需要控制资源访问、管理共享资源或配置信息的场景下特别有用。在Android中,实现单例模式的方法有多种,但主要思想是一致的:私有化构造函数,......
  • 探索鸿蒙应用开发中的沉浸式模式与安全区域文字颜色
    在鸿蒙应用开发中,实现沉浸式显示模式与改变安全区域文字颜色可以为用户带来更加流畅和美观的视觉体验,本文将详细介绍如何在鸿蒙应用中实现特定页面的沉浸式显示,以及在这个过程中遇到的问题和解决方案。效果展示开启沉浸式模式关闭沉浸式模式改变安全区域文字颜色一、......