Android RoboGuice 使用指南 2 第一個例子Hello World
首先介紹一下如果將Guice 和RoboGuice 的庫新增到專案中。
- 建立一個新Android專案,比如GuiceDemo,目標平臺Android1.5以上。
- 一般可以在該專案下新增一個lib目錄,將兩個jar檔案拷到lib目錄下,然後通過: Project > Properties > Java Build Path > Libraries > Add External JARs
添加了對應guice 和roboguice庫的引用之後,就可以開始編寫第一個使用roboguice 的例子。
使用roboguice 的步驟:
1. 建立一個RoboApplication 的子類GuiceApplication,GuiceApplication為Appliacation的子類,因此需要修改AndroidManifest.xml,將Application 的name 指向這個類。可以參見
<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>
2. 在這個簡單的例子中,它使用的Layout 定義如下:
<?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”><TextViewandroid:id=”@+id/hello”android:layout_width=”fill_parent”android:layout_height=”wrap_content”android:text=”@string/hello”/>< /LinearLayout>
我們定義了一個TextView ,它的id為hello.
假定這個應用使用一個IGreetingService ,它有一個方法getGreeting() 返回一個字串,至於IGreetingService 如何實現,GuideDemo 不需要關心。
Dependency injection 設計模式的一個核心原則為: Separate behavior from dependency resolution. 也就說將應用需要實現的功能和其所依賴的服務或其它物件分離。 對本例來說GuiceDemo只要知道它依賴於IGreetingService 服務,至於IGreetingService有誰實現GuiceDemo並不需要知道。
在Roboguice 中使用@Inject 來表示這種依賴關係。
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()); }}
使用RoboGuice 的Activity需要從RoboActivity派生(RoboActivity為Activity的子類).使用@Inject標註greetingServce依賴於IGreetingService服務使用@InjectView表示helloLabel 依賴於R.id.hello (XML)程式碼中沒有建立greetingServce 物件的程式碼(如 new xxx()) 和為helloLabel 賦值的程式碼。這些值都可以Roboguice 自動建立和賦值注入(Inject)到變數中。
為了說明問題,我們在程式碼中新增兩個對getGreetings的實現,一個為HelloWorld, 一個為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"; } }
3. 到這裡,你可能有些困惑,RoboGuice怎麼知道使用那個類(HelloWorld或是HelloChina)為GuiceDemo中的greetingServce 賦值呢?這是通過在Module 中定義binding 來實現的。
在專案中新增一個GreetingModule (從AbstractAndroidModule 派生)過載configure方法:
public class GreetingModule extends AbstractAndroidModule{ @Overrideprotected 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 ,對比一下:
通過改變binding ,GuiceDemo 顯示了不同的結果,GuiceDemo不依賴於具體的實現,可以非常方便的改變介面的實現而無需更改GuiceDemo的程式碼。大大降低了類於類之間的耦合性。
後面將逐個介紹Guice和RoboGuice支援的Binding型別和用法(Guice)以及與android 平臺相關的Dependency injection (RoboGuice)