1

Is it correct/ a best practice to have a commonBean for operations that are common to the application? In my case this would be a logout function that clears the logged value of a session bean.

One of my coworkers told me that only one controller bean should be used per view, however it seems like a waste to have the same method replicated in multiple beans.

4

1 回答 1

5

Your colleague is referring to the seperation of View and Controller as a loosely defined principle in a pure MVC pattern. The Managed Bean in a JSF application lends itself well to the role of a Controller, however this should not be considered a hard and fast rule for the very reasons that you have stated.

I quote Martin Fowler from Patterns of Enterprise Application Architecture:

As I said, the value of MVC lies in its two separations. Of these are the separation of presentation and model is one of the most important design principles in software, and the only time you shouldn't follow it is in very simple systems where the model has no real behavior in it anyway. As soon as you get some nonvisual logic you should apply the separation. Unfortunately, a lot of UI frameworks make it difficult, and those that don't are often taught without a separation.

The separation of view and controller is less important, so I'd only recommend doing it when it is really helpful. For rich-client systems, that ends up being hardly ever, although it is common in Web front ends where the controller is separated out. Most of the patterns on Web design here are based on that principle.

Essentially Fowler states that binding view to controller is not as important as binding model to view, so utilizing controllers across views or multiple controllers per view is certainly acceptable if it is helpful to do so.

于 2012-08-28T11:31:51.690 回答