Android RoboGuice使用指南(4):Linked Bindings

 Roboguice 中最常用的一種綁定為Linked Bindings,將某個類型映射到其實現。這裡我們使用引路蜂二維圖形庫中的類為例。
使用下面幾個類IShape, Rectangle, MyRectangle, MySquare, 其繼承關系如下圖所示:

下面代碼將IShape 映射到MyRectangle
 

public class Graphics2DModule extends AbstractAndroidModule{ 
  
 @Override 
 protected void configure() { 
  
 bind(IShape.class).to(MyRectangle.class); 
  
 } 

public class Graphics2DModule extends AbstractAndroidModule{
 
 @Override
 protected void configure() {
 
 bind(IShape.class).to(MyRectangle.class);
 
 }
}
此時,如果使用injector.getInstance(IShape.class) 或是injector 碰到依賴於IShape地方時,它將使用MyRectangle。可以將類型映射到它任意子類或是實現瞭該類型接口的所有類。也可以將一個實類(非接口)映射到其子類,如

bind(MyRectangle.class).to(MySquare.class);

下面例子使用@Inject 應用IShape.

[java] public class LinkedBindingsDemo extends Graphics2DActivity{ 
  
@Inject IShape  shape; 
  
protected void drawImage(){ 
  
/**
* The semi-opaque blue color in
* the ARGB space (alpha is 0x78)
*/ 
Color blueColor = new Color(0x780000ff,true); 
/**
* The semi-opaque yellow color in the
* ARGB space ( alpha is 0x78)
*/ 
Color yellowColor = new Color(0x78ffff00,true); 
  
/**
* The dash array
*/ 
int dashArray[] = { 20 ,8 }; 
graphics2D.clear(Color.WHITE); 
graphics2D.Reset(); 
Pen pen=new Pen(yellowColor,10,Pen.CAP_BUTT, 
Pen.JOIN_MITER,dashArray,0); 
SolidBrush brush=new SolidBrush(blueColor); 
graphics2D.setPenAndBrush(pen,brush); 
graphics2D.fill(null,shape); 
graphics2D.draw(null,shape); 
  

  

public class LinkedBindingsDemo extends Graphics2DActivity{
 
@Inject IShape  shape;
 
protected void drawImage(){
 
/**
* The semi-opaque blue color in
* the ARGB space (alpha is 0x78)
*/
Color blueColor = new Color(0x780000ff,true);
/**
* The semi-opaque yellow color in the
* ARGB space ( alpha is 0x78)
*/
Color yellowColor = new Color(0x78ffff00,true);
 
/**
* The dash array
*/
int dashArray[] = { 20 ,8 };
graphics2D.clear(Color.WHITE);
graphics2D.Reset();
Pen pen=new Pen(yellowColor,10,Pen.CAP_BUTT,
Pen.JOIN_MITER,dashArray,0);
SolidBrush brush=new SolidBrush(blueColor);
graphics2D.setPenAndBrush(pen,brush);
graphics2D.fill(null,shape);
graphics2D.draw(null,shape);
 
}
 
}
使用bind(IShape.class).to(MyRectangle.class),為瞭簡化問題,這裡定義瞭MyRectangle和MySquare都帶有一個不帶參數的構造函數,註入具有帶參數的構造函數類用法在後面有介紹。

[java] public class MyRectangle extends Rectangle{ 
 public MyRectangle(){ 
 super(50,50,100,120); 
 } 
  
 public MyRectangle(int width, int height){ 
 super(50,50,width,height); 
 } 

… 
public class MySquare extends MyRectangle { 
  
 public MySquare(){ 
 super(100,100); 
 } 
  
 public MySquare(int width){ 
 super(width,width); 
 } 
  

Linked bindings 允許鏈接,例如

[java] public class Graphics2DModule extends AbstractAndroidModule{ 
  
 @Override 
 protected void configure() { 
 bind(IShape.class).to(MyRectangle.class); 
 bind(MyRectangle.class).to(MySquare.class); 
  
 } 

public class Graphics2DModule extends AbstractAndroidModule{
 
 @Override
 protected void configure() {
 bind(IShape.class).to(MyRectangle.class);
 bind(MyRectangle.class).to(MySquare.class);
 
 }
}

 

此時當需要IShape 時,Injector返回MySquare 的實例, IShape->MyRectangle->MySquare

 

本例下載:http://up.aiwalls.com/2012/0504/20120504094728917.zip

摘自 引路蜂移動軟件
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。