Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244317
  • 博文数量: 57
  • 博客积分: 2407
  • 博客等级: 大尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-07 15:41
文章存档

2021年(1)

2016年(1)

2014年(3)

2012年(10)

2011年(35)

2010年(1)

2009年(3)

2008年(3)

分类: C/C++

2011-08-12 18:10:27

pureMVC是一个纯MVC结构,purvMVC中负责数据层(M),视图层(V),和控制层(C)分布是Model类,View类,和Control类

Model类负责操作控制Proxy集合(注册/移除Proxy实例),View类负责操作Mediator集合(依然是注册/移除Mediator实例),Control类维护Command类

Model类,View类与Control类不和框架外的类进行通信,三者作为成员变量聚合在Facade类下(设计模式:聚合),

由Facade类统一于外界进行通信(设计模式:门面模式),Facade类使外部程序在不需要了解pureMVC的内部结构的情况下,而与pureMVC进行交互

Java代码  收藏代码
  1. //Facade  
  2. public void registerCommand( String noteName, Class commandClassRef )  
  3. {  
  4.     //Facade类的registerCommand,函数内部调用了Control类的registerCommand  
  5.     this.controller.registerCommand( noteName, commandClassRef );  
  6. }  
//Facade public void registerCommand( String noteName, Class commandClassRef ) { //Facade类的registerCommand,函数内部调用了Control类的registerCommand this.controller.registerCommand( noteName, commandClassRef ); }

Facade类在pureMVC中只有一个实例(设计模式:单例模式),相应的,由于Model类,View类和Control类是Facade的一个成员变量,
Model,View和Control在pureMVC框架中也只存在一个实例
Java代码  收藏代码
  1. public synchronized static Facade getInstance( )  
  2. {  
  3.     //懒汉式单例  
  4.     if (instance == null) {  
  5.         try {  
  6.             instance = new Facade();  
  7.         } catch (Exception e) {  
  8.         }  
  9.     }  
  10.     return instance;  
  11. }  
public synchronized static Facade getInstance( ) { //懒汉式单例 if (instance == null) { try { instance = new Facade(); } catch (Exception e) { } } return instance; }
前面说过,View类负责注册/移除mediator实例,View在注册Mediator时,首先调用Mediator的listNotificationInterests方法,获得该Mediator感兴趣的Notification(命令通知),
然后实例化一个Observer对象,进行注册,那么这时框架在接受到一个命令通知的时候,就会通知了相应的Observer类
Java代码  收藏代码
  1. public void registerMediator( final IMediator mediator )  
  2.     {  
  3.         // Register the Mediator for retrieval by name  
  4.         this.mediatorMap.put(mediator.getMediatorName(), mediator);  
  5.   
  6.         //获得Mediator感兴趣的事件  
  7.         String[] noteInterests = mediator.listNotificationInterests();  
  8.         if (noteInterests.length == 0) {  
  9.             return;  
  10.         }  
  11.   
  12.         // 创建一个匿名类,实现onNotification方法,告诉Observer(观察  
  13.             //  者),对于Mediator感兴趣的事件,应该统一调用mediator.handleNotification  
  14.         IFunction function = new IFunction()  
  15.         {  
  16.             public void onNotification( INotification notification )  
  17.             {  
  18.                 mediator.handleNotification(notification);  
  19.             }  
  20.         };  
  21.   
  22.         // Create Observer  
  23.         Observer observer = new Observer(function, mediator);  
  24.   
  25.         // Register Mediator as Observer for its list of Notification interests  
  26.         for (int i = 0; i < noteInterests.length; i++) {  
  27.             registerObserver(noteInterests[i], observer);  
  28.         }  
  29.     }  
public void registerMediator( final IMediator mediator ) { // Register the Mediator for retrieval by name this.mediatorMap.put(mediator.getMediatorName(), mediator); //获得Mediator感兴趣的事件 String[] noteInterests = mediator.listNotificationInterests(); if (noteInterests.length == 0) { return; } // 创建一个匿名类,实现onNotification方法,告诉Observer(观察 // 者),对于Mediator感兴趣的事件,应该统一调用mediator.handleNotification IFunction function = new IFunction() { public void onNotification( INotification notification ) { mediator.handleNotification(notification); } }; // Create Observer Observer observer = new Observer(function, mediator); // Register Mediator as Observer for its list of Notification interests for (int i = 0; i < noteInterests.length; i++) { registerObserver(noteInterests[i], observer); } }
在这里,应用了观察者模式,View是一个主题,Oberserver类是一个观察者,Notication是一个通知
registerObserver这个方法是将observer实例放在在一张hashtable中,这个hashtable用来保存通知和Observer的映射关系,它的key是通知名,value是一个Observer的集合类Observers类

在Control中,有一个类似的注册方法:registerCommand,它用来维护Notification的映射关系,使用的仍然是观察者模式
Java代码  收藏代码
  1. //notificationName:通知名  
  2.     //commandClassRef:Command类  
  3.     public void registerCommand( String notificationName, Class commandClassRef )  
  4.     {  
  5.         if (null != this.commandMap.put( notificationName, commandClassRef )) return;  
  6.         this.view.registerObserver( notificationName, new Observer(  
  7.         new IFunction()  
  8.         {//创建匿名类,实现onNotification方法  
  9.             public void onNotification( INotification notification )  
  10.             {  
  11.                 //executeCommand方法从根据notificationName实例化一个Command,执行这个Command类的execute,  
  12.                 //所以说Command它是无状态,它只有被需要的时候才会被创建  
  13.                 executeCommand( notification );  
  14.             }  
  15.         }, this ) );  
  16.     }  
//notificationName:通知名 //commandClassRef:Command类 public void registerCommand( String notificationName, Class commandClassRef ) { if (null != this.commandMap.put( notificationName, commandClassRef )) return; this.view.registerObserver( notificationName, new Observer( new IFunction() {//创建匿名类,实现onNotification方法 public void onNotification( INotification notification ) { //executeCommand方法从根据notificationName实例化一个Command,执行这个Command类的execute, //所以说Command它是无状态,它只有被需要的时候才会被创建 executeCommand( notification ); } }, this ) ); }

在Proxy类,Mediator类中在发送通知时sendNotification时,最后都会调用View类notifyObservers
Java代码  收藏代码
  1. public void notifyObservers( INotification note )  
  2. {  
  3.     Observers observers = (Observers) this.observerMap  
  4.             .get(note.getName());  
  5.     if (observers != null) {  
  6.     //notifyObservers方法循环调用每一个Observer的onNotification,对应  
  7.              //Mediator注册的方法,最终调用Mediator.handleNotification进行处理,  
  8.            //对于Command,则最终调用调用Command.execute  
  9.         observers.notifyObservers(note);  
  10.     }  
  11. }  
public void notifyObservers( INotification note ) { Observers observers = (Observers) this.observerMap .get(note.getName()); if (observers != null) { //notifyObservers方法循环调用每一个Observer的onNotification,对应 //Mediator注册的方法,最终调用Mediator.handleNotification进行处理, //对于Command,则最终调用调用Command.execute observers.notifyObservers(note); } }
阅读(1967) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~