您的位置:首页 > 科技 > IT业 > 设计模式使用场景实现示例(结构型模式——适配器模式、装饰器模式)

设计模式使用场景实现示例(结构型模式——适配器模式、装饰器模式)

2024/10/6 10:29:56 来源:https://blog.csdn.net/weixin_45049746/article/details/140325352  浏览:    关键词:设计模式使用场景实现示例(结构型模式——适配器模式、装饰器模式)

结构型模式

适配器模式(Adapter Pattern)

适配器模式(Adapter Pattern)是一种结构型设计模式,它允许将一个类的接口转换成客户端所期待的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的类可以协同工作。

适用场景

  1. 希望复用一些现存的类,但接口不符合要求

    • 系统中有多个接口相似的类,但接口不完全相同,需要一个统一的接口来调用这些类。
  2. 希望使用一些已有的类,但它们的接口与需求接口不一致

    • 使用第三方库或老旧系统中的类,但它们的接口和当前系统的不一致。

实现示例(Java)

以下是一个简单的适配器模式的实现示例,展示如何将一个不兼容的接口适配到一个目标接口。

1. 定义目标接口
public interface Target {void request();
}
2. 定义适配者类(Adaptee)
public class Adaptee {public void specificRequest() {System.out.println("Called specificRequest()");}
}
3. 定义适配器类(Adapter)
public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {this.adaptee = adaptee;}@Overridepublic void request() {adaptee.specificRequest();}
}
4. 客户端代码
public class Client {public static void main(String[] args) {Adaptee adaptee = new Adaptee();Target target = new Adapter(adaptee);target.request();}
}

注释说明

  1. 目标接口

    • Target 接口定义了客户所期待的接口,包含一个 request 方法。
  2. 适配者类

    • Adaptee 类包含一个 specificRequest 方法,这是需要适配的接口。
  3. 适配器类

    • Adapter 类实现了 Target 接口,并持有一个 Adaptee 对象。它在 request 方法中调用了 AdapteespecificRequest 方法,以实现接口的适配。
  4. 客户端代码

    • Client 类中创建了 AdapteeAdapter 对象,通过 Adapter 对象调用 request 方法,从而实现接口的适配。

类图

Client ----> Target^|Adapter -------> Adaptee

总结

适配器模式通过引入一个适配器类,将原本不兼容的接口适配到目标接口,使得原本不能一起工作的类可以协同工作。它适用于复用现有类但接口不兼容的场景,能够提高代码的复用性和灵活性。

装饰器模式(Decorator Pattern)

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许你动态地为对象添加新的行为,而无需改变其代码。装饰器模式通过创建一个装饰类来包装原始类,从而增强或改变其功能。

适用场景

  1. 需要扩展一个类的功能

    • 需要为一个类添加一些额外的职责,而不希望修改原始类的代码。
  2. 动态添加功能

    • 希望通过组合多个装饰器动态地添加功能。
  3. 希望实现功能的按需组合

    • 通过多个装饰器可以组合出不同的功能组合,避免创建大量的子类。

实现示例(Java)

以下是一个简单的装饰器模式的实现示例,展示如何动态地为一个对象添加新功能。

1. 定义组件接口
public interface Component {void operation();
}
2. 定义具体组件类
public class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("ConcreteComponent operation");}
}
3. 定义装饰器抽象类
public abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();}
}
4. 定义具体装饰器类
public class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedBehavior();}private void addedBehavior() {System.out.println("ConcreteDecoratorA added behavior");}
}public class ConcreteDecoratorB extends Decorator {public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void operation() {super.operation();addedBehavior();}private void addedBehavior() {System.out.println("ConcreteDecoratorB added behavior");}
}
5. 客户端代码
public class Client {public static void main(String[] args) {Component component = new ConcreteComponent();Component decoratorA = new ConcreteDecoratorA(component);Component decoratorB = new ConcreteDecoratorB(decoratorA);decoratorB.operation();}
}

注释说明

  1. 组件接口

    • Component 接口定义了一个方法 operation,这是所有组件和装饰器都需要实现的方法。
  2. 具体组件类

    • ConcreteComponent 是一个具体的组件类,实现了 Component 接口的方法。
  3. 装饰器抽象类

    • Decorator 是一个抽象类,实现了 Component 接口,并持有一个 Component 对象。它的 operation 方法调用了被装饰对象的 operation 方法。
  4. 具体装饰器类

    • ConcreteDecoratorAConcreteDecoratorB 是具体的装饰器类,它们继承自 Decorator 并添加了新的行为。
  5. 客户端代码

    • Client 类通过组合多个装饰器来增强 ConcreteComponent 的功能。decoratorB 包装了 decoratorA,而 decoratorA 包装了 component,最终调用 operation 方法时,会依次调用各个装饰器和具体组件的方法。

类图

Client|v
Component <---- Decorator <---- ConcreteDecoratorA^                                ||                                v
ConcreteComponent          ConcreteDecoratorB

总结

装饰器模式通过创建装饰器类来包装原始类,使得你可以动态地添加或改变对象的行为。它适用于需要扩展类的功能且不希望修改原始类代码的场景,同时通过组合多个装饰器可以实现功能的按需组合,避免创建大量的子类。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com