Reputation: 2652
Hello I have read an example that I found on my school database about MVC in Java. there is this example where the buttons go into the Controller part. but isn't it correct that the buttons need to be in the VIEW because everything that the user can see must be in the view or am I understanding it wrong and is it different in Java?
Upvotes: 4
Views: 1914
Reputation: 4684
My advice to you - have a look at MVC implementation for Java by PureMVC Project. You'll find that it's an amazing thing! The infrastructure is implemented according to MVC architecture described here.
Upvotes: 0
Reputation: 547
You are correct in saying Buttons belong to the View. However, there may be specific cases where you have to define buttons in the controller. For example, in case of dynamic UI, buttons may need to be instantiated in the controller based on a condition.
One main reason for the View/Controller separation, is to make the view "dumb", and be a UI representation that doesn't require testing. UI components are often heavy-weight, and must be abstracted out for testing. This is done by replacing it with light-weight mocks that mirror the original components' interface, and presenting those to the tests for the controller.
If conditions or business logic are involved, the code should be tested for the different scenarios, and hence belongs in the controller.
Upvotes: 2
Reputation: 106470
This image is a generalization of MVC from the Model-View-Controller page on Wikipedia:
In general, if you're dealing with drawing/rendering UI elements, that belongs in the view. Actions/events that let the user interact with the button would belong in the controller (and may have functionality delegated to services as necessary).
You don't want to put any code that draws/renders buttons in the controller - that tightly couples the controller to a specific view, which goes against the principle of using this design pattern.
Upvotes: 4