• 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看到,我就不赘述了。

  • 相关阅读:
    进程和线程
    VC中使对话框全屏的方法
    初为项目经理
    Sqlite知识小记
    ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT
    RTX51.LIB NOT FOUND
    Virtual Villagers 攻略
    专业术语常用名词缩写中英文对照
    Eric的四个无刷新(异步更新)示例
    ASP.NET 安全认证(一)—— 如何运用 Form 表单认证 .
  • 原文地址:https://www.cnblogs.com/studynote/p/3158176.html
Copyright © 2020-2023  润新知