One controller for each usecase?

I'm using a mvc design pattern for my webapp.

In some of the books I've read it seems like they are suggesting to use one controller for each of the usecase. Is this the right way to do it?

For me it seems like you get so many classes. If you are dealing with a User, and has to modify info about a User, are you going to have a

- registerUserController

- updateUserController

- deleteUserController

Or should you have an UserController containing an if-else statement and have 3 functions, one for each of the usecases according to the User.

It seems like the advice is that when you have many if-else statements you should instead use interfaces. Is that correct?

Thanks

[733 byte] By [vatora] at [2007-11-27 6:28:43]
# 1

> I'm using a mvc design pattern for my webapp.

> In some of the books I've read it seems like they are

> suggesting to use one controller for each of the

> usecase. Is this the right way to do it?

Typically you have one Controller per system and one actions/ commands class for each usecase.

> For me it seems like you get so many classes.

Providing each class encapsulates a single responsibility having lots of classes IS a very good thing.

> If you are dealing with a User, and has to modify info about

> a User, are you going to have a

> - registerUserController

> - updateUserController

> - deleteUserController

Yes.

> Or should you have an UserController containing an

> if-else statement and have 3 functions, one for each

> of the usecases according to the User.

You could but I recommend using the command pattern to implement the controller.

> It seems like the advice is that when you have many

> if-else statements you should instead use interfaces.

> Is that correct?

Yes, polymorphism is the underlying concept of the command pattern.

MartinS.a at 2007-7-12 17:52:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Thanks for a very good explanation!!

I'll explain my code a bit more detailed, so that I'm sure this is how you think I should do it.

- I'm using a frontcontroller and the Command/Action pattern.

- I'm not using a applicationcontroller.

- A controller is typically a servlet.

In my app I'm using an action class and an controller (servlet) class for each usecase. So this is the way to do it?

public class FrontControllerServlet extends HttpServlet {

private HashMap actions;

private ModelAndView mv;

public void init() throws ServletException {

// Action hashtable

actions = new HashMap();

// Makes action objects by putting controller objects into action objects

RegUserAction rua = new RegUserAction(new RegUserController());

UpdateUserAction uua = new UpdateUserAction(new UpdateUserController());

// Puts action objects into hash table

actions.put(rua.getName(), rua);

actions.put(rha.getName(), uua);

}

protected void processRequest(HttpServletRequest req, HttpServletResponse res){

op = request.getParameter("op");

Action action = (Action)actions.get(op);

ModelAndView mv = (ModelAndView) action.perform(request, response);

.....

} //End FrontController

public class RegUserAction extends Action{

public RegUserAction (Controller c) {super(c); }

public String getName() { return "regUser"; }

public Object perform(HttpServletRequest request, HttpServletResponse response) {

return c.handleRequest(request, response);

}

} //End RegUserAction

public class RegUserController implements Controller {

public Object handleRequest(HttpServletRequest req, HttpServletResponse res){

String userName = request.getParameter("userName");

String userPwd= request.getParameter("userPwd");

... Registers user in database

....Returns a ModelAndView object confimrming that the user has successfully been registred in the database.

} //End RegUserController

public interface Controller {

public Object handleRequest(HttpServletRequest req, HttpServletResponse res);

}

Does it seem like I'm on the right track?

vatora at 2007-7-12 17:52:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

In my webapp I'm going to search for some products and then view info about them. Would you recommend having a serachcontroller for each searchcriteria?

Lets say that I would search for hotelrooms and my searchcriteria are as follows:

-standard (normal, suite)

-number of beds

-price

Would you have a controller for each searchcriteria?

-StandardSearchController

-BedSearchController

-PriceSearchController

Or would you have a RoomSearchController containing an if-statement something like:

if(searchCriteria == roomStandard)

// get info about rooms from database

else if(searchCriteria == numBeds)

// get info about rooms from database

else if(searchCriteria == price)

// get info about rooms from database

Or is there another way to do this?

Thank for your last post by the way... Helped me a lot!

vatora at 2007-7-12 17:52:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Since you are trying to build your own MVC application, instead of using an existing framework to build off of, e.g. Struts, you will spend a lot of your time coding the controller functionality, maybe more than the search functionality.

In any case here are a few guides, taken from the Struts framework.

You create a single controller to handle all of the functionality. Action classes are part of the controller.

You need to design your controller to use the Action classes to execute its behavior.

Search is a single application function.

A single search may have criteria.

You would have a single Search action.

One controller.

One search action.

Design the search action to read criteria and do appropriate search.

The better way is to use Struts Controller to start with and build you application from there.

GhostRadioTwoa at 2007-7-12 17:52:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

I'll look into Struts if that results in less coding... It's a big job just to code the controllers.

Just a short explanation:

In my webapp I'm using a Frontcontroller that contains action objects, one actionobject for each use-case. Each of the actionobjects contains a controller, for instance the LogInAction contains a LogInController that takes care of the login.

The thing that makes me confused is that all the programming books I've read stresses so much the importance of polymorphism. My problem then is that I end up having a lot classes, and this takes a lot more time then if you just put the code in an if-else statement... In some cases you have to use if-else statements? Right?

Is there a rule of thumb for how many if-else statements you can have before you should use polymorphism?

vatora at 2007-7-12 17:52:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

Your confusion most likely stems from your current design. To reduce confusion you should change design to something more streamlined.

One controller, one search action. The search action receives criteria. The search action searches for different things based on what the criteria is.

Polymorphism is only important in the design of applications where it is applicable. It is unclear why you think polymorphism is related to the quantity of classes.

You have a lot of classes because the design is awkward.

Read again for clarity.

1. One controller, one action for each function.

2. Action classes are part of controller.

3. Controller contains actions.

4. Actions do not contain controllers.

5. One Controller, many actions

6. Example #1: Controller contains a LoginAction. The LoginAction delegates processing to Model. Inside the Model, login is taken care of. Example #2: Controller contains a SearchAction. The SearchAction delegates processing to Model. Inside the Model, the search is taken care of.

7. Learn how to build with Struts and confusion will slowly evaporate in a few years.

GhostRadioTwoa at 2007-7-12 17:52:13 > top of Java-index,Other Topics,Patterns & OO Design...