Duran's technical life
踏踏实实学技术,认认真真做研究。

iteration::two Cairngorm 0.99 开发指南
@author sakis
@version 0.1

#0
MXML优点:使用方便,XML代码简洁易懂
缺点:事件、函数、界面描混在一起。程序规模大了难于开发维护。

#1
Cairngorm框架是iterationtwo推出的号称基于JEE Best Practice的Flex程序开发的light-weight framework。(恩,light-weight这个词还真是流行呢)。目前最新版本为0.99。

Cairngorm的结构如下:
org
└─nevis
    └─cairngorm
        ├─application
        │      CairngormApplication.as
        │
        ├─business
        │      Responder.as
        │      ServiceLocator.as
        │
        ├─commands
        │      Command.as
        │      SequenceCommand.as
        │
        ├─control
        │      Event.as
        │      EventBroadcaster.as
        │      FrontController.as
        │
        ├─model
        │      ModelLocator.as
        │
        ├─view
        │      ViewHelper.as
        │      ViewLocator.as
        │
        └─vo
                ValueObject.as


#2
下面给大家简单介绍Cairngorm的实现思路。

#2.1
Command/FrontController将Event与Viwe分离。
FrontController实现Singleton模式(以下简写为SP)。所有自定义的Command在要在FrontController构造函数中实例化并以关联数组的方式注册FrontController#addCommand(eventType:String, commandInstance:Command)。EventBroadcaster实现SP。Event类的结构为{type:eventType, data:eventData}。我们通过EventBroadcaster#broadcastEvent(eventType:String, eventData:Object)发布Event。Event发布后,与eventType对应的command instance执行Command#execute(event:Event)。

BTW:在Cairngorm的源码中,eventType、commandName、eventName混用,我统一用eventType。

#2.2
ServiceLocator将Remote Service声明与View分离。
ServiceLocator实现SP。在Cairngorm的demo中,又通过Delegate对象解除Command/Responder和ServiceLocator之间的依赖。这个Delegate做的事情其实意义不大,就是调用ServiceLocator中的Method,设置莫个Responder为相应远程方法的handler。个人觉得无谓地增加了代码量,而且Delegate对象也没实现SP,也就是说我们每次调用一次Remote Service中的Method,都要new一个Delegate对象,实在浪费。

#2.3
ViewLocator/ViewHelper将View(MXML)中夹杂的function与View分离。
ViewHelper有点意思,当一个ViewHelper在某个MXML页面中声明时,如<view:LoginViewHelper id="loginViewHelper" />。ViewHelper能自动取得所在MXML对象的引用,并通过ViewLocator#register(id, this:ViewHelper)将自身注册到ViewLocator中。ViewLocator实现SP。借助ViewLocator/ViewHelper,我们就可以方便的调用不同MXML页面中的方法。

#2.4
ModelLocator是一个marker interface,程序中Model可以放在某个ModelLocator方便调用。

#2.5
ValueObject也是一个marker interface, 基本不需要。

#3
Cairngorm.99给我们开发Flex程序提供了很不错的架构模式,M/V/C/Remote之间可以做到完全解构。但在实际开发时没有必要死扣,代码结构清晰有活力就好。

posted on 2005-09-07 21:49 Duran's technical life 阅读(857) 评论(0)  编辑  收藏 所属分类: 技术积累

只有注册用户登录后才能发表评论。


网站导航: