响应式网站怎么样,源代码做的网站好用么,网站建设前台和后台,兴宁网站设计文章目录 装饰器模式#xff08;Decorator Pattern#xff09;代理模式#xff08;Proxy Pattern#xff09;两者之间的区别 装饰器模式#xff08;Decorator Pattern#xff09;
装饰器模式是一种结构型设计模式#xff0c;它允许你动态地将责任附加到对象上#xff… 文章目录 装饰器模式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();}
}两者之间的区别 目的不同 装饰器模式的主要目的是在不改变对象自身的基础上动态地给对象添加职责即功能。代理模式的主要目的是控制对对象的访问或者为对象提供一个代理以执行一些额外的操作如安全检查、远程调用等。 结构差异 装饰器模式通常涉及到一个接口或抽象类和多个装饰器类这些装饰器类都实现了相同的接口或继承自相同的抽象类并持有一个被装饰对象的引用。代理模式通常也涉及到一个接口或抽象类和一个代理类但代理类通常只持有一个真实对象的引用并在调用真实对象的方法之前或之后添加额外的行为。 行为扩展方式 在装饰器模式中装饰器类通过调用被装饰对象的方法并在其前后添加额外的行为来实现功能扩展。在代理模式中代理类通过调用真实对象的方法并在其前后添加额外的行为来实现访问控制或功能增强。 使用场景 装饰器模式适用于需要动态地给对象添加职责的场景如GUI组件的装饰、服务功能的扩展等。代理模式适用于需要控制对对象的访问、为对象提供代理以执行额外操作的场景如远程服务的调用、安全检查的代理等。