首先:
pureMVC既然是MVC(Model、View、Controller)框架,那你就必须要记住pureMVC中的四个主要结构类:
- Proxy(相当于Model)
- Mediator(相当于View)
- Command(相当于Controller)
- Facade(管理Proxy、Mediator和Command的东西)
view
本篇说的是View层,即视图层,在本示例中包括两个部分:MXML文件,即可视控件;Mediator。
因为pureMVC的追求是将MVC分离,而这些逻辑操作我们都会将其放入Command中,即Controller才层。但是在View层,我们也不是没有作为的。一般的,一个可视控件会对应一个Mediator;但有时候多个可视控件也会对应一个Mediator。
sendNotification方法。刚才我们所说的添加用户的方法不是没有作为,这就是我所谓的有所作为,即“发送通知”。pureMVC中将逻辑控制放在了Controller层,因此在View层就不会有添加用户的代码,但是我们将“添加用户”这个命令以通知的方式发送出去。注意 sendNotification方法中携带了两个参数,第一个参数是一个常量,第二个参数是一个UserVO对象。这个常量表示会调用与之对应的 Command,这个UserVO对象表示的就是要添加的用户。这个在Controller层会讲到。
关于Mediator再补充一下,在每个Mediator中会看到有一个公有的静态的常量字符串,这个字符串用来表示Mediator的类名。同样的在 Proxy和Command中你也会看到,这样就可以通过facade的retrieveMediator、retrieveProxy和 retieveCommand来检索对象。
关于Model层要记住:一个MXML可视控件对应一个Mediator,Mediator本身不处理操作但它会发送Notification(发送出去的Notification会自动被执行),关于界面上的操作采用监听的方式即addEventLisentner。
pureMVC是一个MVC框架,核心部分包括Proxy、Mediator、Command和Facade,Facade同意管理前三个核心部分。
Facade,Proxy、Mediator、Command的统一管家。自定义Facade必须继承Facade
ApplicationFacade中使用了单例模式,即整个应用程序中只会存在一个ApplicationFacade的对象。StartupCommand是一个复合命令,在这里注册了这个命令,其他的包括所有的Proxy、Mediator和AddUserCommand、DeleteUserCommand也会被同时注册
*******************************************************************************************
1、ApplicationiFacade发送notification给cmd或者mediator需要在ApplicationFacade里面注册;
2、mediator里面监听facade发送过来的时候,有一个人return,容易忽略;
如
override public function listNotificationInterests():Array{
return [
NotificationConst.GETPERSONINFOBACK, //这里必须做监听
NotificationConst.BATTLE_START ,
];
}
override public function handleNotification(note:INotification):void{
switch(note.getName()){
case NotificationConst.GETPERSONINFOBACK:
getPersonInfoBack(note.getBody());
break;
case NotificationConst.BATTLE_START :
onbattleStart();
break;
}
}
3、
public static const NAME:String="monsterproxy";
public function MonsterListProxy()
{
super(NAME); //这一行容易忽略
}
4、当mediator里面监听view里面的事件时,如
public function TripodMediator(mediatorName:String=null, viewComponent:Object=null)
{
super(mediatorName, viewComponent);
initListener();
this.sendNotification(NotificationConst.GETMONSTERKINDSLIST); //这里发了一次通知
}
public function initListener():void{
view.addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
}
public function get view():TripodView{
return this.getViewComponent() as TripodView; //这里还需要再发一次
}
这里涉及到先后顺序的问题,一个是构造函数,一个是addChild到舞台。其实不太明白
5、mediator和proxy都需要注册。
mediator在view里面注册:ApplicationFacade.getInstance().registerMediator(new TripodMediator(this));
proxy在command里面注册:facade.registerProxy(new TripodProxy()); 这里的comand并不是某一个模块中的controler,它是单独列出来的一个startUpCommand,然后在facade中注册
如下例子:
1
2、
也可以同时注册多个proxy,在一个command里面同时写上全部的proxy注册,然后利用startUP即可。
如果要获得proxy实例,必须proxy=ApplicationFacade.getInstance().retrieveProxy(TripodProxy.NAME) as TripodProxy;
6、同样command也必须注册,它只是和notificationConst关联。它在ApplicationFacade里面注册
ApplicationFacade.getInstance().registerCommand(NotificationConst.STARTUP,InitProxyCommand);
这样在STARTUP的通知里注册,就可以同时注册所有的command
这里的InitProxyCommand包含了所有的command
如下
public class InitProxyCommand extends SimpleCommand
{
override public function execute(notification:INotification):void
{
facade.registerProxy(new LoginProxy());
facade.registerProxy(new ChatProxy());
}
}
也可对单个进行注册,如下
ApplicationFacade.getInstance().registerCommand(NotificationConst.LOGINGAME,LoginCommand);
*********************************************************************************