The MVC pattern and SWING

2019-01-01 03:18发布

One of the design patterns which I find most difficult to get a real grasp of in "real SWING life" is the MVC pattern. I've been through quite a few of the posts at this site which discuss the pattern, but I still do not feel that i have a clear understanding of how to take advantage of the pattern in my (Java SWING) application.

Let's say that I have a JFrame which contains a table, a couple of text fields and a few buttons. I would probably use a TableModel to "bridge" the JTable with an underlying data model. However, all functions responsible for clearing fields, validating fields, locking fields along with button actions would usually go directly in the JFrame. However, doesn't that mix the Controller and View of the pattern?

As far as I can see, I manage to get the MVC pattern "correctly" implemented when looking at the JTable (and the model), but things get muddy when I look at the entire JFrame as a whole.

I'd really like to hear how others go about with regard to this. How do you go about when you need to display a table, a couple of fields and some buttons to a user (using the MVC pattern)?

7条回答
怪性笑人.
2楼-- · 2019-01-01 03:47

If you develop a program with a GUI, mvc pattern is almost there but blurred.

Disecting model, view and controller code is difficult, and normally is not only a refactor task.

You know you have it when your code is reusable. If you have correctly implemented MVC, should be easy to implement a TUI or a CLI or a RWD or a mobile first design with the same functionality. It's easy to see it done than do it actually, moreover on an existing code.

In fact, interactions between model, view and controller happens using other isolation patterns (as Observer or Listener)

I guess this post explains it in detail, from the direct non MVC pattern (as you will do on a Q&D) to the final reusable implementation:

http://www.austintek.com/mvc/

查看更多
登录 后发表回答