设计模式之装饰模式

news/2024/10/25 17:18:59/

一、概念

装饰模式是一种结构型设计模式,允许你通过将对象放入包含行为的特殊封装对象中来为原对象绑定新的行为。

二、构造

部件(Component)声明封装器和被封装对象的公用接口。

具体部件(Concrete Component)类是被封装对象所属的类。它定义了基础行为,但装饰类可以改变这些行为。

基础装饰(Base Decorator)类拥有一个指向被封装对象的引用成员变量。该变量的类型应当被声明为通用部件接口,这样它就可以引用具体的部件和装饰。装饰基类会将所有操作委派给被封装的对象。

具体装饰类(Concrete Decorators)定义了可动态添加到部件的额外行为。具体装饰类会重写装饰基类的方法,并在调用父类方法之前或之后进行额外的行为。

客户端(Client)可以使用多层装饰来封装部件,只要它能使用通用接口与所有对象互动即可。

三、应用场景

class Component {
public:virtual ~Component() {}virtual std::string Operation() const = 0;
};class ConcreteComponent : public Component {
public:std::string Operation() const override {return "ConcreteComponent";}
};class Decorator : public Component {
protected:Component* component_;public:Decorator(Component* component) : component_(component) {}std::string Operation() const override {return this->component_->Operation();}
};class ConcreteDecoratorA : public Decorator {
public:ConcreteDecoratorA(Component* component) : Decorator(component) {}std::string Operation() const override {return "ConcreteDecoratorA(" + Decorator::Operation() + ")";}
};class ConcreteDecoratorB : public Decorator {
public:ConcreteDecoratorB(Component* component) : Decorator(component) {}std::string Operation() const override {return "ConcreteDecoratorB(" + Decorator::Operation() + ")";}
};int main() {Component* simple = new ConcreteComponent;std::cout << "Client: I've got a simple component:\n";std::cout << "RESULT: " << simple->Operation();std::cout << "\n\n";Component* decorator1 = new ConcreteDecoratorA(simple);Component* decorator2 = new ConcreteDecoratorB(decorator1);std::cout << "Client: Now I've got a decorated component:\n";std::cout << "RESULT: " << decorator2->Operation();std::cout << "\n";delete simple;delete decorator1;delete decorator2;return 0;
}

装饰模式总结

优点:

  • 无需创建新子类即可扩展对象的行为。
  • 可以在运行时添加或删除对象的功能。
  • 可以用多个装饰封装对象来组合几种行为。
  • 单一职责原则。可以将实现了许多不同行为的一个大类拆分为多个较小的类。

缺点:

  • 在封装器栈中删除特定封装器比较困难。
  • 实现行为不受装饰栈顺序影响的装饰比较困难。
  • 各层的初始化配置代码看上去可能会很糟糕。

http://www.ppmy.cn/news/1200000.html

相关文章

全光谱大面积氙光灯太阳光模拟器老化测试

氙灯光源太阳光模拟器广泛应用于光解水产氢、光化学催化、二氧化碳制甲醇、光化学合成、光降解污染物、 水污染处理、生物光照,光学检测、太阳能电池研究、荧光材料测试(透射、反射、吸收) 太阳能电池特性测试&#xff0c;光热转化&#xff0c;光电材料特性测试&#xff0c;生物…

快速跳转插件

只要是著名的编辑软件&#xff08;很多人使用的&#xff09;一般就会有相应的跳转插件&#xff0c;用于快速在页面中进行快速的跳转&#xff0c;在Github的这个链接中列出了一些著名软件的跳转插件&#xff1a;https://github.com/acejump/AceJump#comparison

vue如何实现视频全屏切换

最近项目开发中遇到一个视频窗口全屏切换功能&#xff0c;为此在这里做个记录。 具体的实现思路&#xff1a; <template><div class"content-box"><div class"container"><div id"screen" class"screen"><…

Elasticsearch实战:常见错误及详细解决方案

Elasticsearch实战&#xff1a;常见错误及详细解决方案 1.read_only_allow_delete":“true” 当我们在向某个索引添加一条数据的时候&#xff0c;可能&#xff08;极少情况&#xff09;会碰到下面的报错&#xff1a; {"error": {"root_cause": [{&…

R语言环境下使用curl库做的爬虫代码示例

curl库是一个用于传输数据的工具和库&#xff0c;它支持多种协议&#xff0c;包括HTTP、FTP、SMTP等。在爬虫中&#xff0c;curl库可以用来获取网页内容&#xff0c;从而实现爬取网页的功能。通过设置curl的选项&#xff0c;可以实现对网页的请求、响应、重定向等操作。在使用c…

陶瓷生产工艺数字孪生可视化管理平台,推动陶瓷工厂数字化转型新实践

聚焦国家战略需求和先进制造业发展方向&#xff0c;加快数字化发展战略部署&#xff0c;全面推进制造业数字化转型&#xff0c;推进智能制造典型场景和示范工厂的标杆引领&#xff0c;助力陶瓷工业的数字化、网络化、智能化并行推进、融合发展。数字孪生、物联网、云计算、人工…

关于Andorid Studio中build.gradle和setting.gradle配置变更

Build was configured to prefer settings repositories over project repositories but repository Google was added by build file xx/build.gradle 报错的原因是因为&#xff0c;在Android Gradle 7.x 版本中 Android Studio倾向于将下属依赖添加到setting.gradle中&#x…

RK3566上运行yolov5模型进行图像识别

一、简介 本文记录了依靠RK官网的文档&#xff0c;一步步搭建环境到最终在rk3566上把yolov5 模型跑起来。最终实现的效果如下&#xff1a; 在rk3566 板端运行如下app&#xff1a; ./rknn_yolov5_demo model/RK356X/yolov5s-640-640.rknn model/bus.jpg其中yolov5s-640-640.r…