欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > Java设计模式 七 结构型模式 (Structural Patterns)

Java设计模式 七 结构型模式 (Structural Patterns)

2025/9/15 4:46:51 来源:https://blog.csdn.net/WithCYwind/article/details/145285497  浏览:    关键词:Java设计模式 七 结构型模式 (Structural Patterns)

结构型模式 (Structural Patterns) 是一种设计模式,主要关注如何通过合理组织类和对象来形成更大的结构。它解决了类与对象之间的组合或继承关系,旨在实现更高的灵活性和可扩展性,同时减少系统复杂度。

结构型模式有以下几种:


1. 适配器模式 (Adapter Pattern)

定义:
将一个类的接口转换成客户端期望的另一个接口,使原本不兼容的接口能够协同工作。

应用场景:

  • 需要复用现有类,但它的接口不符合当前需求时。
  • 不修改原有类的情况下,让新旧代码协同工作。

示例代码:

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

2. 桥接模式 (Bridge Pattern)

定义:
将抽象部分与其实现部分分离,使它们可以独立变化。

应用场景:

  • 避免多层继承导致的类爆炸问题。
  • 需要将抽象和实现解耦,使它们独立变化。

示例代码:

// 实现部分接口
public interface Implementor {void operationImpl();
}// 具体实现
public class ConcreteImplementorA implements Implementor {@Overridepublic void operationImpl() {System.out.println("ConcreteImplementorA operation.");}
}// 抽象部分
public abstract class Abstraction {protected Implementor implementor;public Abstraction(Implementor implementor) {this.implementor = implementor;}public abstract void operation();
}// 扩展抽象
public class RefinedAbstraction extends Abstraction {public RefinedAbstraction(Implementor implementor) {super(implementor);}@Overridepublic void operation() {implementor.operationImpl();}
}// 客户端
public class Client {public static void main(String[] args) {Implementor implementor = new ConcreteImplementorA();Abstraction abstraction = new RefinedAbstraction(implementor);abstraction.operation();}
}

3. 装饰器模式 (Decorator Pattern)

定义:
动态地为对象添加新的职责,而不改变其结构。

应用场景:

  • 动态扩展对象的功能。
  • 替代继承以增强功能。

示例代码:

// 抽象组件
public interface Component {void operation();
}// 具体组件
public class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("ConcreteComponent operation.");}
}// 抽象装饰器
public abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();}
}// 具体装饰器
public class ConcreteDecoratorA extends Decorator {public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void operation() {super.operation();System.out.println("ConcreteDecoratorA additional operation.");}
}// 客户端
public class Client {public static void main(String[] args) {Component component = new ConcreteComponent();Component decorator = new ConcreteDecoratorA(component);decorator.operation();}
}

4. 外观模式 (Facade Pattern)

定义:
为子系统中的一组接口提供一个统一的高层接口,使子系统更易于使用。

应用场景:

  • 简化复杂子系统的使用。
  • 需要为子系统提供单一入口点。

示例代码:

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

5. 享元模式 (Flyweight Pattern)

定义:
通过共享技术有效地支持大量细粒度对象。

应用场景:

  • 系统中存在大量相似对象,且这些对象可以共享状态。

示例代码:

import java.util.HashMap;
import java.util.Map;// 抽象享元
public interface Flyweight {void operation(String extrinsicState);
}// 具体享元
public class ConcreteFlyweight implements Flyweight {private String intrinsicState;public ConcreteFlyweight(String intrinsicState) {this.intrinsicState = intrinsicState;}@Overridepublic void operation(String extrinsicState) {System.out.println("IntrinsicState: " + intrinsicState + ", ExtrinsicState: " + extrinsicState);}
}// 享元工厂
public class FlyweightFactory {private Map<String, Flyweight> pool = new HashMap<>();public Flyweight getFlyweight(String key) {if (!pool.containsKey(key)) {pool.put(key, new ConcreteFlyweight(key));}return pool.get(key);}
}// 客户端
public class Client {public static void main(String[] args) {FlyweightFactory factory = new FlyweightFactory();Flyweight flyweight1 = factory.getFlyweight("A");Flyweight flyweight2 = factory.getFlyweight("B");Flyweight flyweight3 = factory.getFlyweight("A");flyweight1.operation("X");flyweight2.operation("Y");flyweight3.operation("Z");}
}

6. 代理模式 (Proxy Pattern)

定义:
为其他对象提供一种代理以控制对这个对象的访问。

应用场景:

  • 控制访问权限。
  • 延迟加载。
  • 提供远程代理。

示例代码:

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

总结

模式作用示例
适配器模式使接口不兼容的类可以协同工作InputStreamReader
桥接模式分离抽象和实现,使两者独立变化图形库(形状与颜色)
装饰器模式动态扩展对象的功能BufferedReader
外观模式提供子系统的简化接口数据库连接管理
享元模式共享对象以减少内存使用字符对象池
代理模式控制对象访问或增加额外功能动态代理

这些模式通过不同的方式优化代码结构,使系统更加灵活、可维护。根据实际需求选择合适的模式可以有效提升代码质量。

版权声明:

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

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

热搜词