`
zskc07zskc
  • 浏览: 11352 次
社区版块
存档分类
最新评论

PureMVC之通信

 
阅读更多

  PureMVC的通信并不采用Flash的EventDispatcher/Event机制,而是使用观察者模式以一种松耦合的方式来实现的。
  PureMVC的通信主要依靠三个类来实现:Observer、Notitication和Notifier。我们可以在org.puremvc.patterns.observer包里面找到这三个类,下面就来看看这三个类是怎么实现的吧
  Observer类实现了IObserver接口,在PureMVC中,IObserver接口主要是用于处理notification方法和上下文,并提供notify方法,具体如下: 而Observer的主要工作也正是这些,只不过其有两个属性:notify(Function, notification函数)和context(Object, 上下文对象)
  呵呵,先来看看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。
  这个类简单,就是用于发送信息,所以只提供了一个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持有的数据交互。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics