标签:
转自:http://blog.csdn.net/sutaizi/article/details/6588004
pureMVC 是一个轻量级的框架 它在 flex中非常流行(和cairngorm差不多火)
目前几乎已经移植到所有平台上。
   
下面实现java版得pureMVC搭建
 
先给大家看总体的层次:

 
众所周知 pureMVC是一个轻量级的MVC框架 分为 Model ,View ,Controller。这三个是pureMVC的核心。
 
除此之外pureMVC中含有一个单例模式Facade。facade负责与Model,View,Controller通信。这样就简化了开发的复杂度。
 
所以首先需要编写单一模式类facade 也就是上图的 ApplicationFacade.java
- public class ApplicationFacade extends Facade {  
 
-   
 
-     
 
-     private static ApplicationFacade instance = null;  
 
-   
 
-     
 
-     private PureMVC_Main main ;    
 
-     
 
-     public static ApplicationFacade getInstance(){  
 
-         if( instance == null) instance = new ApplicationFacade ();  
 
-         return instance ;  
 
-     }  
 
-   
 
-     
 
-     protected void initializeController() {  
 
-          trace.d("ApplicationFacade.initController()");  
 
-          super.initializeController();  
 
-          registerCommand(NotiName.NOTI_START, StartupCommand.class);  
 
-     }  
 
-   
 
-     
 
-     public void startup(PureMVC_Main _main) {  
 
-      trace.d("ApplicationFacade.startup");  
 
-      this.main = _main;  
 
-      
 
-      
 
-      
 
-      
 
-      
 
-      
 
-      this.sendNotification(NotiName.NOTI_START, null, null);  
 
-     }  
 
- }  
 
 
这样保证了静态变量facade只有一个实例instance
而这个getInstance 择有具体的main程序来调用,当调用时 框架就启动了。
同时在main程序启动框架时,需要同时调用startup方法,
startup方法做了这么几件事情
1.将main程序的引用传给了facade
2.注册了控制器startupCommand。
 
- public class StartupCommand extends MacroCommand{  
 
-     protected void initializeMacroCommand() {  
 
-         trace.d("PrepStartUpCommand.initializeMacroCommand()");  
 
-         addSubCommand(PrepModelCommand.class);  
 
-         addSubCommand(PrepControllerCommand.class);  
 
-         addSubCommand(PrepViewCommand.class);  
 
-     }  
 
- }  
 
 
而startupCommand又注册了另外3个控制器,他们的作用分别是
注册代理: 
- public class PrepModelCommand extends SimpleCommand implements ICommand{  
 
-       
 
-     public void execute(INotification noti){  
 
-         trace.d("PrepModelCommand.execute()");  
 
-         this.facade.registerProxy(new LoginProxy());  
 
-     }  
 
- }  
 
 
让Mediator得到页面的引用
- public class PrepViewCommand extends SimpleCommand implements ICommand{  
 
-       
 
-     public void execute(INotification noti){  
 
-         trace.d("PrepViewCommand.execute("+ noti.getName() + ")");  
 
-           
 
-         this.facade.registerMediator(new LoginMediator(new LoginWindow()));  
 
-     }  
 
- }  
 
 
还有一个 是保存Controller与Command的映射,注册消息 与Command 当Command需要时 Command会被创建。
- public class PrepControllerCommand extends SimpleCommand implements ICommand{  
 
-       
 
-     
 
-     @Override  
 
-     public void execute(INotification noti){  
 
-         trace.d("PrepComtrollerCommand.excute()");  
 
-         this.facade.registerCommand(NotiName.NOTI_LOGIN, LoginCommand.class);  
 
-     }  
 
- }  
 
 
 
这样框架就启动了。下面来说说登录的例子。
PureMVC 的View分为2个部分 一个是 纯的UI界面,另一个是Mediator模式
这种模式的好处在于他将UI界面(如loginWondow.java--登录界面)中所有的UI组件 比如 button textField ..封装在一起,通过保存UI界面的应用来调用他们。
打个比方就是:
一栋宿舍楼 ,每一间宿舍都是一个UI控件,真个宿舍楼就是一个Mediator,宿舍楼门口有看门的大爷,大爷知道所有宿舍的情况,。
当有人访问宿舍们需要知道具体某间宿舍的情况的时候 他只需要去问门卫大爷。不需要亲自走到那间宿舍。就能知道具体情况。
在Mediator中有2个重要方法:
 
- public String[] listNotificationInterests() {  
 
-        String[] list = {  
 
-            NotiName.LOGIN_SUCCESS, NotiName.LOGIN_FAILED  
 
-        };  
 
-        return list;  
 
-    }  
 
-   
 
- @Override  
 
- public void handleNotification(INotification noti) {  
 
-        if (noti.getName().equals(NotiName.LOGIN_SUCCESS)){  
 
-    trace.d("login success");  
 
-        }  
 
-        
 
-        if (noti.getName().equals(NotiName.LOGIN_FAILED)){  
 
-    trace.d("login failed " + noti.getBody().toString());  
 
-        }  
 
-    }  
 
 
第一个方法罗列出 要接受的消息名字
第二个方法进行处理消息。
再看以下代码
- public LoginMediator(LoginWindow v) {  
 
-         super(NAME, null);  
 
-         
 
-         this.setViewComponent(v);  
 
-         view = v;  
 
-         v.setVisible(true);  
 
-           
 
-         v.btnLogin.addActionListener(new ActionListener(){  
 
-             public void actionPerformed(ActionEvent e) {  
 
-                 
 
-                 login();  
 
-             }  
 
-         });  
 
-     }  
 
-       
 
-     private void login(){  
 
-         
 
-         User user = new User(  
 
-                         view.txtName.getText(),  
 
-                         view.pwdPwd.getText()  
 
-                             );  
 
-         
 
-         
 
-         sendNotification(NotiName.NOTI_LOGIN,user,null);  
 
-     }  
 
 
在Mediator中 处理了所有UI中的事件  比如说为Button添加监听
并且收集数据 (收集 用户名和密码)封装到User中(自动以Object),然后发送消息给Command,把这些数据也发送出去。
此时由于刚才在PrepControllerCommand.java中注册了这个消息 对应的LoginCommand会被激活,处理这个消息。
 
- public class LoginCommand extends SimpleCommand implements ICommand {  
 
-       
 
-       
 
-     public void execute(INotification noti) {  
 
-         
 
-        if (noti.getName().equals(NotiName.NOTI_LOGIN) == true) {  
 
-             User user = (User)noti.getBody();  
 
-               
 
-             LoginProxy lp = new LoginProxy();  
 
-               
 
-             if(checkLogin(user))      
 
-                 lp.login(user);  
 
-             else  
 
-                 sendNotification((String) NotiName.LOGIN_FAILED,"User name should not be empty!",null);  
 
-        }  
 
-     }  
 
-       
 
-     private boolean checkLogin(User u){  
 
-         
 
-         if(u.getUsername() == null )return false;  
 
-           
 
-         return true;  
 
-     }  
 
- }  
 
 
这里处理这个消息买并且调用Model中的Proxy。Proxy负责进行数据操作,比如得到远程服务器的数据,或者与数据库进行交互。
处理完数据后 吧数据通过消息(Notification)发送给Mediator 整个流程就完成了。
 
- public class LoginProxy extends Proxy{  
 
-     public static final String NAME = "LoginProxy";  
 
-       
 
-     public LoginProxy() {  
 
-         super(NAME, null);  
 
-         trace.d("LoginProxy.LoginProxy()");  
 
-     }  
 
-   
 
-     public void login(User user) {  
 
-         
 
-         
 
-         
 
-         
 
-           
 
-         if(user.getUsername().equals("young") )  
 
-             sendNotification(NotiName.LOGIN_SUCCESS,"login success!",null);  
 
-         else   
 
-             sendNotification(NotiName.LOGIN_FAILED,"login error ,wrong username",null);  
 
-     }  
 
-   
 
- }  
 
 
Mediator在HandleNotification中处理Proxy发来的消息
pureMVC java版搭建流程
标签:
原文地址:http://www.cnblogs.com/-yan/p/4273446.html