Is this the correct MVC implementation in ActionScript 3?

The whole program is a click on the button, and it tells you how many times you clicked the button in the text box.

Document Class: This is the code entry point.

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: base 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; } } } 

Management class: controls both input and model updating and viewing 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; } } } 

Viewing class: 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); } } } 
+6
source share
3 answers

In the traditional MVC template, the representation is directly dependent on the model, as www0z0k wrote, but I also think that this is not an ideal solution.

As you set up your controller, it acts as an intermediary between the model and the view, and this is certainly a very good solution.

However, if you need a more direct connection with the model (it will save you some code), you can create a central eventdispatcher, pass it to the models and let them use the central eventdispatcher to send their update events, then also pass the central eventdispatcher to the view and give the view listen to events sent directly by models. Thus, the view is not directly dependent on the models, but it can still listen to the events that they send. In this case, the controller will convert view events only in the model.

Chart: http://bit.ly/sTSDVT

The controller updates the model directly, but also listens to the central event manager for updates from the model (if necessary) and translates them into a view. It also listens for viewing events.

The model has only a dependency on the event dispatcher and uses it to send updates.

The view only has a dependency on the event dispatcher and listens to update events from the model. He also sends out his own events. (You can use the centralized event dispatcher for this, but I would not recommend it)

+3
source
Controller

listens to view and update the model, this part corresponds to my vision of MVC.
but I tried to avoid involving the controller in the binding model for viewing:

  _model = new Model(); _view = new View(this, _model); _control = new Control(_model, _view); 

in model:

 public class Model extends EventDispatcher{ //............ public function AddClick():void { _totalClicks++; dispatchEvent(new Event('one more click')); } 

in sight:

  private var _model:Model; public function View(parent:MovieClip, model:Model) { _parent = parent; _model = model; _model.addEventListener('one more click', update); _dt = _parent.getChildByName("dt") as TextField; _button = _parent.getChildByName("mcButton") as MovieClip; _button.addEventListener(MouseEvent.CLICK, OnClick); } private function update(e:Event):void{ _dt.text = _model.clicks; } 
0
source

I believe my Introduction to Flex Applications - Redux Architecture will be of great interest to you.

For a more complex approach, you can check out the FLit Framework . MVCSprite is very similar to your solution.

Playing with so many frameworks over the years, I came to the conclusion that it is not very pragmatic to carry out a “correct” and “ideal” implementation. Design patterns are not formal — they are just ideas that should always be adjusted to the qualities that you are trying to achieve. Keep in mind that it would be a very rare case to truly fake an approach in one iteration — sometimes it takes a lot of projects to complete before you understand the best way — to gain a deeper understanding.

0
source

Source: https://habr.com/ru/post/901248/


All Articles