zoukankan      html  css  js  c++  java
  • puremvc

         刚刚学了下puremvc,写了个小例子,就是点击下黄色小圆,换掉图片。

    先看Main:

    package 
    {
        import flash.display.Sprite;
        import flash.text.TextField;
        import flash.events .Event ;
        import mvc.LFacade;
        
        /**
         * ...
         * @author ...
         */
        public class Main extends Sprite 
        {
            public var images:Sprite ;
            public var btnL:Sprite ;
            public function Main()
            {
                images = new Sprite();
    
                images .x=0
                images .y = 1;
                btnL  = new Sprite ();
                btnL .graphics .beginFill(0xffffcc33);
                btnL .graphics .drawCircle(450, 450, 20);
                btnL.graphics .endFill ();
                addChild (btnL );
                addChild (images );
                LFacade .getInstance().startup(this );
            }
        }
    }

    LFacade.as

    package mvc 
    {
        import org.puremvc.as3.patterns.facade.Facade;
        import org.puremvc.as3.interfaces.IFacade;
        import mvc.controller.LCommand;
        /**
         * ...
         * @author sevenmoons
         */
        public class LFacade extends Facade implements IFacade 
        {
            public static const STARTUP:String = "startup";//要发送的消息
            public static const IMAGE_COM:String = "imagecom";
            public static const IMAGE_CHANGE:String = "imagechange";//BtnMediator发送
            public function LFacade() 
            {
                
            }
            public static function getInstance ():LFacade//单例模式
            {
                if (instance==null )
                {
                    instance = new LFacade();
                }
                return instance as LFacade;
            }
            override protected function initializeController():void
            {
                super.initializeController();
                registerCommand(STARTUP, LCommand);
            }
            public function startup(_main:Main):void 
            {
                sendNotification(STARTUP, _main);
            }
        }
    
    }

    LCommand.as

    package mvc.controller 
    {
        import org.puremvc.as3.patterns.command.SimpleCommand;
        import org.puremvc.as3.interfaces.ICommand;
        import org.puremvc.as3.interfaces.INotification;
        
        import mvc.LFacade;
        import mvc.view.BtnMediator;
        import mvc.view.ImageMediator;
        import mvc.model.ImagesProxy;
        /**
         * ...
         * @author sevenmoons
         */
        public class LCommand extends SimpleCommand implements ICommand 
        {
            
            public function LCommand() 
            {
                super();
            }
            override public function execute(note:INotification):void 
            {
                var _main:Main = note.getBody() as Main;
                facade .registerProxy(new ImagesProxy());//在这里添加注册
                facade .registerMediator(new BtnMediator(_main.btnL));
                facade.registerMediator(new ImageMediator(_main.images));
                facade.registerCommand(LFacade.IMAGE_CHANGE, ImageCommand);
                facade .registerCommand(LFacade.IMAGE_COM, ImageComCommand);    
            }
        }
    }

    首先启动ImagesProxy.as

    package mvc.model 
    {
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import org.puremvc.as3.interfaces.IProxy;
        import org.puremvc.as3.patterns.proxy.Proxy;
        import flash.events.Event ;
        import mvc.LFacade;
        
        /**
         * ...
         * @author sevenmoons
         */
        public class ImagesProxy extends Proxy implements IProxy 
        {
            public static const NAME:String = "ImagesProxy";
            public function ImagesProxy(proxyName:String = null, date:Object=null) 
            {
                super (NAME, date);
                var urll:URLLoader = new URLLoader(new URLRequest("pict.xml"));
                urll.addEventListener (Event.COMPLETE , oncom);
            }
            private function oncom(e:Event ):void
            {
                var _xml:XML = XML(e.target .data);
                setData(_xml);
                sendNotification(LFacade.IMAGE_COM);
            }
        }
    
    }

    发送消给注册LFacade.IMAGE_COM的ImageComCommand.as,启动ImageComCommand.as

    package mvc.controller 
    {
        import org.puremvc.as3.interfaces.ICommand;
        import org.puremvc.as3.interfaces.INotification;
        import org.puremvc.as3.patterns.command.SimpleCommand;
        import mvc.view.ImageMediator;
        import mvc.model.ImagesProxy;
        /**
         * ...
         * @author sevenmoons
         */
        public class ImageComCommand extends SimpleCommand implements ICommand 
        {
            
            public function ImageComCommand() 
            {
                super();
            }
            override public function execute(note:INotification):void 
            {
                var _imagesMediator:ImageMediator=facade.retrieveMediator(ImageMediator.NAME) as ImageMediator;
                var _imagesProxy:ImagesProxy=facade.retrieveProxy(ImagesProxy.NAME) as ImagesProxy;
                _imagesMediator.onAddImages(_imagesProxy.getData() as XML);
            }
        }
    
    }

    接着便是ImageMediator工作,把xml传过去

    package mvc.view 
    {
        import flash.display.Sprite;
        import flash.net.URLLoader;
        import flash.display.Loader
        import flash.net.URLRequest;
        import org.puremvc.as3.interfaces.IMediator;
        import org.puremvc.as3.patterns.mediator.Mediator;
        import mvc.LFacade;
        
        /**
         * ...
         * @author sevenmoons
         */
        public class ImageMediator extends Mediator implements IMediator 
        {
            public static const NAME:String = "ImageMediator";
            private var x:XML = new XML;
            public function ImageMediator(viewComponent:Object=null) 
            {
                super (NAME , viewComponent);
            }
            public function get images():Sprite
            {
                return viewComponent as Sprite ;
            }
            public function set index(_index:int ):void 
            {
                var l:Loader = new Loader ();
                l.load (new URLRequest(x.url[_index ]));
                images .addChild (l);
            }
            public function onAddImages(_xml:XML ):void 
            {
                x = _xml;
                var l:Loader = new Loader ();
                l.load (new URLRequest(_xml.url[0]));
                images.addChild(l);
            }
        }
    }

    此时会显示一张图片,如果点击btnL会发送IMAGE—CHANGE,

    package mvc.view 
    {
        import org.puremvc.as3.interfaces.IMediator;
        import org.puremvc.as3.patterns.mediator.Mediator;
        import mvc.LFacade;
        import flash.events .MouseEvent;
        import flash.display .Sprite ;
        
        /**
         * ...
         * @author sevenmoons
         */
        public class BtnMediator extends Mediator implements IMediator 
        {
            public static const NAME:String = "BtnMediator";
            public function BtnMediator(viewComponent:Object=null)  
            {
                super (NAME, viewComponent);
                btnL.addEventListener(MouseEvent.CLICK, onclick);
            }
            private function onclick(e:MouseEvent):void 
            {
                sendNotification(LFacade .IMAGE_CHANGE, 2);
            }
            public function get btnL():Sprite
            {
                return viewComponent as Sprite ;
            }
        }
    }

    ImageCommand会启动,返回给ImageMediatior一个值,2

    package mvc.controller 
    {
        import org.puremvc.as3.interfaces.ICommand;
        import org.puremvc.as3.patterns.command.SimpleCommand;
        import mvc.view.ImageMediator;
        import org.puremvc.as3.interfaces.INotification;
        /**
         * ...
         * @author sevenmoons
         */
        public class ImageCommand extends SimpleCommand implements ICommand 
        {
            
            public function ImageCommand() 
            {
                super();
            }
            override public function execute(note:INotification):void
            {
                (facade.retrieveMediator(ImageMediator.NAME) as ImageMediator).index=note.getBody() as int;
            }
        }
    }

    ImageMediator的index函数会显示另一张图片。

    BtnCommand暂时就没用了。

  • 相关阅读:
    第08讲树
    第11讲简单算法
    【ZOJ1004】Anagrams by Stack
    【ZOJ1649】Rescue
    第10讲并查集
    网站建设与网页制作课件
    获取鼠标的坐标
    asp.net页面的默认回车事件
    NeatUpload的安装使用
    Page methods 执行顺序
  • 原文地址:https://www.cnblogs.com/sevenmoons/p/2547555.html
Copyright © 2011-2022 走看看