Guest User

Untitled

a guest
May 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. @RestController
  2. @RequiredArgsConstructor(onConstructor = @__(@Autowired))
  3. class Controller {
  4.  
  5. /* inject here */
  6. public @NonNull GenericService service;
  7.  
  8. public void doo(List<GenericDTO> list, String type) {
  9. service.doo(list);
  10. }
  11.  
  12. }
  13.  
  14. class GenericDTO { ... }
  15. class GenericService { ... }
  16.  
  17. class OfferService extends GenericService { ... }
  18. class OtherService extends GenericService { ... }
  19.  
  20. Class MyController {
  21.  
  22. Map<String, GenericService> servicesMap = new HashMap<String, GenericService>();
  23.  
  24. @Autowired
  25. public setServices(List<GenericService> serviceList) {
  26. for (GenericService service : serviceList) {
  27. if (service instanceof OfferService) {
  28. serviceMap.put("offer", service);
  29. }
  30. if (service instanceof OtherService) {
  31. serviceMap.put("other", service);
  32. }
  33. }
  34.  
  35. public void doo(List<GenericDTO> list, String type) {
  36. GenericService service = serviceMap.get(type);
  37. if (null != service) {
  38. service.doo(list);
  39. }
  40. }
  41. }
  42.  
  43. Class GenericService {
  44.  
  45. // This method could very well be an abstract method or a simple
  46. // getter for a property set through ctor
  47.  
  48. public String getType() {
  49. return "unknown"; // or "generic" if you please
  50. }
  51.  
  52. }
  53.  
  54. Class OfferClass extends GenericService {
  55. @Override
  56. public String getType() {
  57. return "offer";
  58. }
  59. }
  60.  
  61. Class OtherClass extends GenericService {
  62. @Override
  63. public String getType() {
  64. return "other";
  65. }
  66. }
  67.  
  68. Class MyController {
  69.  
  70. Map<String, GenericService> servicesMap = new HashMap<String, GenericService>();
  71. // This is just there so that we do not have to process the
  72. // list everytime we need to invoke the service
  73.  
  74. @Autowired
  75. public setServices(List<GenericService> serviceList) {
  76. for (GenericService service : serviceList) {
  77. serviceMap.put(service.getType(), service)
  78. }
  79. }
  80.  
  81. public void doo(List<GenericDTO> list, String type) {
  82. GenericService service = serviceMap.get(type);
  83. if (null != service) {
  84. service.doo(list);
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment