简体   繁体   English

工厂喜欢带有弹簧豆的图案,没有开关盒

[英]factory like pattern with spring bean without switch case

My goal is to return a method depending on a enum. 我的目标是根据枚举返回一个方法。 Currently, I have created a factory but it's using a switch case that I dislike. 目前,我已经创建了一个工厂,但它使用的是我不喜欢的开关盒。 The code is working fine but I want to use a better pattern and replace the current switch case. 代码工作正常,但我想使用更好的模式并替换当前的开关案例。 How would you design this without any switch case or if else (instance of)... 你如何设计这个没有任何开关案例或如果否则(实例)......

I tried also to implement a Stategy pattern in enum. 我还试图在枚举中实现一个Stategy模式。 But autowiring beans is not possible. 但是无法自动装配bean。

See below my current piece of code. 请参阅下面的我目前的代码。

public enum Car {
     AUDI, FORD;
}
public class SuperCar{
     private Car car;
}
public class Audi extends SuperCar{
     // some other properties
}
public class Ford extends SuperCar{
     // some other properties
}
@Service
public class AudiService{

     public void save(Audi audi){
         // some code
     }

}
@Service
public class FordService{

     public void save(Ford ford){
         // some code
     }

}
@Service
public class CarFactory{

     private FordService fordService;
     private AudiService audiService;

     public CarFactory(FordService fordService, AudiService audiService) {
           this.fordService = fordService;
           this.audiService = AudiService;     
     }

     public void saveCar(SuperCar superCar){
         switch(superCar.getCar()):
             case AUDI:
                 return audiService.save((Audi)superCar));
             case FORD:
                 return fordService.save((Ford)superCar));
             default:
                 return null;

     }

}

Thank you for any help. 感谢您的任何帮助。

I'm sorry that I can't comment. 对不起,我无法发表评论。 Here the type of the car determines the car service. 这里的汽车类型决定了汽车服务。 I'm not sure If the strategy patterns fits in here. 我不确定战略模式是否适合这里。 I would use strategy pattern when there is a varying service behavior for the same car . 同一辆车不同的服务行为时,我会使用策略模式。 Eg: In summer I want to use XService and in Winter I want to use YService for AUDI . 例如:夏天我想使用XService ,冬天我想使用YService作为AUDI I see two ways to implement this. 我认为有两种方法可以实现这一点。

  1. Inject the service during the creation of car object. 在创建汽车对象期间注入服务。 With this implementation the car is tightly coupled with service. 通过这种实施,汽车与服务紧密结合。 I don't recommend this, unless you have a strong reason to not follow point 2. 除非你有充分的理由不遵循第2点,否则我不建议这样做。
  2. Use if/else or branching to determine the type of car and call the required service. 使用if / else或分支来确定汽车的类型并调用所需的服务。

In case of just replacing the switch , I would always prefer a more declarative approach by using a map , since it seams easier to maintain and to read: 如果只是更换switch ,我总是更喜欢使用map的更具声明性的方法,因为它的接缝更易于维护和阅读:

private Map<Car, CarService> services;

public CarFactory(FordService fordService, AudiService audiService) {
   this.services = Map.of(Car.FORD, fordService, Car.AUDI, audiService);
}

public void saveCar(SuperCar superCar) {
   CarService service = services.get(superCar.getCar());
   if (service != null) service.save(..);
}

With the generic interface: 使用通用接口:

private interface CarService<T extends SuperCar> {
   void save(T car);
}

Anyway, I would rethink your object-model to let a super-car save itself (as others already suggested). 无论如何,我会重新考虑你的对象模型让超级汽车自救(正如其他人已经建议的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM