關於 Puremvc Command 的那點事

39
關於 PureMVC Command 那點事 Erin Lin http://about.me/erinlin 2011年5月17日星期

description

PureMVC Command 相關應用 http://erinylin.blogspot.com/ 勘誤: Page20 為 : 啥叫非同步 Command?

Transcript of 關於 Puremvc Command 的那點事

Page 1: 關於 Puremvc Command 的那點事

關於 PureMVC Command 的那點事

Erin Linhttp://about.me/erinlin

2011年5月17日星期二

Page 2: 關於 Puremvc Command 的那點事

到處都有 Command...

2011年5月17日星期二

Page 3: 關於 Puremvc Command 的那點事

Command 是?

2011年5月17日星期二

Page 4: 關於 Puremvc Command 的那點事

Command 是?

2011年5月17日星期二

Page 5: 關於 Puremvc Command 的那點事

Command 是?

2011年5月17日星期二

Page 6: 關於 Puremvc Command 的那點事

Command 是?

2011年5月17日星期二

Page 7: 關於 Puremvc Command 的那點事

不想學還是要學的命令 設計模式

CommandCommand

CommandCommand

Command

Command

Command

CommandCommand

Command

Command

Command

Command

Command

Command

2011年5月17日星期二

Page 8: 關於 Puremvc Command 的那點事

<<interface>>ICommand

execute():void

Command

execute():void

Controller

addCommand()removeCommand()//-------------------

doSomething()

2011年5月17日星期二

Page 9: 關於 Puremvc Command 的那點事

真情推薦傻瓜都可以看懂的設計模式入門書

自己上網去買!

http://www.oreilly.com.tw/product_java.php?id=a1632011年5月17日星期二

Page 10: 關於 Puremvc Command 的那點事

回到 PureMVC...

2011年5月17日星期二

Page 11: 關於 Puremvc Command 的那點事

PureMVC兩個基本必知 Command

2011年5月17日星期二

Page 12: 關於 Puremvc Command 的那點事

最基本的SimpleCommand

2011年5月17日星期二

Page 13: 關於 Puremvc Command 的那點事

反正要亂搞就是extends

SimpleCommand

2011年5月17日星期二

Page 14: 關於 Puremvc Command 的那點事

package com.mvc.controls{ import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand;

public class StartupCommand extends SimpleCommand { public function StartupCommand() { super(); } override public function execute(notification:INotification):void{ //初始 Application 要做的事情 //facade.registerMediator, facade.registerProxy // or facade.registerCommand //通常都會將 Application 傳進來做應用 } }}

你認識的第一支繼承 SimpleCommand 寫法

2011年5月17日星期二

Page 15: 關於 Puremvc Command 的那點事

群組同時執行的MacroCommand

2011年5月17日星期二

Page 16: 關於 Puremvc Command 的那點事

package com.mvc.controls{ import org.puremvc.as3.patterns.command.MacroCommand;

public class StartupCommand extends MacroCommand { public function StartupCommand() { super(); } override protected function initializeMacroCommand() :void { addSubCommand( ModelPrepCommand ); addSubCommand( ViewPrepCommand ); addSubCommand( 你寫的Command ); } }}

使用 MacroCommand 的 StartupCommand

2011年5月17日星期二

Page 17: 關於 Puremvc Command 的那點事

啊...我想要一個命令做完,才要執行下一個....

最後還要來個完美的 Ending

要怎麼辦?

2011年5月17日星期二

Page 18: 關於 Puremvc Command 的那點事

PureMVC Utilities使用的時候要心存感激喔!

http://trac.puremvc.org/PureMVC_AS3/

2011年5月17日星期二

Page 19: 關於 Puremvc Command 的那點事

處理非同步的 AsyncCommand

http://trac.puremvc.org/Utility_AS3_AsyncCommand

2011年5月17日星期二

Page 20: 關於 Puremvc Command 的那點事

啥叫非同步?

2011年5月17日星期二

Page 21: 關於 Puremvc Command 的那點事

當然就是一件工作做完才做下一個指令

照順序來不懂嗎?

2011年5月17日星期二

Page 22: 關於 Puremvc Command 的那點事

AsyncCommand 也有兩個 Class 給你用

2011年5月17日星期二

Page 23: 關於 Puremvc Command 的那點事

AsyncCommand and AsyncMacroCommand

2011年5月17日星期二

Page 24: 關於 Puremvc Command 的那點事

基本用法是以成組的方式應用

2011年5月17日星期二

Page 25: 關於 Puremvc Command 的那點事

package controllers{ import flash.utils.setTimeout; import org.puremvc.as3.multicore.interfaces.ICommand; import org.puremvc.as3.multicore.interfaces.INotification; import org.puremvc.as3.multicore.patterns.command.AsyncCommand; public class AsyncCommand0 extends AsyncCommand implements ICommand { public function AsyncCommand0() { super(); } override public function execute(notification:INotification):void{ trace("lalala AsyncCommand0"); setTimeout( commandComplete, 1000); } }}

2011年5月17日星期二

Page 26: 關於 Puremvc Command 的那點事

package controllers{ import org.puremvc.as3.multicore.patterns.command.AsyncMacroCommand; public class StartupCommand extends AsyncMacroCommand { public function StartupCommand() { super(); } private function onComplete():void{ trace("end of StartupCommand"); } override protected function initializeAsyncMacroCommand():void{ setOnComplete( onComplete ); addSubCommand( AsyncCommand0 ); addSubCommand( AsyncCommand1 ); addSubCommand( AsyncCommand2 ); } }}

2011年5月17日星期二

Page 27: 關於 Puremvc Command 的那點事

AsyncCommandDEMO

2011年5月17日星期二

Page 28: 關於 Puremvc Command 的那點事

所以 Command 可以做什麼?

2011年5月17日星期二

Page 29: 關於 Puremvc Command 的那點事

應用一:Assets loader

2011年5月17日星期二

Page 30: 關於 Puremvc Command 的那點事

package controllers{ import mx.rpc.AsyncToken; import mx.rpc.IResponder; import mx.rpc.http.HTTPService; import org.puremvc.as3.multicore.interfaces.ICommand; import org.puremvc.as3.multicore.interfaces.INotification; import org.puremvc.as3.multicore.patterns.command.AsyncCommand; public class LoadConfigCommand extends AsyncCommand implements IResponder { public function LoadConfigCommand() { super(); } override public function execute(notification:INotification):void{ var service:HTTPService = new HTTPService; service.resultFormat = 'xml'; service.url = "your configuration files url"; service.send(); } public function result( result:Object ):void{ this.commandComplete(); } public function fault( result:Object ):void{ //如果要中斷流程,需要在這邊傳出 ERROR notification 由其他 Command 處理 } }

2011年5月17日星期二

Page 31: 關於 Puremvc Command 的那點事

package controllers{ import org.puremvc.as3.multicore.patterns.command.AsyncMacroCommand; public class StartupCommand extends AsyncMacroCommand { public function StartupCommand() { super(); } private function onComplete():void{ trace("end of StartupCommand"); sendNotification( "APP_INIT" ); } override protected function initializeAsyncMacroCommand():void{ this.setOnComplete( onComplete ); addSubCommand( LoadConfigCommand ); addSubCommand( LoadAssetsCommand ); addSubCommand( LoadXXXCommand ); } }}

2011年5月17日星期二

Page 32: 關於 Puremvc Command 的那點事

應用二:做外掛...

2011年5月17日星期二

Page 33: 關於 Puremvc Command 的那點事

package com.controls{ public class GroupEditorCommand extends SimpleCommand implements ICommand { public function GroupEditorCommand() { super(); } override public function execute(notification:INotification):void { switch( notification.getName() ){ case "GroupEditorCommand.INIT": //將之前開發用的 proxy notification 組織起來 facade.registerCommand( "DataProxy.ITEM_UPDATED" , GroupEditorCommand ); facade.registerCommand( "XXXProxy.NOTIFICATION_NAME" , GroupEditorCommand ); showLoader(); //看你要做什麼起始 break; case "DataProxy.ITEM_UPDATED": //看要叫 proxy 做啥,還是 call 啥畫面出來 break; } } private function clearCommands():void{ facade.removeCommand( "DataProxy.ITEM_UPDATED" ); facade.removeCommand( "XXXProxy.NOTIFICATION_NAME" ); removeLoader(); sendNotification( "GroupEditorCommand.CLOSE" ); } private function showLoader( string:String ):void{ //將檔畫面的 loader call 到前景 } private function removeLoader():void{ //remove loader } }}

2011年5月17日星期二

Page 34: 關於 Puremvc Command 的那點事

其他?

2011年5月17日星期二

Page 35: 關於 Puremvc Command 的那點事

其實你要怎樣玩它就開心的玩吧!

想太多就什麼都寫不出來了!

2011年5月17日星期二

Page 36: 關於 Puremvc Command 的那點事

最後...

2011年5月17日星期二

Page 37: 關於 Puremvc Command 的那點事

請保持愉快的心情開心的寫程式吧!

2011年5月17日星期二

Page 38: 關於 Puremvc Command 的那點事

FIN

2011年5月17日星期二

Page 39: 關於 Puremvc Command 的那點事

參考資料

• http://trac.puremvc.org/PureMVC_AS3/

• http://www.oreilly.com.tw/product_java.php?id=a163

• http://trac.puremvc.org/Utility_AS3_AsyncCommand

2011年5月17日星期二