MVC with swing implementation java [closed]

2019-09-19 22:55发布

I am confused how MVC will work with GUI swing application. I have worked with PHP MVC but that is totally different. I understand what MVC stands for. But what making me confused is different variation of doing it in GUI swing programming. It is hard to conclude particular thing from different articles in web. Who should know whom? I mean what will be the relation between model view and controller? Should controller know both model and the view? I would like to an simple example if possible to illustrate this (like and simple button which will update a label)

If i am not asking more i would like to get sugetions of MVC book which is writtern Swing in mind.

2条回答
一夜七次
2楼-- · 2019-09-19 23:02

There are many different interpretations of MVC for Java. I will try to provide a basic explanation, but as I said, others may disagree on this.

A theoretically 'pure' interpretation of MVC involves the following:

  • Model does not know about view
  • View does not know about model
  • Controller connects model and view in such a way that it provides data from the model to the view, handles 'events' in the view or the model, and updates the model accordingly based on what is happening in the view. It can also just handle an event in the view and provide a result back to the view, for example, a calculator.

Here is a possible/simple example:

The goal of this hypothetical application is to take a String in the model, get it to the GUI (view), allow the user to change the string, and update the aforementioned String value in the model. This example is more or less as decoupled as possible.

Model:

  • Contains a String variable

View:

  • Displays String variable.
  • Takes user input that allows change to the String.

Controller (the 'glue'):

  • Listens to Model via a customized listener for the String.
  • Provides this String to the view for the user to see
  • Listens to the view via a customized listener for the user to change the String.
  • Takes this new String and provides it to the Model, updating the original String.

One of the key things behind MVC is the Observer Pattern Theory.

Although one takes certain risks using wikipedia, it generally does a good job conveying the basics behind MVC. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Here is the link for a discussion on the 'pure' implementation and the source of the interpretation. http://www.youtube.com/watch?v=ACOHAR7PIp4

Here is a link with a very good explanation of a similar MVC interpretation and the theory behind it: http://www.youtube.com/watch?v=CVxt79kK3Mk

查看更多
欢心
3楼-- · 2019-09-19 23:25

If you ask 10 different people "What does MVC mean?" you'll probably get 10 different answers. I'm personally partial to this definition of MVC (at least for non-web apps):

Model-View-Controller Design Pattern

Basically, the only functions the controller serves is to instantiate model and the view as the application starts up and connect them to one another. Everything else is just proper decoupling of your program's data and logic (model) from how you choose to display it to the user and allow user interaction (view).

查看更多
登录 后发表回答