pureMVC简单示例及其原理讲解四(Controller层)

本节将讲述pureMVC示例中的Controller层。

Controller层有以下文件组成:

  • AddUserCommand.as
  • DeleteUserCommand.as
  • ModelPrepCommand.as
  • ViewPrepCommand.as
  • StartupCommand.as

AddUserCommand 。顾名思义,它是添加用户命令。让我们首先看看代码。

Addusercommand.as代码  收藏代码
  1. package com.superwulei.controller  
  2. {  
  3.     import com.superwulei.model.UserProxy;  
  4.     import com.superwulei.model.vo.UserVO;  
  5.       
  6.     import mx.controls.Alert;  
  7.       
  8.     import org.puremvc.as3.interfaces.INotification;  
  9.     import org.puremvc.as3.patterns.command.SimpleCommand;  
  10.   
  11.     public class AddUserCommand extends SimpleCommand  
  12.     {  
  13.         override public function execute(notification:INotification):void  
  14.         {  
  15.               
  16.             var user:UserVO = notification.getBody() as UserVO;  
  17.             var userProxy:UserProxy = facade.retrieveProxy(UserProxy.NAME) as UserProxy;  
  18.               
  19.             if(user.isValid){  
  20.                 userProxy.addItem(user);  
  21.             }else{  
  22.                 Alert.show("请检查用户名和密码");  
  23.             }  
  24.         }  
  25.           
  26.     }  
  27. }  

 AddUserCommand是一个单一命令(SimpleCommand),自定义SimpleCommand必须继承SimpleCommand并重写execute方法。execute方法表示这个命令的执行。曾经在上一篇《pureMVC简单示例及其原理讲解——View层 》中提到的添加用户的逻辑代码,应该在这里编写。还记得上一篇中提到的“View层本身不处理各种操作,但是发送通知”么?

上一篇中发送通知的代码
sendNotification(ApplicationFacade.USER_ADD,userForm.user);

 拿出这段代码是特意的想说明AddUserCommand的execute方法中的notification.getBody()其实就是userForm.user,严谨的说应该是userFrom.user作为参数传到execute方法中来。如此我们在这里通过userProxy.addItem(user)就实现了用户的添加。userProxy中的users就多了一个user。

 

DeleteUserCommand ,删除用户命令。代码如下,与添加用户道理一样,不多言。

Deleteusercommand deleteusercommand.as代码  收藏代码
  1. package com.superwulei.controller  
  2. {  
  3.     import com.superwulei.model.UserProxy;  
  4.     import com.superwulei.model.vo.UserVO;  
  5.       
  6.     import org.puremvc.as3.interfaces.INotification;  
  7.     import org.puremvc.as3.patterns.command.SimpleCommand;  
  8.   
  9.     public class DeleteUserCommand extends SimpleCommand  
  10.     {  
  11.         override public function execute(notification:INotification):void  
  12.         {  
  13.             var user:UserVO = notification.getBody() as UserVO;  
  14.             var userProxy:UserProxy = facade.retrieveProxy(UserProxy.NAME) as UserProxy;  
  15.             userProxy.deleteItem(user);  
  16.         }  
  17.           
  18.     }  
  19. }  

 ModelPrepCommand、ViewPrepCommand分别是Model层注册和View层注册。说道注册就要道一道。在pureMVC中,一切总控制是facade,因此无论是Proxy、Mediator还是Command都要在facade中注册。上面四个Command全部为SimpleCommand,最后一个StartupCommand为MacroCommand(复合命令)。StartupCommand包含了多个SimpleCommand,通过addSubCommand方法添加了子命令,并在之后在facade上注册了AddUserCommand和DeleteUserCommand。

Modelprepcommand.as代码  收藏代码
  1. package com.superwulei.controller  
  2. {  
  3.     import com.superwulei.model.UserProxy;  
  4.       
  5.     import org.puremvc.as3.interfaces.INotification;  
  6.     import org.puremvc.as3.patterns.command.SimpleCommand;  
  7.   
  8.     public class ModelPrepCommand extends SimpleCommand  
  9.     {  
  10.         override public function execute(notification:INotification):void  
  11.         {  
  12.             /* 注册Model */  
  13.             facade.registerProxy(new UserProxy());  
  14.         }  
  15.     }  
  16. }  

 

Viewprepcommand.as代码  收藏代码
  1. package com.superwulei.controller  
  2. {  
  3.     import com.superwulei.view.UserFormMediator;  
  4.     import com.superwulei.view.UserListMediator;  
  5.       
  6.     import org.puremvc.as3.interfaces.INotification;  
  7.     import org.puremvc.as3.patterns.command.SimpleCommand;  
  8.   
  9.     public class ViewPrepCommand extends SimpleCommand  
  10.     {  
  11.         override public function execute(notification:INotification):void  
  12.         {  
  13.             var app:MyPureMVCdemo = notification.getBody() as MyPureMVCdemo;  
  14.             /* 注册View */  
  15.             facade.registerMediator(new UserFormMediator(app.userForm));  
  16.             facade.registerMediator(new UserListMediator(app.userList));  
  17.         }  
  18.     }  
  19. }  

 

Startupcommand.as代码  收藏代码
  1. package com.superwulei.controller  
  2. {  
  3.     import com.superwulei.ApplicationFacade;  
  4.       
  5.     import org.puremvc.as3.patterns.command.MacroCommand;  
  6.   
  7.     public class StartupCommand extends MacroCommand  
  8.     {  
  9.         override protected function initializeMacroCommand():void{  
  10.             addSubCommand(ModelPrepCommand);  
  11.             addSubCommand(ViewPrepCommand);  
  12.             /* 注册添加、删除用户命令 */  
  13.             facade.registerCommand(ApplicationFacade.USER_ADD,AddUserCommand);  
  14.             facade.registerCommand(ApplicationFacade.USER_DELETE,DeleteUserCommand);  
  15.         }  
  16.     }  
  17. }  

 通过使用facade的registerCommand就好象添加一个监听器一样,当有sendNotification发送出来的时候,就会有对应的Command的execute方法被执行。

Controller层包含的应该是整个应用程序的逻辑业务。

posted @ 2014-03-29 18:21  腐烂的翅膀  阅读(1462)  评论(0编辑  收藏  举报