Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I call my class controller, manager or service?

Maybe it is obvious to you. I'm new to java (half year work) and I had a discussion with my collegues. I have troubles naming my classes according to their responsibilities. For that, my classes gain responsibilities they should never have.

Can you help me?

BTW: I'm working current at a project where I have to use a persistance-layer from service classes. I have split my packages to model, service and persistance.

like image 638
MartinL Avatar asked Dec 06 '11 16:12

MartinL


People also ask

What is the difference between a service and a controller?

A Manager / Worker analogy In our analogy, the controller is the manager, while the service is the worker. If you think about what the manager's role is, he/she typically: manages the incoming work requests. decides which worker should do the work.

What does a manager class do?

The Manager class will consult its list of DBConnection objects, and determine if any of them is un-allocated, and return one. If all are allocated, it will either create one and add to the pool (subject to max connections allowed limit) or place the request on queue, or report back failure.

Should a controller call multiple services?

Technically there is nothing that prevents you from calling multiple services from your controller, but it is probably a bad design decision, mainly due to the Single Responsibility Principle.

What is the use of service class?

A Service class is used by a client to interact with some functionality in your application. Usually it is public, and has some business meaning. For example, a TicketingService class might allow you to buyTicket, sellTicket and so on.


1 Answers

There are certain patterns and guidelines behind these term, which is where I usually base it on:

Controller is based on the Model-View-Controller design pattern and should be used explicitly for the classes that implement the controller functionality based on this design pattern. E.g. if you are using Spring MVC and you extend from one of the Controller classes.

Service is a little less specific, but I recommend basing the implementation on the Service Layer pattern from "Patterns of Enterprise Application Architecture". Basically where a controller is more platform specific (e.g. transport via HTTP and rendering of Hypertext, usually HTML for web based controllers) a service shouldn't have to know about who is using it and how. You are just providing a uniform interface that can be used in turn by e.g. a web controller.

Managers well... manage stuff. Connections, application context, sessions; usually as a central location where components throughout the application can talk to.

like image 120
Daff Avatar answered Sep 19 '22 20:09

Daff