zoukankan      html  css  js  c++  java
  • Robotlegs框架1.5简介

    Robotlegs框架1.5简介

    该框架的1.5版本位于https://github.com/robotlegs/robotlegs-framework/tree/version1,现在已经出了重新架构的2.0版本,所以我决定先研究已经成熟的1.6版本,有时间再研究2.0版本的。

     

    我看完这个框架的源码和范例的第一印象就是自动化,这个框架把很多逻辑都集成到了系统内部,而我们只需要进行一些简单的映射,进行一些简单的功能实现就能完成一个项目了。

     

     

    基本结构

     

    Robotlegs 的MVCS实现将应用程序解耦为4层:Model-View-Controller (MVC) 和 Service层,view 层处理用户交互. model 层处理用户创建的或从外部获取的数据. controller 提供一种封装各层之间复杂交互的机制. 最后, service 层提供一种和外界(比如远程服务 API 或文件系统)交互的独立机制.

     

    其核心主要是有4个类:

     

    Context: 提供一个中心的事件 bus 并且处理自己的启动和关闭. 一个 context 定义了一个范围. 框架 actor 们处在 context 之内,并且在 context 定义的范围之内进行相互间的通讯.在利用Robotlegs框架时,我们需要继承该类,并实现我们自己的startup函数,当我们建立了Context后,startup函数会在初始化后自动运行,我们需要在这个函数里配置模型映射,事件映射和视图映射以及新建视图进行系统初始化,下面是一个例子:

     

    复制代码
    public class ExampleContext extends Context
    {
    
    public function ExampleContext(contextView:DisplayObjectContainer, autoStartup:Boolean = true)
    {
    super(contextView, autoStartup);
    }
    
    
    override public function startup():void
    {
    commandMap.mapEvent(ContextEvent.STARTUP, PrepModelCommand, ContextEvent, true);
    commandMap.mapEvent(ContextEvent.STARTUP, PrepViewCommand, ContextEvent, true);
    commandMap.mapEvent(ContextEvent.STARTUP, PrepControllerCommand, ContextEvent);
    commandMap.mapEvent(ContextEvent.STARTUP, StartupCommand, ContextEvent, true);
    
    // fire!
    dispatchEvent(new ContextEvent(ContextEvent.STARTUP));
    }
    
    }
    复制代码

     

     

    Actor: Model 和 Service都继承自Actor。

     

     

    Command: Controller由Command实现,针对我们需要处理的消息,我们先定义相应的处理类,并在execute函数中进行处理。然后我们将消息和相应的Command映射起来:

     

    commandMap.mapEvent(eventType:String, commandClass:Class, eventClass:Class = null, oneshot:Boolean = false)

     

     

     

    Mediator:View层包括view component和Mediator,在建立视图前,我们需要先建立view component和Mediator之间的映射:

     

    mediatorMap.mapView( ViewClass, MediatorClass, autoCreate, autoRemove );

     

     

    建立完两者之间的映射后,当我们新建一个view component加入stage时,系统自动帮我们新建一个指定的Mediator与之相关联。

     

    Mediator 负责所中介的 view component 发出的事件, 我们需要将消息和相应的处理函数映射起来:

     

    eventMap.mapListener(myMediatedViewComponent, SomeEvent.USER_DID_SOMETHING, handleUserDidSomethingEvent)

     

     

     

    SwiftSuspenders框架的应用

     

    RobotLegs框架配备了SwiftSuspenders来实现它的自动化依赖注入。SwiftSuspenders框架的详细信息,见文章轻量级IOC框架SwiftSuspenders,下面说说这个框架在Robotlegs中的应用。

     

    1.在新建Context时就进行一些值映射,这样可以轻松的保证一些对象在整个系统中的一致性。

     

    复制代码
    protected function mapInjections():void
    {
    injector.mapValue(IReflector, reflector);
    injector.mapValue(IInjector, injector);
    injector.mapValue(IEventDispatcher, eventDispatcher);
    injector.mapValue(DisplayObjectContainer, contextView);
    injector.mapValue(ICommandMap, commandMap);
    injector.mapValue(IMediatorMap, mediatorMap);
    injector.mapValue(IViewMap, viewMap);
    injector.mapClass(IEventMap, EventMap);
    }
    复制代码

     

     

     2.初始化Modal和Service时。因为一般这些都有Singleton的需求,可以利用injector.mapSingleton来轻松实现

     

     3.用injector轻松实现添加view component时新建相关联的Mediator。

     

     4.框架使用者也能从中获利。

     

     

    系统自动化的实现

     

    让我们再来回顾一下启动一个系统需要做的事:

     

     

     1.新建Context

     

     2.将Modal映射为Singleton

     

     3.为消息映射相关的Command

     

     4.为view component映射相关的Mediator

     

     5.新建view component并加入view list中。

     

    然后的然后,整个系统就跑起来了!

     

     

    我们不需要为新建的view component建立Meditor,系统会在一个view component加入view list中后,从mediatorMap中找出和view component对应的Mediator类,并新建相应的Mediator。并且会自动的调用Mediator的onRegister函数,将Mediator处理的事件加入eventMap中。

     

    我们不需要操心Modal的新建,一致性等问题,因为injector帮我们将其映射成了singleton,这样我们只需要在使用Modal的类中加入[Inject]注入点就行。

     

    共用的eventMap使得各个部分的通信更容易,每个层都不需要操心和其他层之间的交互,只需要把自己的事情处理好,其他的事情通过dispatch将消息发出去让其他的层处理就行。

     

     

    应用的例子

     

    joelhooks为robotlegs写了很多的例子,可以通过https://github.com/joelhooks/robotlegsdemos看到,我就不赘述了。

     



    作者:Jingle Guo
    出处:http://www.cnblogs.com/studynote/
    若标题中有“转载”字样,则本文版权归原作者所有。若无转载字样,本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

     

     

     

    分类: flash
    标签: ActionScript3AS3框架

  • 相关阅读:
    Atitit 人脸识别 眼睛形态 attilax总结
    Atitit 手机号码选号 规范 流程 attilax总结 v2 r99.docx
    atitit 板块分类 上市公司 龙头企业公司 列表 attilax总结.docx
    Atititi atiitt eam pam资产管理 购物表去年.xlsx
    使用cmd查看电脑连接过的wifi密码(一)
    常见十大web攻击手段 悟寰轩
    常见web攻击方式 悟寰轩
    【MYSQL数据库】MYSQL学习笔记mysql分区基本操作 悟寰轩
    Filter及FilterChain的使用详解 悟寰轩
    启动tomcat spring初始化两次问题(eg:@PostConstruct) 悟寰轩
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3163162.html
Copyright © 2011-2022 走看看