不喜欢视图应该是模型在其数据更改时通知的视图。我会将该功能委托给控制器。在这种情况下,如果更改应用程序逻辑,则无需干扰视图的代码。视图的任务仅用于应用程序组件+布局仅此而已。摆动布局已经是一项繁琐的任务,为什么要让它干扰应用程序逻辑呢?
我对MVC的想法(我目前正在使用,到目前为止一切都很好)是:
- 景色是三者中最愚蠢的。它对控制器和模型一无所知。它关注的只是摆动部件的工艺和布局。
- 模型也很愚蠢,但不像视图那么愚蠢。它执行以下功能。
- 一个非常智能的控制器。非常了解视图和模型。控制器有两个功能:
- 一个。它定义视图在用户与其交互时将执行的操作。
- b.它侦听模型。就像我说的,当调用模型的设置者时,模型将向控制器发出通知。控制器的工作是解释此通知。它可能需要反映对视图的更改。
代码示例
景观 :
就像我说的,创建视图已经很详细了,所以只需创建自己的实现:)
interface View{
JTextField getTxtFirstName();
JTextField getTxtLastName();
JTextField getTxtAddress();
}
出于可测试性目的,将这三者连接起来是理想的。我只提供了模型和控制器的实现。
型号 :
public class MyImplementationOfModel implements Model{
...
private SwingPropertyChangeSupport propChangeFirer;
private String address;
private String firstName;
private String lastName;
public MyImplementationOfModel() {
propChangeFirer = new SwingPropertyChangeSupport(this);
}
public void addListener(PropertyChangeListener prop) {
propChangeFirer.addPropertyChangeListener(prop);
}
public void setAddress(String address){
String oldVal = this.address;
this.address = address;
//after executing this, the controller will be notified that the new address has been set. Its then the controller's
//task to decide what to do when the address in the model has changed. Ideally, the controller will update the view about this
propChangeFirer.firePropertyChange("address", oldVal, address);
}
...
//some other setters for other properties & code for database interaction
...
}
控制器 :
public class MyImplementationOfController implements PropertyChangeListener, Controller{
private View view;
private Model model;
public MyImplementationOfController(View view, Model model){
this.view = view;
this.model = model;
//register the controller as the listener of the model
this.model.addListener(this);
setUpViewEvents();
}
//code for setting the actions to be performed when the user interacts to the view.
private void setUpViewEvents(){
view.getBtnClear().setAction(new AbstractAction("Clear") {
@Override
public void actionPerformed(ActionEvent arg0) {
model.setFirstName("");
model.setLastName("");
model.setAddress("");
}
});
view.getBtnSave().setAction(new AbstractAction("Save") {
@Override
public void actionPerformed(ActionEvent arg0) {
...
//validate etc.
...
model.setFirstName(view.getTxtFName().getText());
model.setLastName(view.getTxtLName().getText());
model.setAddress(view.getTxtAddress().getText());
model.save();
}
});
}
public void propertyChange(PropertyChangeEvent evt){
String propName = evt.getPropertyName();
Object newVal = evt.getNewValue();
if("address".equalsIgnoreCase(propName)){
view.getTxtAddress().setText((String)newVal);
}
//else if property (name) that fired the change event is first name property
//else if property (name) that fired the change event is last name property
}
}
设置 MVC 的主 :
public class Main{
public static void main(String[] args){
View view = new YourImplementationOfView();
Model model = new MyImplementationOfModel();
...
//create jframe
//frame.add(view.getUI());
...
//make sure the view and model is fully initialized before letting the controller control them.
Controller controller = new MyImplementationOfController(view, model);
...
//frame.setVisible(true);
...
}
}