PureMVC之通信
来源:互联网 发布:疯狂的java讲义 pdf 编辑:程序博客网 时间:2024/06/15 15:55
PureMVC的通信并不采用Flash的EventDispatcher/Event机制,而是使用观察者模式以一种松耦合的方式来实现的。
PureMVC的通信主要依靠三个类来实现:Observer、Notitication和Notifier。我们可以在org.puremvc.patterns.observer包里面找到这三个类,下面就来看看这三个类是怎么实现的吧
Observer
Observer类实现了IObserver接口,在PureMVC中,IObserver接口主要是用于处理notification方法和上下文,并提供notify方法,具体如下:
- function setNotifyMethod( notifyMethod:Function ):void; //设置notification方法
- function setNotifyContext( notifyContext:Object ):void; //设置notification上下文
- function notifyObserver( notification:INotification ):void; //通知观察者对象
- function compareNotifyContext( object:Object ):Boolean; //比较给定的对象与notification上下文对象
而Observer的主要工作也正是这些,只不过其有两个属性:notify(Function, notification函数)和context(Object, 上下文对象)
Notification
呵呵,先来看看API文档的说法(英文的)
Notifications are not meant to be a replacement for Events in Flex/Flash/AIR. Generally, IMediator implementors place event listeners on their view components, which they then handle in the usual way. This may lead to the broadcast of Notifications to trigger ICommands or to communicate with other IMediators. IProxy and ICommand instances communicate with each other and IMediators by broadcasting INotifications.
A key difference between Flash Events and PureMVC Notifications is that Events follow the 'Chain of Responsibility' pattern, 'bubbling' up the display hierarchy until some parent component handles the Event, while PureMVC Notifications follow a 'Publish/Subscribe' pattern. PureMVC classes need not be related to each other in a parent/child relationship in order to communicate with one another using Notifications.
呵呵,在这就不翻译了,E文不行啦。
当然,Notification就是我们传送的信息的主体,所有传递的信息都放在这个类对象里面。Notification保存信息的对象名称、类型及传递的信息内容,并且提供了相应的getter/setter。
Notifier
这个类简单,就是用于发送信息,所以只提供了一个sendNotification()方法用于发送信息。不过我们查看源代码,可以看到,它其实是通过调用Facade的sendNotification()来处理消息的发送
public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void
{
facade.sendNotification( notificationName, body, type );
}
// Local reference to the Facade Singleton
protected var facade:IFacade = Facade.getInstance();
对于MVC中的Mediator、Proxy、Command三个类而言,Mediator对象可以发送、声明、接收Notification,Proxy只发送,不接收Notification。
当用View注册Mediator时,Mediator的listNotifications方法会被调用,以数组形式返回该Mediator对象所关心的所有Notification。之后,当系统其它角色发出同名的Notification(通知)时,关心这个通知的Mediator都会调用handleNotification方法并将Notification以参数传递到方法。
在很多场合下Proxy需要发送Notification(通知),比如:Proxy从远程服务接收到数据时,发送Notification告诉系统;或当Proxy的数据被更新时,发送Notification告诉系统。
如果让Proxy也侦听Notification(通知)会导致它和View(视图)层、Controller(控制)层的耦合度太高。
View和Controller必须监听Proxy发送的Notification,因为它们的职责是通过可视化的界面使用户能与Proxy持有的数据交互。
- PureMVC之通信
- UI通信--pureMVC
- 游戏框架之puremvc
- UNITY之PureMvc
- UNITY之PureMvc基础知识
- Unity3D架构之PureMVC
- PureMVC与DarkStar(SGS)通信之接收数据后的处理
- puremvc
- pureMVC
- pureMVC
- puremvc
- pureMVC
- PureMVC
- PureMVC
- AS3 PureMVC之核心层
- puremvc框架之hello world!
- puremvc框架之hello world!
- PureMVC学习笔记之基础概念
- sql汉字排序(转贴)
- 实战利用apache来做集群,实现负载均衡
- pureMVC架构解析
- SQL Server 2000 系统表详细说明
- Microsoft UI Automation
- PureMVC之通信
- 查看linux系统的负载之vmstat
- 发布一个黑莓的小程序----打字练习1.0
- Oracle SQL 维护 管理
- 游戏开发之塔防游戏测试 未完成
- SQL连接
- 杭州辦證件↗q+≈100*6484*188≈©㊣®㊣©㊣®
- 用触发器干扰更新值(Oracle)
- at命令 ruby版本简单实现