灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:1809回复:0

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

楼主#
更多 发布于:2012-09-06 13:56

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




喜欢0 评分0
游客

返回顶部