
本文将探讨在无法访问源代码的情况下,如何实现多态行为。当我们需要对不同类型的对象执行相似的操作,但又无法修改这些对象的原始类时,适配器模式提供了一种优雅的解决方案。通过创建适配器类,我们可以将这些对象包装成具有公共接口的类型,从而实现多态调用,避免了大量的 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。
免费 盛世企业网站管理系统(SnSee)系统完全免费使用,无任何功能模块使用限制,在使用过程中如遇到相关问题可以去官方论坛参与讨论。开源 系统Web代码完全开源,在您使用过程中可以根据自已实际情况加以调整或修改,完全可以满足您的需求。强大且灵活 独创的多语言功能,可以直接在后台自由设定语言版本,其语言版本不限数量,可根据自已需要进行任意设置;系统各模块可在后台自由设置及开启;强大且适用的后台管理支
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 Bean 配置
如果使用 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 框架结合使用,可以更好地管理和使用这些服务,提高代码的可维护性和可测试性。
注意事项:
- 在 Physical.wrap() 方法中,需要处理不支持的类型,抛出异常或返回默认值。
- 适配器类需要仔细设计,确保能够正确地将原始对象的属性转换为公共接口所需的数据。
- 在实际应用中,可以根据具体情况选择不同的适配器实现方式,例如类适配器、对象适配器等。









