这是 Actionscript 3 中 MVC 的正确实现吗?
该程序所做的就是您单击一个按钮,它会告诉您在文本字段中单击该按钮的次数。
文档类:这是代码的入口点。
package {
import flash.display.MovieClip;
/**
* MVCTest.as
* @author rotaercz
*/
public class MVCTest extends MovieClip {
private var _model:Model;
private var _view:View;
private var _control:Control;
public function MVCTest() {
_model = new Model();
_view = new View(this);
_control = new Control(_model, _view);
}
}
}
模型类:基本模型代码。
package {
/**
* Model.as
* @author rotaercz
*/
public class Model {
private var _totalClicks:int;
public function AddClick():void {
_totalClicks++;
}
public function get Clicks():int {
return _totalClicks;
}
public function Model() {
_totalClicks = 0;
}
}
}
控制类:控制此处模型和视图的输入和更新。
package {
import flash.events.MouseEvent;
/**
* Control.as
* @author rotaercz
*/
public class Control {
private var _model:Model;
private var _view:View;
public function Control(model:Model, view:View):void {
_model = model;
_view = view;
_view.addEventListener(MouseEvent.CLICK, OnClick);
}
private function OnClick(e:MouseEvent):void {
_model.AddClick();
_view.Text = "clicked " + _model.Clicks;
}
}
}
视图类:程序的可视化表示。
package {
import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.text.TextField;
/**
* View.as
* @author rotaercz
*/
public class View extends EventDispatcher {
private var _parent:MovieClip;
private var _button:MovieClip;
private var _dt:TextField;
public function set Text(s:String):void {
_dt.text = s;
}
public function View(parent:MovieClip) {
_parent = parent;
_dt = _parent.getChildByName("dt") as TextField;
_button = _parent.getChildByName("mcButton") as MovieClip;
_button.addEventListener(MouseEvent.CLICK, OnClick);
}
private function OnClick(e:MouseEvent):void {
dispatchEvent(e);
}
}
}
All the program does is you click a button and it tells you how many times you clicked the button in a textfield.
Document Class: This is the entry point of the code.
package {
import flash.display.MovieClip;
/**
* MVCTest.as
* @author rotaercz
*/
public class MVCTest extends MovieClip {
private var _model:Model;
private var _view:View;
private var _control:Control;
public function MVCTest() {
_model = new Model();
_view = new View(this);
_control = new Control(_model, _view);
}
}
}
Model Class: The basic model code.
package {
/**
* Model.as
* @author rotaercz
*/
public class Model {
private var _totalClicks:int;
public function AddClick():void {
_totalClicks++;
}
public function get Clicks():int {
return _totalClicks;
}
public function Model() {
_totalClicks = 0;
}
}
}
Control Class: controls both the input and updating of the model and view here.
package {
import flash.events.MouseEvent;
/**
* Control.as
* @author rotaercz
*/
public class Control {
private var _model:Model;
private var _view:View;
public function Control(model:Model, view:View):void {
_model = model;
_view = view;
_view.addEventListener(MouseEvent.CLICK, OnClick);
}
private function OnClick(e:MouseEvent):void {
_model.AddClick();
_view.Text = "clicked " + _model.Clicks;
}
}
}
View Class: The visual representation of the program.
package {
import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.text.TextField;
/**
* View.as
* @author rotaercz
*/
public class View extends EventDispatcher {
private var _parent:MovieClip;
private var _button:MovieClip;
private var _dt:TextField;
public function set Text(s:String):void {
_dt.text = s;
}
public function View(parent:MovieClip) {
_parent = parent;
_dt = _parent.getChildByName("dt") as TextField;
_button = _parent.getChildByName("mcButton") as MovieClip;
_button.addEventListener(MouseEvent.CLICK, OnClick);
}
private function OnClick(e:MouseEvent):void {
dispatchEvent(e);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在传统的 MVC 模式中,视图确实像 www0z0k 所写的那样直接依赖于模型,但我也认为这不是一个理想的解决方案。
他们设置控制器的方式是充当模型和视图之间的中介,这当然是一个非常有效的解决方案。
但是,如果您希望从模型到视图进行更直接的通信(这会节省一些代码),您可以创建一个中央事件调度程序,将其传递给模型并让它们使用中央事件调度程序来调度其更新事件,然后还传递中央事件调度程序到视图,让视图直接监听模型调度的事件。这样视图就不会直接依赖于模型,但它仍然可以侦听它们发送的事件。在这种情况下,控制器只会将视图事件转换为模型。
图表:
http://bit.ly/sTSDVT
控制器直接更新模型,但它也监听中央事件调度程序从模型获取更新(如果需要)并将其转换为视图。
它还侦听视图事件
该模型仅依赖于事件调度程序并使用它来调度更新事件。
视图仅依赖于事件调度程序并侦听来自模型的更新事件。它也调度自己的事件。 (您可以使用中央事件调度程序,但我不推荐它)
In the traditional MVC pattern the view does have a direct dependency on the model just as www0z0k wrote, but I too think that's not an ideal solution.
They way you have setup your controller it acts as a mediator between the model and view, and that's certainly a very valid solution.
However, if you want a more direct communication from model to view (it will save you some code) you could create a central eventdispatcher, pass it to the models and let them use the central eventdispatcher to dispatch their update events, then also pass the central eventdispatcher to the view and let the view listen to the events dispatched by the models directly. That way the view doesn't have a direct dependency on the models, yet it still can listen to the events they send. In that case the controller will only translate view events to the models.
Diagram:
http://bit.ly/sTSDVT
The controller updates the model directly, but it also listens to the central event dispatcher for updates from the model (if necessary) and translates them to the view.
It also listens for view events
The model only has a dependency on the event dispatcher and uses it to dispatch update events.
The view only has a dependency on the event dispatcher and listens to update events from the model. It dispatches its own events too. (You could use the central event dispatcher for that, but I'd not recommend it)
我相信我的 介绍Flex 应用程序的架构 - Redux 将会引起您的极大兴趣。
如需更华丽的方法,您可以查看FLit Framework。 MVCSprite 是与您的解决方案非常相似。
多年来使用了如此多的框架,我开始相信追求“正确”和“理想”的实现并不是很务实。设计模式并不正式——这些只是想法,应该始终根据您想要实现的质量属性进行调整。请记住,在一次迭代中真正确定该方法的情况非常罕见 - 有时需要完成许多项目才能找到更好的方法 - 以获得更深入的见解。
I believe my Introduction to Flex Application’s Architecture - Redux is going to be quite of interest to you.
For a more Flashy approach you can check the FLit Framework. The MVCSprite is very similar to your solution.
Playing with so many frameworks through the years, I came to believe that it's not very pragmatic to pursue the 'correct' and 'ideal' implementation. Design patterns aren't formal - these are just ideas that should always be adjusted according to the quality attributes you're trying to achieve. Keep in mind that it would be a very rare occasion to really nail the approach in a single iteration - sometimes it takes many projects to be completed before you spot a better way - to get to a deeper insight.
控制器监听视图并更新模型,这部分符合我对 MVC 的愿景。
但我试图避免让控制器参与将模型绑定到视图:
在模型中:
在视图中:
controller listens to view and updates model, this part fits my vision of MVC.
but i'd tried to avoid involving controller in binding model to view:
in model:
in view: