A basic principle for application development is modularization, and the coupling between modules is minimized. Dependency injection is supported in Spring Framework and. Net platform CAB, SCSF and Prism (WPF, Silverlight) on the Java platform.
Dependency injection greatly reduces dependencies between classes. You can use annotation (Java) or SeviceDepdendcy (.. Net) describes the dependencies between classes, avoiding directly calling similar constructors or using Factory to participate in the required classes, thus reducing the coupling between classes or modules, to improve code reuse and enhance code maintainability.
Google Guice provides a lightweight Dependency injection framework on the Java platform and supports Android application development. This Guide describes how to use Google Guice on the Android platform.
To put it simply, Guice reduces the call of the new and Factory functions in Java code. You can regard Guice's @ Inject as a substitute for new. You may need to write some Factory methods to use Guice, but your code will not rely on these Factory methods to create instances. Using Guice to modify code makes code reuse easier for unit tests.
RoboGuice is a library developed based on Google Guice on the Android platform. It can greatly simplify the code for Android Application Development and some tedious and repetitive code. For example, you may need to use findViewById to search for a View in XML and forcibly convert it to the desired type. onCreate may have a lot of similar code. RoboGuice allows annotation to describe the relationship between views. The rest of the work is done by the roboGuice library. For example:
[Java]
Class AndroidWay extends Activity {
TextView name;
ImageView thumbnail;
LocationManager loc;
Drawable icon;
String myName;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Name = (TextView) findViewById (R. id. name );
Thumbnail = (ImageView) findViewById (R. id. thumbnail );
Loc = (LocationManager) getSystemService (Activity. LOCATION_SERVICE );
Icon = getResources (). getDrawable (R. drawable. icon );
MyName = getString (R. string. app_name );
Name. setText ("Hello," + myName );
}
}
If you use roboguice to write:
[Java]
Class RoboWay extends RoboActivity {
@ InjectView (R. id. name) TextView name;
@ InjectView (R. id. thumbnail) ImageView thumbnail;
@ InjectResource (R. drawable. icon) Drawable icon;
@ InjectResource (R. string. app_name) String myName;
@ Inject LocationManager loc;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Name. setText ("Hello," + myName );
}
}
You only need to use @ InjectView to describe the relationship between the view and the Id. RoboGuice automatically completes the remaining work, and the code is concise and easy to read. After introducing Google Guice, I will introduce how to use RoboGuice on the Android platform.
You only need to use @ InjectView to describe the relationship between the view and the Id. RoboGuice automatically completes the remaining work, and the code is concise and easy to read. After introducing Google Guice, I will introduce how to use RoboGuice on the Android platform. You only need to use @ InjectView to describe the relationship between the view and the Id. RoboGuice automatically completes the remaining work, and the code is concise and easy to read. After introducing Google Guice, I will introduce how to use RoboGuice on the Android platform. You only need to use @ InjectView to describe the relationship between the view and the Id. RoboGuice automatically completes the remaining work, and the code is concise and easy to read. After introducing Google Guice, I will introduce how to use RoboGuice on the Android platform. You only need to use @ InjectView to describe the relationship between the view and the Id. RoboGuice automatically completes the remaining work, and the code is concise and easy to read. After introducing Google Guice, I will introduce how to use RoboGuice on the Android platform. You only need to use @ InjectView to describe the relationship between the view and the Id. RoboGuice automatically completes the remaining work, and the code is concise and easy to read. After introducing Google Guice, I will introduce how to use RoboGuice on the Android platform.