Android RoboGuice User Guide (3): Bindings Overview

Source: Internet
Author: User

The dependency between classes in an application may be very complex. To create a class instance, you must first create an example of the class on which the class depends, and then create an instance of the dependent class, these classes may depend on other classes, and so on. Therefore, when creating a class instance, you are creating an Object Graph ).
Creating an Object Graph manually is a complex and error-prone process, and it is difficult to test the code. Guice or Roboguice can help you create an Object Graph, the task is to configure the dependency between classes.
The module is the basic construction block for Guice to construct the Object Graph. The construction of the object Graph in Guice is completed by a class called "Injector.
Guice is a subclass of AbstractMoudule in the module, while RoboGuice is a subclass of AbstractAndroidModule in the module. RoboGuice uses Injector to create the dependent object, and Injector constructs the relationship diagram between classes by referring to the Bindings defined in Module.
For example, if you are familiar with make file or other Build systems (such as wix ). You can use makefile to define the source code files on which the object to be compiled depends. These source codes may depend on other libraries or header files. The dependencies defined in makefile correspond to the bindings defined in the module in Roboguice.
When you use make to compile a Target program, make will view the dependencies in makefile and compile the dependent objects in sequence until the Target is finally compiled. Corresponds to Roboguide (Guice) to create an object for Injector. It will first create those dependent objects according to the defined Bindings until the required objects are created.
In the HelloWorld example, we do not see direct use of Injector, because RoboGuice calls Injector for us to create an IGreetingService object.
In some cases, if you want to use Injector directly, you can use getInjector () of RoboActivity ().
For example, modify GuiceDemo, remove @ Inject IGreetingService greetingServce, and use the getInstance of Injector to create the IGreetingService instance.
[Java]
Public class GuiceDemo extends RoboActivity {

@ InjectView (R. id. hello) TextView helloLabel;
// @ Inject IGreetingService greetingServce;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Injector injector = getInjector ();
IGreetingService greetingServce
= Injector. getInstance (IGreetingService. class );
HelloLabel. setText (greetingServce. getGreetings ());
}

}

The Module is bound to HelloChina.
[Java]
Public class GreetingModule extends AbstractAndroidModule {

@ Override
Protected void configure (){
// Bind (IGreetingService. class). to (HelloWorld. class );
Bind (IGreetingService. class). to (HelloChina. class );

}

}





The work of Injector is to construct the Object Graph. When you call getInstance to construct an Object of a certain type, Injector will automatically create instances of the required classes based on the dependencies between classes.
The method for defining dependencies between classes is to extend the AbstractAndroidModule and reload its configure method. Define various Bindings in the configure method. These methods also perform type detection. If the type used is incorrect, the compiler will give an error.
Bindings can be bound to the following types:
Linked bindings
Instance bindings
@ Provider methods
Provider bindings
Constructor bindings
Untargetted bindings
Built-in bindings
Just-in-time bindings
Providers, etc.
Later, we will introduce these bindings one by one. These bindings are common and have little relevance with the Android platform. They can be used on both Java EE and Java SE platforms. RoboGuice provides dependency injector related to the Android platform, we will also provide a detailed description later.


Excerpted from the mobile app

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.