Android RoboGuice User Guide (4): Linked Bindings

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.