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

结构者设计模式

结构者模式(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(); } }

总结

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

相关文章:

  • Linux进程概念
  • 还不懂BIO,NIO,AIO吗
  • 命令模式(Command Pattern)
  • 因IBM裁员聊一聊外企程序员如何应对
  • 需求分析例题
  • STM32工程中各部分文件作用
  • 程序员卷技术前,先掌握这三种能力!
  • 企业出海网络方案,助力TikTok直播
  • HashMap常见面试题
  • 【OpenGL 002】着色器 GLSL 语言及GLFW代码案例
  • 2024最全前端面试系列(CSS)(盒模型、flex)
  • JAVA反射
  • [开源]YOLOv8+Pyside6的交通红绿灯目标检测源码
  • Kafka Broker处于高负载状态(例如消息处理量大或系统资源不足),无法及时响应消费者的请求
  • 在笔记本电脑上配置RTX GPU以使用TensorFlow和PyTorch的详细指南
  • 积分第二中值定理的证明
  • 算法【Java】 —— 前缀和
  • 使用 `readResolve` 防止序列化破坏单例模式
  • 测试过程中的不同版本含义
  • 深度学习从入门到精通——yolov1
  • 首次采用“顶置主星+侧挂从星”布局,长二丁“1箭12星”发射成功
  • 铁路部门:确保沿线群众安全,焦柳铁路6个区段将陆续安装防护栅栏
  • 超新星|罚丢点球的那道坎,刘诚宇靠自己迈了过去
  • 中美经贸高层会谈在日内瓦结束,中国代表团将举行发布会
  • 人民日报读者点题·共同关注:今天我们为什么还需要图书馆?
  • 未来之城湖州,正在书写怎样的城市未来