I am a little confused. I tried to make a simple web app when user choose some type of client and operations performed in services will depend on this choice. I create interface that each of my client implements but how can I decide which implementation of client? If I will use if/else construction this will break open/closed principle(that is true?), cause when I would like to add new client i need to modify my code which adding new else. How can I do this correctly?
@RestController
public class SomeRest {
@Autowired
private SomeService someService;
@GetMapping("/test")
private Set<String> test(@RequestBody ClientType type) {
return someService.method(type);
}
}
@Service
public class SomeService {
private SomeInterface someInterface;
public Set<String> method(ClientType type) {
if (type.equals(ClientType.TYPE_ONE)) {
someInterface = new ClientOne();
return someInterface.operation();
} else if (type.equals(ClientType.TYPE_TWO)) {
someInterface = new ClientTwo();
return someInterface.operation();
}
return null;
}
}