使用泛型<k>来减少重复代码?

时间:2018-02-20 05:54:28

标签: java oop generics inheritance

我有以下代码。正如你所看到的那样,我正在执行类似的逻辑,但一次用于自行车,一次用于汽车我可以使用<K>来减少重复查看代码吗?我没有使用<K>因此我不确定我可以在何处以及如何合并它。我在哪里可以决定是否致电getCarsWithFeaturegetBikesWithFeature

最佳做法是减少行数(可能使其不太可读)或拥有这样看起来重复的代码?

public Set<Car> getPremiumCars(String filter) {
    final Callable<Set<Car>> retryGetCars = new RetryingCallable<>(retryStrategy(), getCars(filter));  
    return retryGetCars.call();
}

public Callable<Set<Car>> getCars(String feature) {
    return new Callable<Set<Car>>() {
        @Override
        public Set<Car> call() throws Exception {
            Set<Car> cars = getCarsWithFeature(feature); 
            return Collections.unmodifiableSet(cars);
        }
    };
}

public Set<Bike> getPremiumBikes(String filter) {
    final Callable<Set<Bike>> retryGetBikes = new RetryingCallable<>(retryStrategy(), getBikes(filter));
    return retryGetBikes.call();            
}

public Callable<Set<Bike>> getBikes(String feature) {
    return new Callable<Set<Bike>>() {
        @Override
        public Set<Bike> call() throws Exception {
            Set<Bike> bikes = getBikesWithFeature(feature); 
            return Collections.unmodifiableSet(bikes);
        }
    };
}

2 个答案:

答案 0 :(得分:2)

我不知道你的整个代码,但我建议两个类都实现相同的界面 - 让我们说Vehicle

  public interface Vehicle {
  }

然后你可以编写最近可以重用的代码:

  public <T extends Vehicle> Set<T> getPremiumVehicle(Function<String, Callable<Set<T>>> vehicleSupplier, String filter) throws Exception {
    final Callable<Set<T>> retryGetCars = new RetryingCallable<T>(retryStrategy(), vehicleSupplier.apply(filter));
    return retryGetCars.call();
  }

  public <T extends Vehicle> Callable<Set<T>> getVehicle(Function<String, Set<T>> vehicleSupplier, String feature) {
    return () -> {
      Set<T> vehicles = vehicleSupplier.apply(feature);
      return Collections.unmodifiableSet(vehicles);
    };
  }

现在,您可以重复上述代码,例如:

  public Set<Car> getPremiumCars(String filter) throws Exception {
    return getPremiumVehicle(this::getCars, filter);
  }

  public Set<Bike> getPremiumBikes(String filter) throws Exception {
    return getPremiumVehicle(this::getBikes, filter);
  }

  public Callable<Set<Car>> getCars(String feature) {
    return getVehicle(this::getCarsWithFeature, feature);
  }

  public Callable<Set<Bike>> getBikes(String feature) {
    return getVehicle(this::getBikesWithFeature, feature);
  }

答案 1 :(得分:1)

创建Car和Bike的基类,然后将常用方法放在那里。 然后从它伸出汽车和自行车。使用基类更新公共方法。下面给出了实施的示例提示:

class Vehicle {
  public Set<Vehicle> getWithFilter(String filter) {
    final Callable<Set<Vehicle>> retryGet = new RetryingCallable<>(retryStrategy(), get(filter));
    return retryGet.call();            
  }
  public Callable<Set<Vehicle>> getWithFeature(String feature) {
    return new Callable<Set<Vehicle>>() {
        public Set<Vehicle> call() throws Exception {
            Set<Vehicle> vehicles = getWithFeature(feature); 
            return Collections.unmodifiableSet(vehicles);
        }
    };
  }

} 
class Car extends Vehicle {
}
class Bike extends Vehicle {
}

现在打电话使用:

Car car = new Car();
car.getWithFilter(/* Pass parameter*/);

Bike bike = new Bike();
bike.getWithFilter(/* Pass parameter*/);