多态行为:在没有源代码访问权限的情况下实现

多态行为:在没有源代码访问权限的情况下实现

在软件开发中,我们经常会遇到需要对现有类进行扩展或修改,以满足新的需求。但有时,我们可能无法直接访问这些类的源代码,这给实现多态行为带来了挑战。本文将探讨一种常用的设计模式——适配器模式,来解决这个问题,并以计算不同类型对象的重量为例进行说明。

假设我们有两个类 Car 和 Computer,它们分别代表汽车和计算机。我们希望创建一个 PhysicalDetailsService 服务,该服务能够计算这两种对象的重量。由于我们无法访问 Car 和 Computer 类的源代码,因此无法直接让它们继承一个公共的接口或基类。

适配器模式

适配器模式是一种结构型设计模式,它允许将一个类的接口转换成客户希望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的类可以一起工作。

在这种情况下,我们可以为 Car 和 Computer 类分别创建一个适配器,让它们都实现一个公共的接口,例如 Physical。

interface Physical {
  int getWeight();
}

class CarWrapper implements Physical {
  private final Car car;

  public CarWrapper(Car car) { this.car = car; }

  public int getWeight() {
    // 假设Car类有getTyreWeight()和getEngineWeight()方法
    return car.getTyreWeight() * 4 + car.getEngineWeight();
  }
}

class ComputerWrapper implements Physical {
  private final Computer computer;

  public ComputerWrapper(Computer computer) { this.computer = computer; }

  public int getWeight() {
    // 假设Computer类有getProcessorWeight()和getCasingWeight()方法
    return computer.getProcessorWeight() + computer.getCasingWeight() + computer.getPowerBankWeight();
  }
}

在上面的代码中,CarWrapper 和 ComputerWrapper 分别是 Car 和 Computer 类的适配器。它们都实现了 Physical 接口,并提供了 getWeight() 方法,该方法根据各自的内部逻辑计算重量。

使用适配器

现在,我们可以使用 Physical 接口来实现 PhysicalDetailsService 服务:

class PhysicalDetailsService {
  public int calculateWeight(Physical physical) {
    return physical.getWeight();
  }
}

使用示例:

Car car = new Car();
Computer computer = new Computer();

Physical carPhysical = new CarWrapper(car);
Physical computerPhysical = new ComputerWrapper(computer);

PhysicalDetailsService service = new PhysicalDetailsService();
int carWeight = service.calculateWeight(carPhysical);
int computerWeight = service.calculateWeight(computerPhysical);

System.out.println("Car weight: " + carWeight);
System.out.println("Computer weight: " + computerWeight);

适配器工厂

为了更方便地创建适配器,我们可以创建一个适配器工厂:

class PhysicalAdapterFactory {
  public static Physical wrap(Object o) {
    if (o instanceof Car car) {
      return new CarWrapper(car);
    } else if (o instanceof Computer computer) {
      return new ComputerWrapper(computer);
    } else {
      throw new IllegalArgumentException("Unsupported object type: " + o.getClass().getName());
    }
  }
}

使用示例:

Car car = new Car();
Computer computer = new Computer();

Physical carPhysical = PhysicalAdapterFactory.wrap(car);
Physical computerPhysical = PhysicalAdapterFactory.wrap(computer);

PhysicalDetailsService service = new PhysicalDetailsService();
int carWeight = service.calculateWeight(carPhysical);
int computerWeight = service.calculateWeight(computerPhysical);

System.out.println("Car weight: " + carWeight);
System.out.println("Computer weight: " + computerWeight);

注意事项

  • 适配器模式可以有效地解决无法修改现有类源代码的情况下实现多态行为的问题。
  • 适配器模式可能会增加代码的复杂性,因此需要谨慎使用。
  • 在设计适配器时,需要仔细考虑适配器的职责,避免适配器过于复杂。

总结

适配器模式是一种非常有用的设计模式,它可以在不修改现有类源代码的情况下,实现多态行为。通过为不同的类创建适配器,我们可以让它们都实现一个公共的接口,从而实现多态调用。这在处理第三方库或遗留代码时非常有用。