PureMVC之通信

 

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 持有的数据交互。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值