Swiftsuspenders2 是一個基於元數據(metadata)的IOC(控制反轉,inversion of control)的AS3的解決方案。反轉控制又被稱依賴注射(Dependency Injection),也就是將依賴先剝離,而後在適當時候再注射進入。它是一種下降耦合度的程序設計模式其核心原則是高層模塊不該依賴於低層模塊,他們都應該依賴於抽象。抽象不依賴細節,細節依賴抽象,它經過將對象的建立過程解耦出來來下降對象間的依賴關係。IOC的設計目標是不直接建立對象,可是描述對象的建立方式,在代碼中不直接連接對象和服務,經過配置的方式描述哪種組件須要哪一些服務,而後在ioc容器中負責自動將對象服務和須要使用他們的地方進行連接。下面咱們經過一個簡單的例子,先了解一下如何利用依賴注入進行解耦。swift
當咱們須要進行一個發送消息或者郵件的流程時 咱們可能會寫以下代碼(demo0):設計模式
1 package demo0 2 { 3 /** 4 * ... 5 * @author titi 6 */ 7 public class Mail 8 { 9 public var Title:String; 10 11 public function Send():void 12 { 13 trace(" Send Mail:"+Title); 14 } 15 16 public function Mail(title:String) 17 { 18 Title = title; 19 } 20 21 } 22 23 } 24 25 26 package demo0 27 { 28 /** 29 * ... 30 * @author titi 31 */ 32 public class Notification 33 { 34 private var _email:Mail; 35 public function Notification() 36 { 37 _email = new Mail("測試郵件1"); 38 } 39 40 41 public function PromotionalNotification():void 42 { 43 _email.Send(); 44 } 45 } 46 47 } 48 49 50 package demo0 51 { 52 import flash.display.Sprite; 53 import flash.events.Event; 54 55 /** 56 * ... 57 * @author titi 58 */ 59 public class Main0 extends Sprite 60 { 61 62 public function Main0():void 63 { 64 if (stage) init(); 65 else addEventListener(Event.ADDED_TO_STAGE, init); 66 } 67 68 private function init(e:Event = null):void 69 { 70 removeEventListener(Event.ADDED_TO_STAGE, init); 71 // entry point 72 var notificatoin:Notification = new Notification(); 73 notificatoin.PromotionalNotification(); 74 } 75 76 } 77 78 }
這裏Mail和其控制器Notification 是直接關聯的,控制器依賴Mail對象。經過抽象Mail 能夠進行解耦下降Notification和Mail的耦合度。app
1 package demo1 2 { 3 4 /** 5 * ... 6 * @author titi 7 */ 8 public interface IMsg 9 { 10 function set Title(value:String):void; 11 12 function get Title():String; 13 14 function Send():void; 15 } 16 17 } 18 19 package demo1 20 { 21 /** 22 * ... 23 * @author titi 24 */ 25 public class Mail implements IMsg 26 { 27 public var _Title:String; 28 29 public function Send():void 30 { 31 trace(" Send Mail:"+Title); 32 } 33 34 public function Mail(title:String) 35 { 36 _Title = title; 37 } 38 39 /* INTERFACE demo1.IMsg */ 40 41 public function set Title(value:String):void 42 { 43 _Title = value; 44 } 45 46 public function get Title():String 47 { 48 return _Title; 49 } 50 51 } 52 53 } 54 55 package demo1 56 { 57 /** 58 * ... 59 * @author titi 60 */ 61 public class Notification 62 { 63 private var _email:IMsg; 64 65 public function PromotionalNotification():void 66 { 67 _email.Send(); 68 } 69 } 70 71 }
抽象後 能夠經過四種方式(注入點) 在Notification 中注入IMail對應的具體的Mail 分別是框架
1 package demo1 2 { 3 /** 4 * ... 5 * @author titi 6 */ 7 public class Notification 8 { 9 private var _email:IMsg; 10 public function Notification(email:IMsg) 11 { 12 _email = email; 13 } 14 15 16 public function PromotionalNotification():void 17 { 18 _email.Send(); 19 } 20 } 21 22 }
1 package demo1 2 { 3 /** 4 * ... 5 * @author titi 6 */ 7 public class Notification3 8 { 9 private var _email:IMsg; 10 11 public function Notification3() 12 { 13 14 } 15 16 17 public function PromotionalNotification(mail:IMsg):void 18 { 19 mail.Send(); 20 } 21 } 22 23 }
1 package demo1 2 { 3 /** 4 * ... 5 * @author titi 6 */ 7 public class Notification2 8 { 9 private var _email:IMsg; 10 11 public function Notification2() 12 { 13 14 } 15 16 17 public function PromotionalNotification():void 18 { 19 _email.Send(); 20 } 21 22 //提供供外部注入Set屬性 23 public function set email(value:IMsg):void 24 { 25 _email = value; 26 } 27 } 28 29 }
1 package demo1 2 { 3 /** 4 * ... 5 * @author titi 6 */ 7 public class Notification4 8 { 9 /** 10 * 公共變量提供抽象注入 11 */ 12 public var _email:IMsg; 13 14 public function Notification4() 15 { 16 17 } 18 19 20 public function PromotionalNotification():void 21 { 22 _email.Send(); 23 } 24 25 } 26 27 }
在Demo1中咱們用以下代碼來進行顯示的抽象注入:ide
1 package demo1 2 { 3 import flash.display.Sprite; 4 import flash.events.Event; 5 6 /** 7 * ... 8 * @author titi 9 */ 10 public class Main1 extends Sprite 11 { 12 13 public function Main1():void 14 { 15 if (stage) init(); 16 else addEventListener(Event.ADDED_TO_STAGE, init); 17 } 18 19 private function init(e:Event = null):void 20 { 21 removeEventListener(Event.ADDED_TO_STAGE, init); 22 // entry point 23 24 //構造依賴 25 var notificatoin:Notification = new Notification(new Mail("測試郵件1")); 26 notificatoin.PromotionalNotification(); 27 //屬性依賴 28 var notificatoin2:Notification2 = new Notification2(); 29 notificatoin2.email = new Mail("測試郵件2"); 30 notificatoin2.PromotionalNotification(); 31 //方法依賴 32 var notificatoin3:Notification3 = new Notification3(); 33 notificatoin3.PromotionalNotification(new Mail("測試郵件3")); 34 //變量依賴 35 var notificatoin4:Notification4 = new Notification4(); 36 notificatoin4._email=new Mail("測試郵件4") 37 notificatoin4.PromotionalNotification(); 38 } 39 40 } 41 42 }
執行後輸出以下:函數
1 Send Mail:測試郵件1 2 Send Mail:測試郵件2 3 Send Mail:測試郵件3 4 Send Mail:測試郵件4
上面2個demo大體說明了如何經過依賴注入對現有的邏輯進行解耦,接下來咱們用Swiftsuspenders2框架來快速實現上述的demo1做爲對Swiftsuspenders2框架的入門詳細參考以下demo2代碼。測試
1 package demo2 2 { 3 /** 4 * ... 5 * @author titi 6 */ 7 8 [Inject(name="email1")] 9 public class Notification 10 { 11 private var _email:IMsg; 12 public function Notification(email:IMsg) 13 { 14 _email = email; 15 } 16 17 18 public function PromotionalNotification():void 19 { 20 _email.Send(); 21 } 22 } 23 24 } 25 26 package demo2 27 { 28 /** 29 * ... 30 * @author titi 31 */ 32 public class Notification2 33 { 34 private var _email:IMsg; 35 36 public function Notification2() 37 { 38 39 } 40 41 42 public function PromotionalNotification():void 43 { 44 _email.Send(); 45 } 46 47 public function get email():IMsg 48 { 49 return _email; 50 } 51 52 [Inject(name="email2")] 53 public function set email(value:IMsg):void 54 { 55 _email = value; 56 } 57 } 58 59 } 60 package demo2 61 { 62 /** 63 * ... 64 * @author titi 65 */ 66 public class Notification3 67 { 68 private var _email:IMsg; 69 70 public function Notification3() 71 { 72 73 } 74 75 [Inject(name = "email3")] 76 public function inject(mail:IMsg):void 77 { 78 _email = mail; 79 } 80 81 public function PromotionalNotification():void 82 { 83 _email.Send(); 84 } 85 } 86 87 } 88 89 package demo2 90 { 91 /** 92 * ... 93 * @author titi 94 */ 95 public class Notification4 96 { 97 /** 98 * 公共變量提供抽象注入 99 */ 100 101 [Inject(name = "email4")] 102 public var _email:IMsg; 103 104 public function Notification4() 105 { 106 107 } 108 109 110 public function PromotionalNotification():void 111 { 112 _email.Send(); 113 } 114 115 } 116 117 } 118 119 package demo2 120 { 121 import flash.display.Sprite; 122 import flash.events.Event; 123 import org.swiftsuspenders.Injector; 124 125 /** 126 * ... 127 * @author titi 128 */ 129 public class Main2 extends Sprite 130 { 131 132 public function Main2():void 133 { 134 if (stage) init(); 135 else addEventListener(Event.ADDED_TO_STAGE, init); 136 } 137 138 private function init(e:Event = null):void 139 { 140 removeEventListener(Event.ADDED_TO_STAGE, init); 141 // entry point 142 143 //申明注入器 144 var injector:Injector = new Injector(); 145 146 injector.map(IMsg, "email1").toValue(new Mail("測試郵件1")); 147 injector.map(IMsg, "email2").toValue(new Mail("測試郵件2")); 148 injector.map(IMsg, "email3").toValue(new Mail("測試郵件3")); 149 injector.map(IMsg, "email4").toValue(new Mail("測試郵件4")); 150 151 (injector.instantiateUnmapped(Notification) as Notification).PromotionalNotification(); 152 (injector.instantiateUnmapped(Notification2) as Notification2).PromotionalNotification(); 153 (injector.instantiateUnmapped(Notification3) as Notification3).PromotionalNotification(); 154 (injector.instantiateUnmapped(Notification4) as Notification4).PromotionalNotification(); 155 } 156 157 } 158 159 }