Model-View-Controller, MVC模式的目的是将自己的用户界面逻辑从业务逻辑中分离。通过这种实现,他可以重用业务逻辑,并且阻止界面中的修改对业务逻辑造成影响。
下面这个实例正好体现了这一点。WorkPanel中的CommandButton被控制器LoginAction监听,控制器控制model工作,Model工作得到结果,同时JWorkPanel监听Model的工作,在试图上做出相应的反应。
下面这个实例正好体现了这一点。WorkPanel中的CommandButton被控制器LoginAction监听,控制器控制model工作,Model工作得到结果,同时JWorkPanel监听Model的工作,在试图上做出相应的反应。
package
oyjpart.designpattern;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Model {
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this);
private String login;
private boolean loginStatus;
private boolean password;
public Model() {
loginStatus = false;
}
public Model(PropertyChangeSupport changeSupport, String login, boolean loginStatus, boolean password) {
super();
this.changeSupport = changeSupport;
this.login = login;
this.loginStatus = loginStatus;
this.password = password;
}
void login(String login, String password) {
if(isLoginStatus()) {
setLoginStatus(false);
} else {
setLoginStatus(true);
}
}
/**//* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((login == null) ? 0 : login.hashCode());
result = PRIME * result + (loginStatus ? 1231 : 1237);
result = PRIME * result + (password ? 1231 : 1237);
return result;
}
/**//* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
final Model other = (Model) obj;
if (login == null) {
if (other.login != null)
return false;
} else if (!login.equals(other.login))
return false;
if (loginStatus != other.loginStatus)
return false;
if (password != other.password)
return false;
return true;
}
/** *//**
* @return the loginStatus
*/
public boolean isLoginStatus() {
return loginStatus;
}
/** *//**
* @return the password
*/
public boolean isPassword() {
return password;
}
/** *//**
* @return the login
*/
public String getLogin() {
return login;
}
/** *//**
* @param login the login to set
*/
public void setLogin(String login) {
this.login = login;
}
/** *//**
* @param loginStatus
* the loginStatus to set
*/
public void setLoginStatus(boolean loginStatus) {
boolean old = this.loginStatus;
this.loginStatus = loginStatus;
changeSupport.firePropertyChange("model.loginStatus", old, loginStatus);
}
/** *//**
* @param password
* the password to set
*/
public void setPassword(boolean password) {
this.password = password;
}
/** *//**
* @return the changeSupport
*/
public PropertyChangeSupport getChangeSupport() {
return changeSupport;
}
/** *//**
* @param changeSupport the changeSupport to set
*/
public void setChangeSupport(PropertyChangeSupport changeSupport) {
this.changeSupport = changeSupport;
}
/** *//**
* @param arg0
* @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
}
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Model {
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this);
private String login;
private boolean loginStatus;
private boolean password;
public Model() {
loginStatus = false;
}
public Model(PropertyChangeSupport changeSupport, String login, boolean loginStatus, boolean password) {
super();
this.changeSupport = changeSupport;
this.login = login;
this.loginStatus = loginStatus;
this.password = password;
}
void login(String login, String password) {
if(isLoginStatus()) {
setLoginStatus(false);
} else {
setLoginStatus(true);
}
}
/**//* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((login == null) ? 0 : login.hashCode());
result = PRIME * result + (loginStatus ? 1231 : 1237);
result = PRIME * result + (password ? 1231 : 1237);
return result;
}
/**//* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
final Model other = (Model) obj;
if (login == null) {
if (other.login != null)
return false;
} else if (!login.equals(other.login))
return false;
if (loginStatus != other.loginStatus)
return false;
if (password != other.password)
return false;
return true;
}
/** *//**
* @return the loginStatus
*/
public boolean isLoginStatus() {
return loginStatus;
}
/** *//**
* @return the password
*/
public boolean isPassword() {
return password;
}
/** *//**
* @return the login
*/
public String getLogin() {
return login;
}
/** *//**
* @param login the login to set
*/
public void setLogin(String login) {
this.login = login;
}
/** *//**
* @param loginStatus
* the loginStatus to set
*/
public void setLoginStatus(boolean loginStatus) {
boolean old = this.loginStatus;
this.loginStatus = loginStatus;
changeSupport.firePropertyChange("model.loginStatus", old, loginStatus);
}
/** *//**
* @param password
* the password to set
*/
public void setPassword(boolean password) {
this.password = password;
}
/** *//**
* @return the changeSupport
*/
public PropertyChangeSupport getChangeSupport() {
return changeSupport;
}
/** *//**
* @param changeSupport the changeSupport to set
*/
public void setChangeSupport(PropertyChangeSupport changeSupport) {
this.changeSupport = changeSupport;
}
/** *//**
* @param arg0
* @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
}
package
oyjpart.designpattern;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class WorkPanel extends JPanel implements PropertyChangeListener {
private JPanel center;
private JPanel buttonPanel = new JPanel();
private JLabel loginStatusLabel = new JLabel();
public WorkPanel(JPanel center) {
super();
this.center = center;
init();
}
private void init() {
setLayout(new BorderLayout());
add(center, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
add(loginStatusLabel, BorderLayout.NORTH);
}
public void propertyChange(PropertyChangeEvent e) {
if(e.getPropertyName().equals("model.loginStatus")) {
Boolean status = (Boolean) e.getNewValue();
if(status.booleanValue()) {
loginStatusLabel.setText("Login was successful");
} else {
loginStatusLabel.setText("Login Failed");
}
}
}
public void addButton(String name, Action action) {
JButton button = new JButton();
button.setText(name);
button.addActionListener(action);
buttonPanel.add(button);
}
}
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class WorkPanel extends JPanel implements PropertyChangeListener {
private JPanel center;
private JPanel buttonPanel = new JPanel();
private JLabel loginStatusLabel = new JLabel();
public WorkPanel(JPanel center) {
super();
this.center = center;
init();
}
private void init() {
setLayout(new BorderLayout());
add(center, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
add(loginStatusLabel, BorderLayout.NORTH);
}
public void propertyChange(PropertyChangeEvent e) {
if(e.getPropertyName().equals("model.loginStatus")) {
Boolean status = (Boolean) e.getNewValue();
if(status.booleanValue()) {
loginStatusLabel.setText("Login was successful");
} else {
loginStatusLabel.setText("Login Failed");
}
}
}
public void addButton(String name, Action action) {
JButton button = new JButton();
button.setText(name);
button.addActionListener(action);
buttonPanel.add(button);
}
}
package
oyjpart.designpattern;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class LoginAction extends AbstractAction {
private Model model;
private CenterPanel panel;
public LoginAction(Model model, CenterPanel panel) {
this.model = model;
this.panel = panel;
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Login Action : " + panel.getLogin() + " "
+ panel.getPassword());
model.login(panel.getLogin(), panel.getPassword());
}
}
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
public class LoginAction extends AbstractAction {
private Model model;
private CenterPanel panel;
public LoginAction(Model model, CenterPanel panel) {
this.model = model;
this.panel = panel;
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Login Action : " + panel.getLogin() + " "
+ panel.getPassword());
model.login(panel.getLogin(), panel.getPassword());
}
}
package
oyjpart.designpattern;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Application extends JFrame {
private Model model;
/** *//**
* @param model
* @throws HeadlessException
*/
public Application(Model model) throws HeadlessException {
super();
this.model = model;
CenterPanel center = new CenterPanel();
WorkPanel work = new WorkPanel(center);
work.addButton("login", new LoginAction(model, center));
work.addButton("exit", new ExitAction());
model.addPropertyChangeListener(work);
setTitle("MVC Pattern Application");
getContentPane().add(work);
pack();
show();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Model model = new Model();
Application app = new Application(model);
}
}
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Application extends JFrame {
private Model model;
/** *//**
* @param model
* @throws HeadlessException
*/
public Application(Model model) throws HeadlessException {
super();
this.model = model;
CenterPanel center = new CenterPanel();
WorkPanel work = new WorkPanel(center);
work.addButton("login", new LoginAction(model, center));
work.addButton("exit", new ExitAction());
model.addPropertyChangeListener(work);
setTitle("MVC Pattern Application");
getContentPane().add(work);
pack();
show();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
Model model = new Model();
Application app = new Application(model);
}
}
package
oyjpart.designpattern;
import java.awt.GridLayout;
import javax.swing. * ;
public class CenterPanel extends JPanel {
private JTextField login = new JTextField();
private JTextField password = new JTextField();
public CenterPanel() {
setLayout(new GridLayout(2, 2));
add(new JLabel("login:"));
add(login);
add(new JLabel("password:"));
add(password);
}
public String getLogin() {
return login.getText();
}
public String getPassword() {
return password.getText();
}
}
import java.awt.GridLayout;
import javax.swing. * ;
public class CenterPanel extends JPanel {
private JTextField login = new JTextField();
private JTextField password = new JTextField();
public CenterPanel() {
setLayout(new GridLayout(2, 2));
add(new JLabel("login:"));
add(login);
add(new JLabel("password:"));
add(password);
}
public String getLogin() {
return login.getText();
}
public String getPassword() {
return password.getText();
}
}
package
oyjpart.designpattern;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ExitAction extends AbstractAction {
public void actionPerformed(ActionEvent arg0) {
JFrame frame = new JFrame();
int response = JOptionPane.showConfirmDialog(frame,
"Exit Application ?", "Exit", JOptionPane.OK_CANCEL_OPTION);
if(JOptionPane.YES_OPTION == response) {
System.exit(0);
}
}
}
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ExitAction extends AbstractAction {
public void actionPerformed(ActionEvent arg0) {
JFrame frame = new JFrame();
int response = JOptionPane.showConfirmDialog(frame,
"Exit Application ?", "Exit", JOptionPane.OK_CANCEL_OPTION);
if(JOptionPane.YES_OPTION == response) {
System.exit(0);
}
}
}