Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (1)

时间:2023-03-08 16:16:03

Swiftsuspenders2 是一个基于元数据(metadata)的IOC(控制反转,inversion of control)的AS3的解决方案。反转控制又被称依赖注射(Dependency Injection),也就是将依赖先剥离,然后在适当时候再注射进入。它是一种降低耦合度的程序设计模式其核心原则是高层模块不应依赖于低层模块,他们都应该依赖于抽象。抽象不依赖细节,细节依赖抽象,它通过将对象的创建过程解耦出来来降低对象间的依赖关系。IOC的设计目标是不直接创建对象,但是描述对象的创建方式,在代码中不直接链接对象和服务,通过配置的方式描述哪一种组件需要哪一些服务,然后在ioc容器中负责自动将对象服务和需要使用他们的地方进行链接。下面我们通过一个简单的例子,先了解一下如何利用依赖注入进行解耦。

关于依赖注入

当我们需要进行一个发送消息或者邮件的流程时 我们可能会写如下代码(demo0):

 package  demo0
{
/**
* ...
* @author titi
*/
public class Mail
{
public var Title:String; public function Send():void
{
trace(" Send Mail:"+Title);
} public function Mail(title:String)
{
Title = title;
} }   }
  
  
package demo0
{
/**
* ...
* @author titi
*/
public class Notification
{
private var _email:Mail;
public function Notification()
{
_email = new Mail("测试邮件1");
} public function PromotionalNotification():void
{
_email.Send();
}
}   }
  
  
package demo0
{
import flash.display.Sprite;
import flash.events.Event; /**
* ...
* @author titi
*/
public class Main0 extends Sprite
{ public function Main0():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
} private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var notificatoin:Notification = new Notification();
notificatoin.PromotionalNotification();
} }   }

这里Mail和其控制器Notification 是直接关联的,控制器依赖Mail对象。通过抽象Mail 可以进行解耦降低Notification和Mail的耦合度。

 package demo1
{ /**
* ...
* @author titi
*/
public interface IMsg
{
function set Title(value:String):void; function get Title():String; function Send():void;
} }
  
package demo1
{
/**
* ...
* @author titi
*/
public class Mail implements IMsg
{
public var _Title:String; public function Send():void
{
trace(" Send Mail:"+Title);
} public function Mail(title:String)
{
_Title = title;
} /* INTERFACE demo1.IMsg */ public function set Title(value:String):void
{
_Title = value;
} public function get Title():String
{
return _Title;
} } } package demo1
{
/**
* ...
* @author titi
*/
public class Notification
{
private var _email:IMsg; public function PromotionalNotification():void
{
_email.Send();
}
} }

抽象后 可以通过四种方式(注入点) 在Notification 中注入IMail对应的具体的Mail 分别是

1、构造函数注入

 package  demo1
{
/**
* ...
* @author titi
*/
public class Notification
{
private var _email:IMsg;
public function Notification(email:IMsg)
{
_email = email;
} public function PromotionalNotification():void
{
_email.Send();
}
} }

2、方法注入

 package  demo1
{
/**
* ...
* @author titi
*/
public class Notification3
{
private var _email:IMsg; public function Notification3()
{ } public function PromotionalNotification(mail:IMsg):void
{
mail.Send();
}
} }

3、属性注入

 package  demo1
{
/**
* ...
* @author titi
*/
public class Notification2
{
private var _email:IMsg; public function Notification2()
{ } public function PromotionalNotification():void
{
_email.Send();
} //提供供外部注入Set属性
public function set email(value:IMsg):void
{
_email = value;
}
} }

4、变量注入

 package  demo1
{
/**
* ...
* @author titi
*/
public class Notification4
{
/**
* 公共变量提供抽象注入
*/
public var _email:IMsg; public function Notification4()
{ } public function PromotionalNotification():void
{
_email.Send();
} } }

在Demo1中我们用如下代码来进行显示的抽象注入:

 package demo1
{
import flash.display.Sprite;
import flash.events.Event; /**
* ...
* @author titi
*/
public class Main1 extends Sprite
{ public function Main1():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
} private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point //构造依赖
var notificatoin:Notification = new Notification(new Mail("测试邮件1"));
notificatoin.PromotionalNotification();
//属性依赖
var notificatoin2:Notification2 = new Notification2();
notificatoin2.email = new Mail("测试邮件2");
notificatoin2.PromotionalNotification();
//方法依赖
var notificatoin3:Notification3 = new Notification3();
notificatoin3.PromotionalNotification(new Mail("测试邮件3"));
//变量依赖
var notificatoin4:Notification4 = new Notification4();
notificatoin4._email=new Mail("测试邮件4")
notificatoin4.PromotionalNotification();
} } }

执行后输出如下:

  Send Mail:测试邮件1
Send Mail:测试邮件2
Send Mail:测试邮件3
Send Mail:测试邮件4

上面2个demo大致说明了如何通过依赖注入对现有的逻辑进行解耦,接下来我们用Swiftsuspenders2框架来快速实现上述的demo1作为对Swiftsuspenders2框架的入门详细参考如下demo2代码。

 package  demo2
{
/**
* ...
* @author titi
*/ [Inject(name="email1")]
public class Notification
{
private var _email:IMsg;
public function Notification(email:IMsg)
{
_email = email;
} public function PromotionalNotification():void
{
_email.Send();
}
} } package demo2
{
/**
* ...
* @author titi
*/
public class Notification2
{
private var _email:IMsg; public function Notification2()
{ } public function PromotionalNotification():void
{
_email.Send();
} public function get email():IMsg
{
return _email;
} [Inject(name="email2")]
public function set email(value:IMsg):void
{
_email = value;
}
} }
package demo2
{
/**
* ...
* @author titi
*/
public class Notification3
{
private var _email:IMsg; public function Notification3()
{ } [Inject(name = "email3")]
public function inject(mail:IMsg):void
{
_email = mail;
} public function PromotionalNotification():void
{
_email.Send();
}
} } package demo2
{
/**
* ...
* @author titi
*/
public class Notification4
{
/**
* 公共变量提供抽象注入
*/ [Inject(name = "email4")]
public var _email:IMsg; public function Notification4()
{ } public function PromotionalNotification():void
{
_email.Send();
} } } package demo2
{
import flash.display.Sprite;
import flash.events.Event;
import org.swiftsuspenders.Injector; /**
* ...
* @author titi
*/
public class Main2 extends Sprite
{ public function Main2():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
} private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point //申明注入器
var injector:Injector = new Injector(); injector.map(IMsg, "email1").toValue(new Mail("测试邮件1"));
injector.map(IMsg, "email2").toValue(new Mail("测试邮件2"));
injector.map(IMsg, "email3").toValue(new Mail("测试邮件3"));
injector.map(IMsg, "email4").toValue(new Mail("测试邮件4")); (injector.instantiateUnmapped(Notification) as Notification).PromotionalNotification();
(injector.instantiateUnmapped(Notification2) as Notification2).PromotionalNotification();
(injector.instantiateUnmapped(Notification3) as Notification3).PromotionalNotification();
(injector.instantiateUnmapped(Notification4) as Notification4).PromotionalNotification();
} } }