Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a correct implementation of MVC in Actionscript 3?

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);
        }
    }
}
like image 953
rotaercz Avatar asked Nov 10 '11 21:11

rotaercz


Video Answer


1 Answers

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)

like image 69
Creynders Avatar answered Sep 22 '22 03:09

Creynders