结构者设计模式

结构者模式(Structural Design Patterns)指的是一组设计模式,旨在帮助设计者将对象或类组合成更大的结构,以便于形成更复杂的系统。这些模式关注如何将对象或类组织起来,以构建更大的结构或系统。主要目的是提高系统的灵活性和可维护性。

常见的结构者模式包括:

  1. 适配器模式(Adapter Pattern)
  2. 桥接模式(Bridge Pattern)
  3. 组合模式(Composite Pattern)
  4. 装饰者模式(Decorator Pattern)
  5. 外观模式(Facade Pattern)
  6. 享元模式(Flyweight Pattern)
  7. 代理模式(Proxy Pattern)

1. 适配器模式(Adapter Pattern)

目的:将一个类的接口转换成客户端所期望的另一个接口,使得原本接口不兼容的类可以协同工作。

结构

  • 目标接口(Target Interface):客户端期望的接口。
  • 适配者(Adaptee):已经存在的接口,接口与客户端的期望接口不兼容。
  • 适配器(Adapter):实现目标接口,并将客户端请求转发到适配者对象。

示例代码(Java)

复制代码

java

复制代码

// 目标接口 interface Target { void request(); } // 适配者 class Adaptee { void specificRequest() { System.out.println("Specific request"); } } // 适配器 class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.specificRequest(); } } // 客户端代码 public class Main { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Target target = new Adapter(adaptee); target.request(); } }

2. 桥接模式(Bridge Pattern)

目的:将抽象部分与实现部分分离,使得两者可以独立变化。

结构

  • 抽象类(Abstraction):定义抽象的接口。
  • 修正抽象类(RefinedAbstraction):扩展抽象类。
  • 实现接口(Implementor):定义实现类的接口。
  • 具体实现类(ConcreteImplementor):实现实现接口。

示例代码(Java)

复制代码

java

复制代码

// 实现接口 interface Implementor { void operationImpl(); } // 具体实现类 class ConcreteImplementorA implements Implementor { @Override public void operationImpl() { System.out.println("ConcreteImplementorA operation"); } } // 抽象类 abstract class Abstraction { protected Implementor implementor; protected Abstraction(Implementor implementor) { this.implementor = implementor; } abstract void operation(); } // 修正抽象类 class RefinedAbstraction extends Abstraction { protected RefinedAbstraction(Implementor implementor) { super(implementor); } @Override void operation() { implementor.operationImpl(); } } // 客户端代码 public class Main { public static void main(String[] args) { Implementor impl = new ConcreteImplementorA(); Abstraction abs = new RefinedAbstraction(impl); abs.operation(); } }

3. 组合模式(Composite Pattern)

目的:允许客户以一致的方式处理单个对象和对象集合。

结构

  • 组件接口(Component):声明叶子和容器的共同接口。
  • 叶子(Leaf):实现组件接口,表示树的叶节点。
  • 容器(Composite):实现组件接口,表示树的节点,包含子组件。

示例代码(Java)

复制代码

java

复制代码

import java.util.ArrayList; import java.util.List; // 组件接口 interface Component { void operation(); } // 叶子 class Leaf implements Component { @Override public void operation() { System.out.println("Leaf operation"); } } // 容器 class Composite implements Component { private List<Component> children = new ArrayList<>(); void add(Component component) { children.add(component); } void remove(Component component) { children.remove(component); } @Override public void operation() { for (Component child : children) { child.operation(); } } } // 客户端代码 public class Main { public static void main(String[] args) { Component leaf1 = new Leaf(); Component leaf2 = new Leaf(); Composite composite = new Composite(); composite.add(leaf1); composite.add(leaf2); composite.operation(); } }

4. 装饰者模式(Decorator Pattern)

目的:动态地给对象添加功能,不改变其结构。

结构

  • 组件接口(Component):定义对象的接口。
  • 具体组件(ConcreteComponent):实现组件接口的基本对象。
  • 装饰器(Decorator):持有一个组件对象的引用,并提供相同的接口。
  • 具体装饰器(ConcreteDecorator):扩展装饰器,添加额外的功能。

示例代码(Java)

复制代码

java

复制代码

// 组件接口 interface Component { void operation(); } // 具体组件 class ConcreteComponent implements Component { @Override public void operation() { System.out.println("ConcreteComponent operation"); } } // 装饰器 abstract class Decorator implements Component { protected Component component; protected Decorator(Component component) { this.component = component; } @Override public void operation() { component.operation(); } } // 具体装饰器 class ConcreteDecorator extends Decorator { public ConcreteDecorator(Component component) { super(component); } @Override public void operation() { super.operation(); additionalOperation(); } private void additionalOperation() { System.out.println("ConcreteDecorator additional operation"); } } // 客户端代码 public class Main { public static void main(String[] args) { Component component = new ConcreteComponent(); Component decorator = new ConcreteDecorator(component); decorator.operation(); } }

5. 外观模式(Facade Pattern)

目的:为复杂子系统提供一个统一的接口,使得子系统更易于使用。

结构

  • 外观类(Facade):提供一个简单接口,隐藏子系统的复杂性。
  • 子系统类(Subsystem):复杂的子系统类,提供具体功能。

示例代码(Java)

复制代码

java

复制代码

// 子系统类 class SubsystemA { void operationA() { System.out.println("SubsystemA operation"); } } class SubsystemB { void operationB() { System.out.println("SubsystemB operation"); } } // 外观类 class Facade { private SubsystemA subsystemA = new SubsystemA(); private SubsystemB subsystemB = new SubsystemB(); void operation() { subsystemA.operationA(); subsystemB.operationB(); } } // 客户端代码 public class Main { public static void main(String[] args) { Facade facade = new Facade(); facade.operation(); } }

6. 享元模式(Flyweight Pattern)

目的:通过共享对象来减少内存消耗,提高效率。

结构

  • 享元接口(Flyweight):声明操作方法。
  • 具体享元(ConcreteFlyweight):实现享元接口,处理共享的状态。
  • 享元工厂(FlyweightFactory):管理享元对象的创建和共享。

示例代码(Java)

复制代码

java

复制代码

import java.util.HashMap; import java.util.Map; // 享元接口 interface Flyweight { void operation(String extrinsicState); } // 具体享元 class ConcreteFlyweight implements Flyweight { private String intrinsicState; public ConcreteFlyweight(String intrinsicState) { this.intrinsicState = intrinsicState; } @Override public void operation(String extrinsicState) { System.out.println("Intrinsic State: " + intrinsicState + ", Extrinsic State: " + extrinsicState); } } // 享元工厂 class FlyweightFactory { private Map<String, Flyweight> flyweights = new HashMap<>(); Flyweight getFlyweight(String intrinsicState) { if (!flyweights.containsKey(intrinsicState)) { flyweights.put(intrinsicState, new ConcreteFlyweight(intrinsicState)); } return flyweights.get(intrinsicState); } } // 客户端代码 public class Main { public static void main(String[] args) { FlyweightFactory factory = new FlyweightFactory(); Flyweight flyweight1 = factory.getFlyweight("StateA"); flyweight1.operation("ExtrinsicA"); Flyweight flyweight2 = factory.getFlyweight("StateA"); flyweight2.operation("ExtrinsicB"); Flyweight flyweight3 = factory.getFlyweight("StateB"); flyweight3.operation("ExtrinsicC"); } }

7. 代理模式(Proxy Pattern)

目的:为其他对象提供一种代理以控制对该对象的访问。

结构

  • 抽象主题(Subject):定义真实对象和代理对象的共同接口。
  • 真实主题(RealSubject):实际实现对象的类。
  • 代理(Proxy):持有对真实主题对象的引用,并控制对它的访问。

示例代码(Java)

复制代码

java

复制代码

// 抽象主题 interface Subject { void request(); } // 真实主题 class RealSubject implements Subject { @Override public void request() { System.out.println("RealSubject request"); } } // 代理 class Proxy implements Subject { private RealSubject realSubject; @Override public void request() { if (realSubject == null) { realSubject = new RealSubject(); } System.out.println("Proxy request"); realSubject.request(); } } // 客户端代码 public class Main { public static void main(String[] args) { Subject proxy = new Proxy(); proxy.request(); } }

总结

结构者模式帮助我们以不同的方式组织和构建系统中的类和对象,从而提高系统的灵活性和可维护性。通过合理选择和应用这些模式,可以有效地管理和扩展系统的复杂性。

相关推荐
晨米酱13 小时前
JavaScript 中"对象即函数"设计模式
前端·设计模式
数据智能老司机18 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机19 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机19 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机19 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
使一颗心免于哀伤19 小时前
《设计模式之禅》笔记摘录 - 21.状态模式
笔记·设计模式
数据智能老司机2 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机2 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
烛阴2 天前
【TS 设计模式完全指南】懒加载、缓存与权限控制:代理模式在 TypeScript 中的三大妙用
javascript·设计模式·typescript
李广坤2 天前
工厂模式
设计模式