http://wiki.jikexueyuan.com/project/android-roboguice/images/1.png" alt="" />
添加了對(duì)應(yīng) guice 和 roboguice 庫的引用之后,就可以開始編寫第一個(gè)使用 roboguice 的例子。
<application android:name=”GuiceApplication”
android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.GuiceDemo”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:id=”@+id/hello”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
</LinearLayout>
我們定義了一個(gè) TextView ,它的 id 為 hello.
假定這個(gè)應(yīng)用使用一個(gè) IGreetingService ,它有一個(gè)方法 getGreeting() 返回一個(gè)字元串,至于 IGreetingService 如何實(shí)現(xiàn),GuideDemo 不需要關(guān)心。
http://wiki.jikexueyuan.com/project/android-roboguice/images/2.png" alt="" />
Dependency injection 設(shè)計(jì)模式的一個(gè)核心原則為: Separate behavior from dependency resolution. 也就說將應(yīng)用需要實(shí)現(xiàn)的功能和其所依賴的服務(wù)或其它對(duì)象分離。 對(duì)本例來說 GuiceDemo 只要知道它依賴于 IGreetingService 服務(wù),至于 IGreetingService 有誰實(shí)現(xiàn)GuiceDemo 并不需要知道。
在 Roboguice 中使用 @Inject 來表示這種依賴關(guān)系。
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);
helloLabel.setText(greetingServce.getGreetings());
}
}
代碼中沒有創(chuàng)建 greetingServce 對(duì)象的代碼(如 new xxx()) 和為 helloLabel 賦值的代碼。這些值都可以 Roboguice 自動(dòng)創(chuàng)建和賦值注入(Inject) 到變數(shù)中。
為了說明問題,我們?cè)诖a中添加兩個(gè)對(duì) getGreetings 的實(shí)現(xiàn),一個(gè)為 HelloWorld, 一個(gè)為 HelloChina:
public class HelloChina implements IGreetingService{
@Override
public String getGreetings() {
return "Hello,China";
}
}
public class HelloWorld implements IGreetingService{
@Override
public String getGreetings() {
return "Hello,World";
}
}
項(xiàng)目中添加一個(gè) GreetingModule (從 AbstractAndroidModule 派生)重載 configure 方法:
public class GreetingModule extends AbstractAndroidModule{
@Override
protected void configure() {
bind(IGreetingService.class).to(HelloWorld.class);
//bind(IGreetingService.class).to(HelloChina.class);
}
}
將 IGreetingService 綁定到 HelloWorld 類。
然后在 GuiceApplication 的 addApplicationModules 添加上述模塊:
public class GuiceApplication extends RoboApplication {
protected void addApplicationModules(List<Module> modules) {
modules.add(new GreetingModule());
}
}
可以將 GreetingModule 綁定改為 HelloChina ,對(duì)比一下:
http://wiki.jikexueyuan.com/project/android-roboguice/images/3.png" alt="" />
通過改變 binding ,GuiceDemo 顯示了不同的結(jié)果,GuiceDemo 不依賴于具體的實(shí)現(xiàn),可以非常方便的改變介面的實(shí)現(xiàn)而無需更改 GuiceDemo的代碼。大大降低了類于類之間的藕合性。
后面將逐個(gè)介紹 Guice 和 RoboGuice 支持的 Binding 類型和用法(Guice) 以及與 android 平臺(tái)相關(guān)的 Dependency injection (RoboGuice)