actionscript3.0中的MVC,EventDispather,IEventDispather的学习

在as3.0中使用mvc来构建项目的时候,在model层,总免不了使用事件。在这个例子里,使用MVC创建了一个简单的数字时钟的功能。在model层,使用了4种方式来处理事件发送。并简单对比了一下。

方法1.model类继承自EventDispather

方法2.model类复合EventDispather

方法3.model类继承自Sprite

方法4.model类实现IEventDispatcher

当前代码注释默认设置方法1来实现,若设置对应的注释可以的尝试使用4种方法来实现这个简单的例子。

自我感觉除了方法2不太好以外,其它方法的都可行。

环境:Flash build4 ,sdk3.5

代码下载地址:https://files.cnblogs.com/bigbigdotnet/MVC.rar

QQ截图未命名

程序运行效果:

文档类(程序入口):

Main.as

package
{
    import Control.*;   
    import Model.*;   
    import View.*;   
    import flash.display.Sprite;   
    [SWF(width="200",height="100")]
    public class Main extends Sprite
    {               
        public function Main()
        {
            var model:Model_A = new Model_A();
            //var model:Model_B = new Model_B();
            //var model:Model_C = new Model_C();
            //var model:Model_D = new Model_D();
            var controller:Controller = new Controller(model);
            var view:View = new View(model,controller);
            addChild(view);   
            view.x = 30;
            view.y = 30;
        }   
    }
}

控制器(control):

Controller.as

package Control
{
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import Model.*;

    public class Controller
    {   
        private var model:Model_A;
        //private var model:Model_B;
        //private var model:Model_C;
        //private var model:Model_D;
        private var timer:Timer;
        public function Controller(model:Model_A):void{
            this.model = model;
        }   

        public function startTime():void{           
            model.startTime();
        }   
    }
}

视图器(view):

View.as

package View
{
    import Control.*;   
    import Model.*;   
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;   
    public class View extends Sprite
    {       
        private var model:Model_A;
        //private var model:Model_B;
        //private var model:Model_C;
        //private var model:Model_D;
        private var controller:Controller;
        private var tf:TextField;   
        public function View(model:Model_A,controller:Controller):void{
            this.controller = controller;
            tf = new TextField();
            addChild(tf);
            controller.startTime();           
            this.model = model;
            //Mode层为Model_A时使用:
            model.addEventListener("action",onActionHandler);           
            //Mode层为Model_B时使用:
            //model.getSender().addEventListener("action",onActionHandler);   
            //Mode层为Model_C时使用:
            //model.addEventListener("action",onActionHandler);   
            //Mode层为Model_D时使用:
            //model.addEventListener("action",onActionHandler);
        }   
        private function onActionHandler(e:Event):void{
            tf.text = "时间:" + model.hour+" : "+model.minutes+" : "+model.second;       
            trace(e.target);
            //当使用方法2的时候,以下这段代码,将会报错!这是因为mode_B中创建_dispather的时候,没有将this的引用注入到_dispatcher里面,
            //即是说,没有把mode_B类的引用注入到_dispatcher里,侦听函数的target对象指向的是dispatcher,而不是mode_B本身
            //尽管在mode_B类中发送消息,可以这样写:dispatcher = new EventDispatcher();,但以下代码就会报错!
            //而如果mode_B类中实现IEventDispather接口,同时这样写:dispatcher = new EventDispatcher( this );以下代码就会正确执行。
            trace(e.target.hour);
        }       
    }
}

模型器(model):

 

方法一:Model_A.as

package Model
{
    import flash.events.*;
    import flash.utils.Timer;       

    //方法1:Model继承自EventDispatcher
    public class Model_A  extends EventDispatcher
    {
        public var hour    :String;       
        public var minutes    :String;       
        public var second    :String;       
        public var timer    :Timer;       
        public function Model_A(){}
        public function startTime():void{
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
            timer.start();
        }
        private function onTimerHandler(e:TimerEvent):void{
            var nowDate:Date = new Date();
            hour     = nowDate.getHours()    >9    ?    String(nowDate.getHours())    :"0"    +    nowDate.getHours();
            minutes = nowDate.getMinutes()    >9    ?    String(nowDate.getMinutes()):"0"    +    nowDate.getMinutes();
            second     = nowDate.getSeconds()    >9    ?    String(nowDate.getSeconds()):"0"    +    nowDate.getSeconds();
            //直接继承自EventDispatcher所以直接调用dispatchEvent广播事件
            dispatchEvent(new Event("action"));
        }
    }
}

方法二:Model_B.as

package Model
{
    import flash.events.*;
    import flash.utils.Timer;       
    //方法1:Model没有继承自EventDispatcher
    public class Model_B
    {
        public var hour    :String;       
        public var minutes    :String;       
        public var second    :String;       
        public var timer    :Timer;       
        //定义一个EventDispatcher对象
        public var _dispatcher    :EventDispatcher;
        public function Model_B(){           
            initSender();
        }       
        public function startTime():void{
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
            timer.start();
        }       
        public function getSender():EventDispatcher{
            return _dispatcher;
        }
        private function initSender():void{           
            _dispatcher = new EventDispatcher();
        }
        private function onTimerHandler(e:TimerEvent):void{
            var nowDate:Date = new Date();
            hour     = nowDate.getHours()    >9    ?    String(nowDate.getHours())    :"0"    +    nowDate.getHours();
            minutes = nowDate.getMinutes()    >9    ?    String(nowDate.getMinutes()):"0"    +    nowDate.getMinutes();
            second     = nowDate.getSeconds()    >9    ?    String(nowDate.getSeconds()):"0"    +    nowDate.getSeconds();
            //调用_dispatchEvent广播事件
            _dispatcher.dispatchEvent(new Event("action"));       
        }
    }
}

方法三:Model_C.as

package Model
{
    import flash.display.Sprite;
    import flash.events.*;
    import flash.utils.Timer;
    //方法3:继承自Sprite,同时Sprite也是属于EventDispather类的子类,
    public class Model_C extends Sprite
    {
        public var hour    :String;       
        public var minutes    :String;       
        public var second    :String;       
        public var timer    :Timer;           
        public function Model_C(){           
        }       
        public function startTime():void{
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
            timer.start();
        }   
        private function onTimerHandler(e:TimerEvent):void{
            var nowDate:Date = new Date();
            hour     = nowDate.getHours()    >9    ?    String(nowDate.getHours())    :"0"    +    nowDate.getHours();
            minutes = nowDate.getMinutes()    >9    ?    String(nowDate.getMinutes()):"0"    +    nowDate.getMinutes();
            second     = nowDate.getSeconds()    >9    ?    String(nowDate.getSeconds()):"0"    +    nowDate.getSeconds();
            //直接调用Spreite所继承的EventDispather类的dispatchEvent广播事件
            super.dispatchEvent(new Event("action"));           
        }
    }
}

方法四:Model_C.as

package Model
{
    import flash.events.*;
    import flash.utils.Timer;
    //方法4,Model直接实现IEventDispatcher,但要实现5个必须的方法
    public class Model_D implements IEventDispatcher
    {       
        public var hour    :String;       
        public var minutes    :String;       
        public var second    :String;       
        public var timer    :Timer;
        private var _dispatcher:EventDispatcher;
        public function Model_D(){
            //this传进来很重要。也即是说把该类的引用传递给EventDispatcher.
            //监听函数的target对象指向Model_D类的引用而不是_dispatcher;
            _dispatcher = new EventDispatcher(this);
        }
        public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
        {
            _dispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);
        }
        public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
        {
            _dispatcher.removeEventListener(type,listener,useCapture);
        }
        public function dispatchEvent(event:Event):Boolean
        {
            return _dispatcher.dispatchEvent(event);
        }
        public function hasEventListener(type:String):Boolean
        {
            return _dispatcher.hasEventListener(type);
        }
        public function willTrigger(type:String):Boolean
        {
            return _dispatcher.willTrigger(type);
        }       
        public function startTime():void{
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
            timer.start();
        }   
        private function onTimerHandler(e:TimerEvent):void{
            var nowDate:Date = new Date();
            hour     = nowDate.getHours()    >9    ?    String(nowDate.getHours())    :"0"    +    nowDate.getHours();
            minutes = nowDate.getMinutes()    >9    ?    String(nowDate.getMinutes()):"0"    +    nowDate.getMinutes();
            second     = nowDate.getSeconds()    >9    ?    String(nowDate.getSeconds()):"0"    +    nowDate.getSeconds();
            //调用dispatchEvent广播事件
            _dispatcher.dispatchEvent(new Event("action"));           
        }       
    }
}

posted @ 2010-08-29 23:37  bigbigdotnet  阅读(1059)  评论(1编辑  收藏  举报