新人补钙系列教程之:Pipes Util 实现 Module 之间消息通信

PureMVC MutilCore 版本。所谓多核,是指程序中有多个 PureMVC 实例;在 Flex 中以 Module 方式实现,其中每个 Module 都有其Facade,独立的功能。但 PureMVC 未提供 Module 之间通信的方法,虽然可以通过 Interface 等方法来管理每个 Module,但这样程序间耦合性太高,无法体现MVC优点;好在有官方的解决方法,推荐使用 Pipes Util 解决 Module 之间的通信,Pipes Util 在 Module 之间构建了一层通信框架,使每个 Module 通过普通 Notification 的方式进行消息通信。
     下面举例说明:主程序 Shell 与 Module 相互通信,两者都是独立的 PureMVC 框架。
     Pipe Util 主要通过 JunctionMediator 与其他 Module 通信;由于 JunctionMediator 继承自 Mediator,可转发Notification 通知消息,所以就使 Module 之间的消息通信可利用 PureMVC 本身的架构。

  1. package cn.xinsync.kmeeting.shell.view
  2. {
  3.         import cn.xinsync.kmeeting.common.PipeAwareModule;
  4.         import cn.xinsync.kmeeting.shell.ApplicationFacade;
  5.         import org.puremvc.as3.multicore.interfaces.INotification;
  6.         import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeAware;
  7.         import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
  8.         import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
  9.         import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
  10.         import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;
  11.         import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
  12.         import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeMerge;
  13.         import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeSplit;
  14.         
  15.         public class ShellJunctionMediator extends JunctionMediator
  16.         {
  17.                 public static const NAME:String = "AppJunctionMediator";
  18.                 public function ShellJunctionMediator()
  19.                 {
  20.                         // Junction 是不可视的连结对象,只用于管理 Shell 主程序与 Module 之间通信联络
  21.                         // 实际就是两者之间的逻辑关系: 如响应、监听 Notification 消息, 如何转发等
  22.                         super(NAME, new Junction());
  23.                 }
  24.                 override public function listNotificationInterests():Array
  25.                 {
  26.                         return [ApplicationFacade.MODULE_ADDED, ApplicationFacade.SEND_MESSAGE];
  27.                 }
  28.                 override public function handleNotification(note:INotification):void
  29.                 {
  30.                         switch (note.getName())
  31.                         { // 每个 Module 都必须进行 Pipe(管道)连结管理
  32.                                 case ApplicationFacade.MODULE_ADDED:
  33.                                 {       // 有新 Module 增加
  34.                                         var module:IPipeAware = IPipeAware(note.getBody());
  35.                                         connectPipe(module); // 每个 Module 都连结到管道
  36.                                         break;
  37.                                 }
  38.                                 case ApplicationFacade.SEND_MESSAGE:
  39.                                 {       // 发送消息, 通过 junction 连结器裁决消息通过管道发送到指定的模块中。
  40.                                         var message:Message = new Message(PipeAwareModule.SHELL_MESSAGE, null, note.getBody());
  41.                                        junction.sendMessage(PipeAwareModule.SHELL_TO_MODULE, message);
  42.                                         break;
  43.                                 }
  44.                         }
  45.                 }
  46.                
  47.                 private function connectPipe(module:IPipeAware):void
  48.                 {
  49.                         // 注册管道(Pipe)和管道接口(三通连结 TeeSplit(消息分离)/TeeMerge(消息聚合))
  50.                         junction.registerPipe(PipeAwareModule.SHELL_TO_MODULE, Junction.OUTPUT, new TeeSplit());
  51.                         junction.registerPipe(PipeAwareModule.MODULE_TO_SHELL, Junction.INPUT, new TeeMerge());
  52.                         // 监听消息 - 处理 Module 发送的 Notification 消息
  53.                         junction.addPipeListener(PipeAwareModule.MODULE_TO_SHELL, this, handlePipeMessage);
  54.                         // 通过管道连结 Module 与 Shell
  55.                         var moduleToShell:Pipe = new Pipe();
  56.                         module.acceptOutputPipe(PipeAwareModule.MODULE_TO_SHELL, moduleToShell);
  57.                         var shellInFitting:TeeMerge = junction.retrievePipe(PipeAwareModule.MODULE_TO_SHELL) as TeeMerge;
  58.                         shellInFitting.connectInput(moduleToShell);
  59.                         // 通过管道连结 Shell 与 Module
  60.                         var shellToModule:Pipe = new Pipe();
  61.                         module.acceptInputPipe(PipeAwareModule.SHELL_TO_MODULE, shellToModule);
  62.                         var shellOutFitting:TeeSplit = junction.retrievePipe(PipeAwareModule.SHELL_TO_MODULE) as TeeSplit;
  63.                         shellOutFitting.connect(shellToModule);
  64.                         trace("connected");
  65.                 }
  66.                 override public function handlePipeMessage(message:IPipeMessage):void
  67.                 {
  68.                         switch (message.getType())
  69.                         { // 响应、监听处理 Module 通过管道发送的 Notification 通知消息
  70.                                 case PipeAwareModule.MODULE_MESSAGE:
  71.                                 {       // 使用 PureMVC 框架转发
  72.                                         sendNotification(ApplicationFacade.GET_MESSAGE, message.getBody());
  73.                                         break;
  74.                                 }
  75.                         }
  76.                 }
  77.         }
  78. }
复制代码

连接(Junction)通过管道(Pipe)监听消息,通常写起来比较的麻烦,但可有效限制 Module 之间通信以及监听的消息。
注:本程序模仿自 Water and Bread 的演示程序.

你可能感兴趣的:(Module)