Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestController
- @RequiredArgsConstructor(onConstructor = @__(@Autowired))
- class Controller {
- /* inject here */
- public @NonNull GenericService service;
- public void doo(List<GenericDTO> list, String type) {
- service.doo(list);
- }
- }
- class GenericDTO { ... }
- class GenericService { ... }
- class OfferService extends GenericService { ... }
- class OtherService extends GenericService { ... }
- Class MyController {
- Map<String, GenericService> servicesMap = new HashMap<String, GenericService>();
- @Autowired
- public setServices(List<GenericService> serviceList) {
- for (GenericService service : serviceList) {
- if (service instanceof OfferService) {
- serviceMap.put("offer", service);
- }
- if (service instanceof OtherService) {
- serviceMap.put("other", service);
- }
- }
- public void doo(List<GenericDTO> list, String type) {
- GenericService service = serviceMap.get(type);
- if (null != service) {
- service.doo(list);
- }
- }
- }
- Class GenericService {
- // This method could very well be an abstract method or a simple
- // getter for a property set through ctor
- public String getType() {
- return "unknown"; // or "generic" if you please
- }
- }
- Class OfferClass extends GenericService {
- @Override
- public String getType() {
- return "offer";
- }
- }
- Class OtherClass extends GenericService {
- @Override
- public String getType() {
- return "other";
- }
- }
- Class MyController {
- Map<String, GenericService> servicesMap = new HashMap<String, GenericService>();
- // This is just there so that we do not have to process the
- // list everytime we need to invoke the service
- @Autowired
- public setServices(List<GenericService> serviceList) {
- for (GenericService service : serviceList) {
- serviceMap.put(service.getType(), service)
- }
- }
- public void doo(List<GenericDTO> list, String type) {
- GenericService service = serviceMap.get(type);
- if (null != service) {
- service.doo(list);
- }
- }
- }
Add Comment
Please, Sign In to add comment