I don't know if this question belongs here or softwareengineering.stackexchange. I have a service named car:
public interface CarSerivce{ Car create(String model);}
Now I want to have two capabilities, create and create random. Creating a random car does not need a model and the service should select one available random model. So effectively this could be the implementations:
@Service
public class CarServiceImpl implements CarService{
public Car create(String name){...}
}
//This one cannot implement the interface
@Service
public class RandomCarServiceImpl{ public Car create(){...}}
One solution is to externalize creating a random name by the class that uses the service and passing it to the service. But it is not an option because this example is contrived and the real one needs a rather sophisticated random generation algorithm for many parameters, not just one. My second idea is to have one implementation with two methods, create(String name) and createRandom() (or just an overloaded create with no parameter). This works fine and this way I can put all creation codes inside create method and call it from createRandom method. Since it is about selecting the strategy (just like strategy design pattern) I was wondering if there is a better solution to this.