Modifying Model Implementation


Because the view and the model use good encapsulation, we can modify the model implementation without breaking anything. To prove this, we'll make the following change to the ClockData class: enable a real-time feature whereby the model dispatches a change event every second.

The new ClockData class looks like this:

package com.peachpit.aas3wdp.mvcexample.data {        import flash.events.EventDispatcher;    import com.peachpit.aas3wdp.mvcexample.data.Time;    import flash.utils.Timer;    import flash.events.TimerEvent;    import flash.events.Event;    import flash.utils.getTimer;    public class ClockData extends EventDispatcher {              private var _time:Time;       private var _timer:Timer;       private var _realTime:Boolean;       private var _startTime:uint;              public function get time():Time {          // Test if the _realTime property is true as well as          // if the _time property is null.          if(_realTime || _time == null) {             var date:Date;             // Only use a new Date object representing              // the current time and date if the _time             // property is null. Otherwise create a Date             // object using the _time values and then             // add to that the number of milliseconds             // since the model was created.             if(_time == null) {                date = new Date();             }             else {                date = new Date(null, null, null,                 _time.hour, _time.minute,                 _time.second);                date.milliseconds = getTimer()                _startTime;             }             return new Time(date.hours, date.minutes,               date.seconds);          }          else {             return _time.clone();          }       }              public function set time(value:Time):void {          _time = value.clone();          dispatchEvent(new Event(Event.CHANGE));       }       // Setting realTime starts and stops a timer that runs at 1       // second intervals indefinitely.       public function set realTime(value:Boolean):void {          _realTime = value;          if(value) {             if(_timer == null) {                _timer = new Timer(1000, 0);                _timer.addEventListener(TimerEvent.TIMER, onTimer);             }             if(!_timer.running) {                _timer.start();             }          }          else {             if(_timer.running) {                _timer.stop();             }          }       }       public function ClockData() {          _startTime = getTimer();       }                    private function onTimer(event:TimerEvent):void {          dispatchEvent(new Event(Event.CHANGE));       }           } }


With these changes, you can now run the main class and you'll see exactly the same behavior as before. When you've verified that the application works as it did before, even with the changes to the model's implementation, you can now use the new functionality with a few changes to the main class. In this example, we'll delete the timer code from the main class (ClockTest), and we'll set the realTime property of the ClockData object so that the clocks display the current time as it updates in real time.

package {    import flash.display.Sprite;    import flash.display.StageAlign;    import flash.display.StageScaleMode;    import com.peachpit.aas3wdp.mvcexample.data.ClockData;    import com.peachpit.aas3wdp.mvcexample.clock.AbstractClockView;    import com.peachpit.aas3wdp.mvcexample.clock.AnalogClock;    import com.peachpit.aas3wdp.mvcexample.clock.DigitalClock;    import com.peachpit.aas3wdp.mvcexample.data.Time;    public class ClockTest extends Sprite   {       private var _clockData:ClockData;       public function ClockTest() {                    stage.align = StageAlign.TOP_LEFT;          stage.scaleMode = StageScaleMode.NO_SCALE;                    _clockData = new ClockData();          _clockData.realTime = true;                    var clock:AbstractClockView = new            DigitalClock(_clockData);          clock.x = 100;          clock.y = 100;          addChild(clock);                    var clock2:AbstractClockView = new            AnalogClock(_clockData);          clock2.x = 200;          clock2.y = 300;          addChild(clock2);                 }        } }





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net