我们用AsWing 开发应用程序的时候,可以考虑用 MVC,下面我来帖一个我自己平时用的 MVC 的写法。
如果还一直对 “逻辑代码与界面代码混淆” 感到困扰的话,可以参考一下这样的写法。
大概是这样的。
有一个 View 类,这个类负责建立界面,然后界面上需要活动的组件通过 getXXX 方式向外部提供。
View 当中没有逻辑代码,对外公开的只有对需要活动的组件的访问,至于这些组件是被放置在什么位置,多大等等界面细节外部是看不到的。另一方面,界面的布局发生变化不会影响到 View 以外的类。
有一个 Model 类,这个类就是数据,同时也是一个 EventDispatcher, 当数据发生变化的时候会对外发布事件。
有一个 Controller 类,这个类有两个通道,一个通道监听界面的事件,并且把相应的数据写入 Model。另一个通道负责监听Model 的事件,把更新显示到 View 上面。
主程序是这样的:
var view:View = new View(this);
var model:Model = new Model();
var controller:Controller = new Controller();
controller.setModel(model);
controller.setView(view);
controller.start();
View 类的代码是这样的:
package example {
import org.aswing.JLabel;
import org.aswing.JButton;
import org.aswing.JTextField;
import org.aswing.JFrame;
import flash.display.DisplayObjectContainer;
import org.aswing.SoftBoxLayout;
import org.aswing.JPanel;
import org.aswing.GridLayout;
import org.aswing.border.LineBorder;
import org.aswing.Container;
import org.aswing.ASColor;
import org.aswing.border.TitledBorder;
import org.aswing.BorderLayout;
import org.aswing.FlowLayout;
public class View {
private var label:JLabel;
private var textField:JTextField;
private var btn:JButton;
public function View(owner:DisplayObjectContainer) {
this.build(owner);
}
protected function build(owner:DisplayObjectContainer) :void {
this.label = new JLabel(”No Data”);
this.textField = new JTextField();
this.btn = new JButton(”向 Model 提交”);
var frame:JFrame = new JFrame(owner, “显示界面”, true);
frame.setSizeWH(300, 200);
frame.setLocationXY(18, 18);
frame.setBackground(ASColor.WHITE);
frame.setVisible(true);
var c:Container = frame.getContentPane();
c.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 15, SoftBoxLayout.CENTER));
var panel0:JPanel = new JPanel(new BorderLayout());
panel0.setBorder(new TitledBorder(null, “这里显示从 Model 获取的内容”, 1.0, 0.0, 0.0, 3.0));
panel0.append(this.label);
c.append(panel0);
var panel1:JPanel = new JPanel(new BorderLayout());
panel1.setBorder(new TitledBorder(null, “输入您要提交给 Model 的内容”, 1.0, 0.0, 0.0, 3.0));
panel1.append(this.textField);
c.append(panel1);
var panel2:JPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel2.append(this.btn);
c.append(panel2);
}
public function getLabel() :JLabel {
return this.label;
}
public function getTextField() :JTextField {
return this.textField;
}
public function getButton() :JButton {
return this.btn;
}
}
}
Model 类的代码是这样的:
package example {
import flash.events.EventDispatcher;
import flash.events.Event;
public class Model extends EventDispatcher {
private var data:String;
public function setData(src:String) :void {
if (src != data) {
this.data = src;
var event:Event = new Event(Event.CHANGE);
this.dispatchEvent(event);
} else {
//pass
}
}
public function getData() :String {
return this.data;
}
}
}
Controller 类的代码是这样的:
package example {
import flash.events.Event;
import flash.events.MouseEvent;
import org.aswing.event.AWEvent;
public class Controller {
private var model:Model;
private var view:View;
public function Controller(model:Model=null, view:View=null) {
this.setModel(model);
this.setView(view);
}
public function setModel(model:Model) :void {
this.model = model;
}
public function setView(view:View) :void {
this.view = view;
}
//start controling
public function start() :void {
var inController:InputController = new InputController(this.view, this.model);
var outControler:OutputController = new OutputController(this.view, this.model);
}
}
}
import flash.events.Event;
import example.View;
import example.Model;
import org.aswing.JLabel;
class InputController {
private var view:View;
private var model:Model;
public function InputController(view:View, model:Model) {
this.view = view;
this.model = model;
this.view.getButton().addActionListener(this.actionPerformed);
}
public function actionPerformed(e:Event) :void {
var src:String = this.view.getTextField().getText();
this.model.setData(src);
}
}
class OutputController {
private var view:View;
private var model:Model;
public function OutputController(view:View, model:Model) {
this.view = view;
this.model = model;
this.model.addEventListener(Event.CHANGE, this.actionPerformed);
}
public function actionPerformed(e:Event) :void {
var data:String = this.model.getData();
this.view.getLabel().setText(data);
}
}