來討論一下Swing中的MVC

Model/View/Controller Architecture 
 
There are a number of ways to approach using Swing to develop GUIs.  
As shown in the first part of this course, you can use most of the  
Swing widgets in the same way AWT widgets are used. If you take this  
approach, the transition from AWT programming is very easy.  
 
However, Swing gives the programmer a more powerful interface to the  
widgets. Employing a technology called the Model/View/Controller (MVC) 
architecture, the Swing team has given you the ability to control how  
widgets look, how they respond to input, and, for some more complex  
widgets, how data is represented.  
 
... 
 
Aside from a richer collection of widgets, the primary advantage of the  
Swing Component set over AWT is its use of MVC. MVC is a design pattern  
often used in building user interfaces (UI). In an MVC UI, there are  
three communicating objects, the model, view, and controller. The model  
is the underlying logical representation, the view is the visual  
representation, and the controller specifies how to handle user input.  
When a model changes, it notifies all views that depend on it. This  
separation of state and presentation allows for two very powerful features.  
 
1 You can base multiple views on the same model. For instance, you can  
  present a set of data in both table form and chart form. As you update  
  the data model, the model notifies both views and gives each an  
  opportunity to update itself.  
 
2 Because models specify nothing about presentation, you can modify or  
  create views without affecting the underlying model.  
 
A view uses a controller to specify its response mechanism. For instance,  
the controller determines what action to take when receiving keyboard input.  
 
Although the primary purpose of MVC is for building UIs, it can be used  
to establish an analogous notification protocol between non-visual objects.  
A model object can send change notifications to an arbitrary set of interested  
objects without knowing details about those objects. The Observer/Observable  
objects in java.util have served this need well, since Java 1.0.  
 
... 
 
我們常用的JTable JTree是非常典型的MVC構架。下圖所示的內容是兩個常用 
的MVC構架: 
 
       View              Controller               Model 
---------------------------------------------------------------------- 
     JTable              TableModelListener       DefaultTableModel 
     JTree               TreeModelListener        DefaultTreeModel 
 
從JDK1.1開始,事件接收和處理的方式發生了重大變化。JDK1.0採用的是層次 
事件模型:事件先發送到組件,然後沿容器層次向上傳播,直到被處理爲止。 
JDK1.1以後採用的是委託事件模型:事件是隻向註冊的Listener報告的對象, 
從沒有註冊的Listener的組件中發出的事件不會被廣播。 
 
正是因爲採用了委託事件模型,才使得MVC結構得以實現。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章