
本文将探讨在无法访问源代码的情况下,如何实现多态行为。当我们需要对不同类型的对象执行相似的操作,但又无法修改这些对象的原始类时,适配器模式提供了一种优雅的解决方案。通过创建适配器类,我们可以将这些对象包装成具有公共接口的类型,从而实现多态调用,避免了大量的 if-else 判断。
假设我们有两个类 Car 和 Computer,它们的源代码我们无法访问。现在,我们需要创建两个服务 PhysicalDetailsService 和 PriceService,分别用于计算重量和价格。对于 Car 和 Computer,计算重量和价格的方式不同,但我们希望以一种统一的方式调用这些服务。
直接使用 if-else 判断对象类型来选择不同的计算方式显然不是一个好的选择,因为它会导致代码冗余且难以维护。那么,有没有更好的方法来实现这种多态行为呢?
适配器模式是一种结构型设计模式,它允许将一个类的接口转换成客户希望的另一个接口。适配器使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
在这个场景中,我们可以为 Car 和 Computer 分别创建一个适配器类,例如 CarWrapper 和 ComputerWrapper。这两个适配器类都实现一个公共接口 Physical,该接口定义了 getWeight() 方法。
interface Physical {
  int getWeight();
}
class CarWrapper implements Physical {
  private final Car car;
  public CarWrapper(Car car) {
    this.car = car;
  }
  @Override
  public int getWeight() {
    // 根据 Car 的属性计算重量
    return car.getTyreWeight() * 4 + car.getEngineWeight();
  }
}
class ComputerWrapper implements Physical {
  private final Computer computer;
  public ComputerWrapper(Computer computer) {
    this.computer = computer;
  }
  @Override
  public int getWeight() {
    // 根据 Computer 的属性计算重量
    return computer.getProcessorWeight() + computer.getCasingWeight() + computer.getPowerBankWeight();
  }
}通过这种方式,我们就可以将 Car 和 Computer 统一抽象为 Physical 类型,并可以使用相同的方式调用 getWeight() 方法。
有了适配器,我们可以很容易地实现 PhysicalDetailsService 和 PriceService。
class PhysicalDetailsService {
  public int calculateWeight(Physical physical) {
    return physical.getWeight();
  }
}
// 类似地,可以实现 PriceService为了方便使用,我们可以创建一个静态方法来根据对象的类型创建相应的适配器。
interface Physical {
  int getWeight();
  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 type: " + o.getClass().getName());
    }
  }
}现在,我们可以这样使用:
Car car = new Car();
Computer computer = new Computer();
PhysicalDetailsService physicalDetailsService = new PhysicalDetailsService();
int carWeight = physicalDetailsService.calculateWeight(Physical.wrap(car));
int computerWeight = physicalDetailsService.calculateWeight(Physical.wrap(computer));
System.out.println("Car weight: " + carWeight);
System.out.println("Computer weight: " + computerWeight);如果使用 Spring 框架,可以将 PhysicalDetailsService 配置为一个 Bean,并在需要使用的地方注入。
@Service
public class PhysicalDetailsService {
  public int calculateWeight(Physical physical) {
    return physical.getWeight();
  }
}然后在需要使用 PhysicalDetailsService 的地方,使用 @Autowired 注解注入即可。
@Service
public class MyService {
  @Autowired
  private PhysicalDetailsService physicalDetailsService;
  public void process(Object object) {
    Physical physical = Physical.wrap(object);
    int weight = physicalDetailsService.calculateWeight(physical);
    // ...
  }
}通过适配器模式,我们可以在不修改现有类源代码的情况下,实现多态行为。这种方法不仅代码清晰,而且易于扩展。当需要支持新的对象类型时,只需要创建一个新的适配器类即可,无需修改现有的代码。 此外,将适配器模式与 Spring 框架结合使用,可以更好地管理和使用这些服务,提高代码的可维护性和可测试性。
注意事项:
以上就是多态行为在无法访问源代码的情况下实现的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                 
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号