结构型模式(Structural Patterns)
结构型模式(Structural Patterns)主要关注类和对象的组合方式,旨在通过不同的结构组合方式来建立新的功能。结构型模式可以帮助开发者确保即使对象组合方式改变,也能保持系统的整体结构和功能的一致性,提高代码的可扩展性和复用性。
1. 适配器模式 (Adapter Pattern)
定义:将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的类可以一起工作。
详细释义:适配器模式通过创建一个适配器类,将一个类的接口转换为客户期望的另一个接口,从而使得原本由于接口不兼容而无法一起工作的类可以一起工作。适配器可以是对象适配器(通过组合实现)或类适配器(通过继承实现)。
// 目标接口
public interface Target {
void request();
}
// 需要适配的类
public class Adaptee {
public void specificRequest() {
System.out.println("Specific request");
}
}
// 对象适配器
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void request() {
adaptee.specificRequest();
}
}
2. 桥接模式 (Bridge Pattern)
定义:将抽象部分与实现部分分离,使它们可以独立变化。
详细释义:桥接模式通过将抽象部分与其具体实现分离,使得它们可以独立变化。抽象部分定义高层控制逻辑,而具体实现部分则负责底层具体操作。这种模式通过在抽象部分和实现部分之间引入桥接接口,使得它们之间的依赖关系变得更加灵活和可扩展。
// 实现部分接口
public interface Implementor {
void operationImpl();
}
// 具体实现部分A
public class ConcreteImplementorA implements Implementor {
public void operationImpl() {
System.out.println("ConcreteImplementorA operation");
}
}
// 抽象部分
public abstract class Abstraction {
protected Implementor implementor;
protected Abstraction(Implementor implementor) {
this.implementor = implementor;
}
public abstract void operation();
}
// 扩展抽象部分
public class RefinedAbstraction extends Abstraction {
public RefinedAbstraction(Implementor implementor) {
super(implementor);
}
public void operation() {
implementor.operationImpl();
}
}
3. 组合模式 (Composite Pattern)
定义:将对象组合成树形结构以表示“部分-整体”的层次结构,使得客户可以统一地对待单个对象和组合对象。
详细释义:组合模式允许你将对象组合成树形结构以表示“部分-整体”的层次结构,从而使客户可以统一地对待单个对象和组合对象。这种模式对于那些希望通过递归组合复杂对象的情况特别有效。
// 组件接口
public interface Component {
void operation();
}
// 叶子组件
public class Leaf implements Component {
public void operation() {
System.out.println("Leaf operation");
}
}
// 组合组件
public class Composite implements Component {
private List<Component> children = new ArrayList<>();
public void add(Component component) {
children.add(component);
}
public void remove(Component component) {
children.remove(component);
}
public void operation() {
for (Component component : children) {
component.operation();
}
}
}
4. 装饰模式 (Decorator Pattern)
定义:动态地给对象添加职责,通过创建一个装饰类来包装原始类。
详细释义:装饰模式通过创建一个装饰类来包装原始类,从而在不改变原始类的情况下动态地给对象添加职责。装饰模式允许你在运行时扩展对象的功能,并且比继承更灵活。
// 组件接口
public interface Component {
void operation();
}
// 具体组件
public class ConcreteComponent implements Component {
public void operation() {
System.out.println("ConcreteComponent operation");
}
}
// 装饰抽象类
public abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
}
}
// 具体装饰类
public class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component component) {
super(component);
}
public void operation() {
super.operation();
addedBehavior();
}
private void addedBehavior() {
System.out.println("ConcreteDecorator added behavior");
}
}
5. 外观模式 (Facade Pattern)
定义:为子系统中的一组接口提供一个一致的界面,使得子系统更容易使用。
详细释义:外观模式通过为子系统中的一组接口提供一个一致的界面,简化了复杂系统的使用。外观模式使得子系统更容易使用,隐藏了其复杂性,提供了一个简单的接口供客户端调用。
public class SubsystemA {
public void operationA() {
System.out.println("SubsystemA operation");
}
}
public class SubsystemB {
public void operationB() {
System.out.println("SubsystemB operation");
}
}
public class Facade {
private SubsystemA subsystemA;
private SubsystemB subsystemB;
public Facade() {
subsystemA = new SubsystemA();
subsystemB = new SubsystemB();
}
public void operation() {
subsystemA.operationA();
subsystemB.operationB();
}
}
6. 享元模式 (Flyweight Pattern)
定义:使用共享对象来有效地支持大量细粒度的对象。
详细释义:享元模式通过共享来减少大量细粒度对象的数量,从而节省内存。享元模式将对象的状态分为内部状态和外部状态,内部状态是共享的,不会随环境变化,而外部状态是依赖于环境的,可以根据需要进行变化。
public interface Flyweight {
void operation(String extrinsicState);
}
public class ConcreteFlyweight implements Flyweight {
private String intrinsicState;
public ConcreteFlyweight(String intrinsicState) {
this.intrinsicState = intrinsicState;
}
public void operation(String extrinsicState) {
System.out.println("IntrinsicState: " + intrinsicState + ", ExtrinsicState: " + extrinsicState);
}
}
public class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
if (!flyweights.containsKey(key)) {
flyweights.put(key, new ConcreteFlyweight(key));
}
return flyweights.get(key);
}
}
7. 代理模式 (Proxy Pattern)
定义:为另一个对象提供一个代理以控制对这个对象的访问。
详细释义:代理模式通过为某个对象提供一个代理对象,控制对这个对象的访问。代理模式可以用于延迟实例化、控制访问权限、记录日志等。代理对象和实际对象实现相同的接口,从而实现对实际对象的控制和管理。
public interface Subject {
void request();
}
public class RealSubject implements Subject {
public void request() {
System.out.println("RealSubject request");
}
}
public class Proxy implements Subject {
private RealSubject realSubject;
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.request();
}
}
这些结构型模式通过不同的对象组合方式,提供了灵活的系统结构,提高了代码的可扩展性和复用性。