One of the most commonly used Roboguice Bindings is Linked Bindings, which maps a type to its implementation. Here we use the class in the two-dimensional graphics library of the Web tracking bee as an example.
Use the following classes of IShape, Rectangle, MyRectangle, and MySquare. The Inheritance relationships are shown in:
The following code maps IShape to 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 );
}
}
In this case, if you use injector. getInstance (IShape. class) or injector to encounter a location dependent on IShape, it uses MyRectangle. You can map a type to any of its subclasses or all classes that implement this type of interface. You can also map a real class (non-Interface) to its subclass, such
Bind (MyRectangle. class). to (MySquare. class );
The following example uses @ Inject to apply IShape.
[Java] public class extends bindingsdemo 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
* 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 extends bindingsdemo 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
* 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 );
}
}
Use bind (IShape. class ). to (MyRectangle. class). To simplify the problem, MyRectangle and MySquare are defined with a constructor without parameters. Injection of constructor classes with parameters is described later.
[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 allows connections, such
[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 );
}
}
When IShape is required, Injector returns the instance of MySquare, IShape-> MyRectangle-> MySquare
Download this example: http://www.bkjia.com/uploadfile/2012/0504/20120504094728917.zip
Excerpted from the mobile app