简体   繁体   中英

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. But autowiring beans is not possible.

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 . 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. Use if/else or branching to determine the type of car and call the required service.

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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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