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

【设计模式】装饰器与代理模式的对比

文章目录

      • 装饰器模式(Decorator Pattern)
      • 代理模式(Proxy Pattern)
      • 两者之间的区别

装饰器模式(Decorator Pattern)

装饰器模式是一种结构型设计模式,它允许你动态地将责任附加到对象上,而不会影响其他对象。装饰器模式通过创建一个装饰器类,该类包装了原始对象,并在调用原始对象的方法之前或之后添加额外的行为。

以下是一个简单的Java实现,用于装饰一个Component接口的实现类ConcreteComponent

// Component接口
interface Component {void operation();
}// ConcreteComponent类,实现了Component接口
class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("ConcreteComponent operation");}
}// Decorator抽象类,实现了Component接口,并持有一个Component类型的对象
abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();}
}// ConcreteDecoratorA类,继承了Decorator,并添加了额外的行为
class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedBehavior();}public void addedBehavior() {System.out.println("ConcreteDecoratorA added behavior");}
}// ConcreteDecoratorB类,继承了Decorator,并添加了额外的行为
class ConcreteDecoratorB extends Decorator {public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void operation() {addedState();super.operation();}public void addedState() {System.out.println("ConcreteDecoratorB added state");}
}// 测试装饰器模式
public class DecoratorPatternDemo {public static void main(String[] args) {Component component = new ConcreteComponent();// 使用装饰器A和B装饰原始组件Component decoratorA = new ConcreteDecoratorA(component);Component decoratorB = new ConcreteDecoratorB(decoratorA);// 调用装饰后的组件的方法decoratorB.operation();}
}

代理模式(Proxy Pattern)

代理模式也是一种结构型设计模式,它提供了一个代理对象来控制对另一个对象的访问。代理对象可以在访问真实对象之前或之后添加额外的行为。

以下是一个简单的Java实现,用于代理一个Subject接口的实现类RealSubject

// Subject接口
interface Subject {void request();
}// RealSubject类,实现了Subject接口
class RealSubject implements Subject {@Overridepublic void request() {System.out.println("RealSubject request");}
}// Proxy类,实现了Subject接口,并持有一个RealSubject类型的对象
class Proxy implements Subject {private RealSubject realSubject;@Overridepublic void request() {if (realSubject == null) {realSubject = new RealSubject();}preRequest();realSubject.request();postRequest();}public void preRequest() {System.out.println("Proxy pre-request");}public void postRequest() {System.out.println("Proxy post-request");}
}// 测试代理模式
public class ProxyPatternDemo {public static void main(String[] args) {Subject proxy = new Proxy();proxy.request();}
}

两者之间的区别

  1. 目的不同

    • 装饰器模式的主要目的是在不改变对象自身的基础上,动态地给对象添加职责(即功能)。
    • 代理模式的主要目的是控制对对象的访问,或者为对象提供一个代理以执行一些额外的操作(如安全检查、远程调用等)。
  2. 结构差异

    • 装饰器模式通常涉及到一个接口(或抽象类)和多个装饰器类,这些装饰器类都实现了相同的接口(或继承自相同的抽象类),并持有一个被装饰对象的引用。
    • 代理模式通常也涉及到一个接口(或抽象类)和一个代理类,但代理类通常只持有一个真实对象的引用,并在调用真实对象的方法之前或之后添加额外的行为。
  3. 行为扩展方式

    • 在装饰器模式中,装饰器类通过调用被装饰对象的方法,并在其前后添加额外的行为来实现功能扩展。
    • 在代理模式中,代理类通过调用真实对象的方法,并在其前后添加额外的行为来实现访问控制或功能增强。
  4. 使用场景

    • 装饰器模式适用于需要动态地给对象添加职责的场景,如GUI组件的装饰、服务功能的扩展等。
    • 代理模式适用于需要控制对对象的访问、为对象提供代理以执行额外操作的场景,如远程服务的调用、安全检查的代理等。
http://www.lryc.cn/news/517722.html

相关文章:

  • Proteus-8086调试汇编格式的一点心得
  • 什么是Kafka?有什么主要用途?
  • SpringBoot插件
  • UE 5.3 C++ 管理POI 如何对WidgetComponent 屏幕模式进行点击
  • Nginx实现接口复制
  • Selenium 八大元素定位方法及场景扩展
  • WebRTC 的优缺点详细解析
  • B树及其Java实现详解
  • 下载ffmpeg执行文件
  • Redis高频知识点
  • Boost.Asio 同步读写及客户端 - 服务器实现详解
  • LeetCode 3019.按键变更的次数:遍历(转小写)
  • ETCD未授权测试
  • 【Hystrix-1】Hystrix:构建弹性分布式系统的基石
  • 【超详细】MIT 液态神经网络(LNNs)——深度学习新动向
  • Git最便捷的迁移方式
  • 2024AAAI SCTNet论文阅读笔记
  • Laravel操作ElasticSearch
  • 江科大STM32入门——SPI通信笔记总结
  • 微信小程序map组件所有markers展示在视野范围内
  • 深度解析 tanh ⁡ tanh 激活函数
  • 嵌入式入门Day38
  • 探索Rancher服务发现机制:容器世界的“导航仪”
  • 【ROS2】Qt事件循环和ROS2订阅机制一起使用有什么注意事项?
  • donet (MVC)webAPI 的接受json 的操作
  • Qt 界面外观
  • aws(学习笔记第二十二课) 复杂的lambda应用程序(python zip打包)
  • HTML课堂之搜索工具箱/讲师duluo
  • 当歌 - RSS 订阅分发平台开发
  • 学习threejs,导入wrl格式的模型