14

I have an controller that call three services :

public class ProductController() {
    @Autowired
    private AccountService accountService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private releaseService releaseService;

    @RequestMapping("/process")
    public Product process(@RequestParam(value="name", defaultValue="docs")     ProductProcessed process) {

        accountService.notify();
        releaseService.sendRelease(process);


        return processService.process(process);
    }
}

What is the best approach to encapsulate this service calls??

Yash
  • 11,486
  • 4
  • 19
  • 35
Vipercold
  • 599
  • 1
  • 7
  • 27

2 Answers2

10

What you are looking for is possibly some design patterns. I approach could be to create a coarse-grained facade over the fine-grained services (Account, Process and Release). (see also Coarse-grained vs fine-grained)

The Facade will basically have these 3 services injected in them and encapsulate the behavior you are making your controller perform currently. This way you will minimize the business logic to invoking the coarse grained service in your controller thus further encapsulating the guts of the system.

Community
  • 1
  • 1
Y123
  • 915
  • 13
  • 30
  • 2
    will the facade broke the single responsibility principle? – Vipercold Apr 01 '15 at 16:40
  • 2
    Good followup - no it won't break the single responsibility. In fact it is in lines with the principle you pointed out. The Facade will be responsible for encapsulating and the internal working of your components. The controller will be responsible for handling incoming requests and redirecting to a view - the business logic aspect is now "delegated" to the facade by the controller. – Y123 Apr 01 '15 at 16:56
  • You can absolutely do this. Sometimes it's overkill though, so be sure that you actually have a need for a facade. – riddle_me_this Apr 01 '15 at 17:12
1

You already have them marked as private, so they cannot be called outside of this class. This is encapsulated.

A common practice is to autowire them so the implementation of the service can be modified.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80