问题
I wonder me, how I should build a bigger program with the MVC pattern? In the web I saw usually one model, controller and view.
For example, if I want to make a vocable trainer. In the vocable trainer I can add new vocables and make a query. Of course I want to make a menu where I can switch between this two modes. Now I don't know how I should design this program with the MVC pattern. Does someone know a good tutorial or can explain it to me. Maybe someone know a small clean example program or something other... And should have the main menu a model, too?
And a question to the MVC pattern itself. The model only contains only data, the view only defines how the gui looks like and the controller show in the model for data and set with that knowledge the states of the view, for example add an combo box item. Is that correct?
And if someone want to know, I use Java.
I think you see that I am really confused.
回答1:
Usually you create a "Core" project and you separate it from your web project.
"Core project" will have : Business objects, such as entities and Data access
"Web project" will have:
- Controllers: which route requests from the UI to the core logic
- Views: which focus on presenting data in HTML
- Models: which transform and makes simple core business objects into simpler structures designed to support specific views
Use google to find good tutorial about MVC , there are many resources but I found one for you: http://leepoint.net/notes-java/GUI/structure/40mvc.html
Also one more hint to help you on your google search, there are many frameworks out there make sure, if you are following a tutorial(with a framework) then you need to download all the APIs/jar files to be able to get your MVC up and running. (you may already know this but I thought I should share just in case if you are new to java and it's frameworks).
回答2:
I apply this method when I’m creating both small and big, complete, Java programs. This method can scale easily if you think about how you do things and could be a good start:
default
- Main.java - just main method to start up things
[GUI] package (view)
- GUI.java - Head of the gui, some gui
parts are built here and some are built separately
- PanelFoo.java - one part of the gui
- PanelBar.java - another part of the gui
[Control] package(control)
- Monitor.java - used for concurrency synchronizing tasks, mostly method that are called by the GUI package ActionListeners
- SomeTask.java implements Runnable - Maybe a downloading thread
- SomeLogicHelper.java - some bigger methods with some logic
[Model] package (model), basic modelling
- Vehicleish.java
- Carish.java
- Bicycleish.java
- Garageish .java
[GUI] is connected to [Control] by its ActionListeners calling methods in the Monitor. [GUI] provides a callback interface for the Monitor to call for changes that are not done directly within the [GUI].
[Control] is connected to [Model], it uses Models of things to make things easy, it can coordinate the logic and "do things" to the Objects from the [Model]
回答3:
Use of MVC in GUI Client Apps is very different from the Java Web apps on servers. In Java Web Apps the request first hits the controller and then Model and then View to render it back to the User.
In GUI Client Apps, Actually View holds the Controller. When an event happens, That is the replica of the request and hence it hits controller and then Model and Data Access objects.
Though this is one way of implementing MVC. There could be other interpretation and implementations too. MVC for GUI client Apps is not as clearly defined as it is defined for Server Web Apps.
来源:https://stackoverflow.com/questions/21100125/mvc-pattern-in-complex-gui-programs