一般来说,可以把系统粗略的分为三个层次,视图层,简称为View,它负责数据的输出和输入;业务层,简称为Model,它代表程序的实际业务;控制层,简称为Controller,处理界面的相应并调用业务层进行处理,有时把View和Controller两层合称为UI层。
在程序发展的历史上,MVC模式进过了多次演化,MVC1和MVC2是两种比较典型的模式,它们的区别主要在于View和Model的联系方式上。
(1
) MVC1
模式
这种模式主要用于桌面程序,使用观察者模式实现,具体来说就是让View充当观察者来观察Model的变化,而用户交互控制的地方用匿名类的方式统一放在controller中.
View接受用户输入,并将交互数据传递到Controller.
Controller统一进行处理命令,交由Model处理具体的业务.
进过处理Model更新后,通知View进行更新.
这种模式在旧的桌面应用程序使用较多,但是它的Model必须继承Observable类,View必须实现Observer接口,人为加大了继承体系的复杂度;而且Model通知View的方式使View必须了解Model的结构,无谓的加大了两个类的耦合程度。这些缺点使这种模式逐渐淡出了程序舞台。
代码如下:
Control类:
package
com.sitinspring;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.event.WindowAdapter;
import
java.awt.event.WindowEvent;
/** */
/**
* 控制类
*
*
@author
sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public
class
Mvc1Ctrl
{
private
Mvc1View view;
private
Mvc1Model model;
public
Mvc1Ctrl()
{
view
=
new
Mvc1View();
model
=
new
Mvc1Model();
model.addObserver(view);
handleEvents();
}
//
处理事件响应
private
void
handleEvents()
{
addCloseLintener();
addButtonListener();
addButtonListener2();
}
//
窗体关闭事件相应
private
void
addCloseLintener()
{
view.addWindowListener(
new
WindowAdapter()
{
public
void
windowClosing(WindowEvent e)
{
System.out.println(
"
Exit MVC1
"
);
System.exit(
0
);
}
}
);
}
private
void
addButtonListener()
{
view.getButton().addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent e)
{
model.showText();
}
}
);
}
private
void
addButtonListener2()
{
view.getButton2().addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent e)
{
model.showText2();
}
}
);
}
}
视图类(View):
package
com.sitinspring;
import
java.awt.Dimension;
import
java.awt.GridLayout;
import
java.awt.Toolkit;
import
java.util.Observable;
import
java.util.Observer;
import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
/** */
/**
* 视图类(View)
*
*
@author
sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public
class
Mvc1View
extends
JFrame
implements
Observer
{
private
static
final
long
serialVersionUID
=
621145935910133202L
;
private
JButton button;
private
JLabel label;
private
JButton button2;
private
JLabel label2;
public
Mvc1View()
{
locateView(
300
,
200
);
this
.setTitle(
"
MVC1 Program
"
);
setupComponents();
this
.setVisible(
true
);
}
//
当模块更新时,此函数会被调用
public
void
update(Observable o, Object arg)
{
Mvc1Model model
=
(Mvc1Model) o;
if
(model.getUpdateState().equals(Mvc1UpdateState.UpdateLabel))
{
label.setText(model.getResponseText());
}
else
if
(model.getUpdateState().equals(Mvc1UpdateState.UpdateLabel2))
{
label2.setText(model.getResponseText());
}
}
//
定位程序在屏幕正中并设置程序大小
private
void
locateView(
int
width,
int
height)
{
Dimension screenSize
=
Toolkit.getDefaultToolkit().getScreenSize();
this
.setSize(width, height);
this
.setLocation(screenSize.width
/
2
-
width
/
2
, screenSize.height
/
2
-
height
/
2
);
}
//
初始化内部组件
private
void
setupComponents()
{
button
=
new
JButton(
"
点击响应事件1
"
);
label
=
new
JLabel(
"
等待事件响应1
"
);
button2
=
new
JButton(
"
点击响应事件2
"
);
label2
=
new
JLabel(
"
等待事件响应2
"
);
setLayout(
new
GridLayout(
2
,
2
));
add(button);
add(label);
add(button2);
add(label2);
}
public
JButton getButton()
{
return
button;
}
public
JButton getButton2()
{
return
button2;
}
}
模块类:
package
com.sitinspring;
import
java.util.Observable;
/** */
/**
* 模块类
*
@author
sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public
class
Mvc1Model
extends
Observable
{
private
String responseText;
private
String updateState;
//
用于通知View更新,此函数被调用后View的update函数会被调用
private
void
notifyView()
{
setChanged();
notifyObservers();
}
public
void
showText()
{
updateState
=
Mvc1UpdateState.UpdateLabel;
responseText
=
"
事件1响应完毕
"
;
notifyView();
}
public
void
showText2()
{
updateState
=
Mvc1UpdateState.UpdateLabel2;
responseText
=
"
事件2响应完毕
"
;
notifyView();
}
public
String getResponseText()
{
return
responseText;
}
public
String getUpdateState()
{
return
updateState;
}
}
更新状态辅助类:
package
com.sitinspring;
public
class
Mvc1UpdateState
{
public
static
final
String UpdateLabel
=
"
updateLabel
"
;
public
static
final
String UpdateLabel2
=
"
updateLabel2
"
;
}
MVC1示例代码下载:
http://www.blogjava.net/Files/sitinspring/MVC1.rar
(2)MVC2模式
这种模式首见于网络程序,起初因为Model无法通知到Web程序的界面而发明,这种模式采用Controller做中介者,一方面取得View的输入,然后交由Model层处理,之后再把返回的数据传递到View。
View接受用户输入,并传递到Controller.
Controller统一进行处理命令,交由Model处理具体的业务.
进过处理Model更新后,Controller会选一个View并把Model内容传递(request,session)给它(forward).
然后View进行显示.
这种模式相对
MVC
优势很明显,首先
Model
和
View
无需继承甚么东西,其次
Model
和
View
无需了解对方的存在,只需准备相应的接口而已;缺点是
Controller
层变得相对复杂了。
代码如下:
控制类:
package
com.sitinspring;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.event.WindowAdapter;
import
java.awt.event.WindowEvent;
/** */
/**
* 控制类
*
*
@author
sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public
class
Mvc2Ctrl
{
private
Mvc2View view;
private
Mvc2Model model;
public
Mvc2Ctrl()
{
view
=
new
Mvc2View();
model
=
new
Mvc2Model();
handleEvents();
}
//
处理事件响应
private
void
handleEvents()
{
addCloseLintener();
addButtonListener();
addButtonListener2();
}
//
窗体关闭事件相应
private
void
addCloseLintener()
{
view.addWindowListener(
new
WindowAdapter()
{
public
void
windowClosing(WindowEvent e)
{
System.out.println(
"
Exit MVC2
"
);
System.exit(
0
);
}
}
);
}
private
void
addButtonListener()
{
view.getButton().addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent e)
{
view.getLabel().setText(model.getText());
}
}
);
}
private
void
addButtonListener2()
{
view.getButton2().addActionListener(
new
ActionListener()
{
public
void
actionPerformed(ActionEvent e)
{
view.getLabel2().setText(model.getText2());
}
}
);
}
}
视图类:
package com.sitinspring;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/** *//**
* 视图类(View)
*
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc2View extends JFrame {
private static final long serialVersionUID = 621145935910133202L;
private JButton button;
private JLabel label;
private JButton button2;
private JLabel label2;
public Mvc2View() {
locateView(300, 200);
this.setTitle("MVC2 Program");
setupComponents();
this.setVisible(true);
}
// 定位程序在屏幕正中并设置程序大小
private void locateView(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(width, height);
this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
/ 2 - height / 2);
}
// 初始化内部组件
private void setupComponents() {
button = new JButton("点击响应事件1");
label = new JLabel(" 等待事件响应1");
button2 = new JButton("点击响应事件2");
label2 = new JLabel(" 等待事件响应2");
setLayout(new GridLayout(2, 2));
add(button);
add(label);
add(button2);
add(label2);
}
public JButton getButton() {
return button;
}
public JButton getButton2() {
return button2;
}
public JLabel getLabel() {
return label;
}
public JLabel getLabel2() {
return label2;
}
} 模块类:
package com.sitinspring;
/** *//**
* 模块类
* @author sitinspring(junglesong@gmail.com)
*
* @date 2007-11-5
*/
public class Mvc2Model{
public String getText(){
return " 事件1响应完毕";
}
public String getText2(){
return " 事件2响应完毕";
}
} MVC2示例代码下载:
http://www.blogjava.net/Files/sitinspring/MVC2.rar
|