Android roboguice 開源架構使用

來源:互聯網
上載者:User

標籤:android   開源   roboguice   

Android roboguice 應用

開源的roboguice是一個依賴注入架構,如果你用過Spring 應該知道其好處。
減少程式員的負擔,代碼變的更加簡潔。

地址:https://github.com/roboguice/roboguice

工具用的是Android Studio 因為Gradle可以自動添加第三方庫。

Gradle部分內容:

dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    compile ‘com.android.support:appcompat-v7:22.1.1‘    compile ‘org.roboguice:roboguice:2.0‘    compile (‘com.google.inject.extensions:guice-assistedinject:3.0‘){        exclude group: ‘com.google.inject‘    }}

版本用的是2.0,官方版本最新是3.0

示範基本用法

import android.os.Bundle;import android.view.View;import android.widget.TextView;import com.google.inject.Binder;import com.google.inject.ImplementedBy;import com.google.inject.Inject;import com.google.inject.Module;import roboguice.activity.RoboActivity;import roboguice.inject.ContentView;import roboguice.inject.InjectView;//會調用setContentView方法@ContentView(R.layout.activity_main)public class MainActivity extends RoboActivity {    @InjectView(R.id.text1)    private TextView textView; //代替findViewById 不用自己強轉類型    @InjectView(R.id.btn1)    private TextView button;    @Inject    private MyCache<String,String> myCache;//單列    @Inject    private IService myService;//介面注入    @Inject    private Toaster toaster;//Toast簡單的用法封裝    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                textView.setText("Test is ok." + myCache);                toaster.showMsg("hey, I like to talk to you" );                myService.login("1","1");            }        });    }}

Toaster.java

import android.app.Activity;import android.widget.Toast;import com.google.inject.Inject;import roboguice.inject.ContextSingleton;/** * Created by gaofeng on 15-5-13. *///可以擷取當前Activity中的上下文@ContextSingletonpublic class Toaster {    @Inject    private Activity activity;    //不用傳上下文了    public void showMsg(String msg) {//不用自己傳入Context,Activity內容物件        Toast.makeText(activity,msg, Toast.LENGTH_SHORT).show();    }}MyCache.javaimport com.google.inject.Singleton;import java.util.HashMap;import java.util.Map;/** * Created by gaofeng on 15-5-13. *///Cache通常是單列的@Singletonpublic class MyCache<K,V> { //這裡只是示範,沒有寫太複雜    Map<K,V> map = new HashMap<K,V>();    public void put(K k,V t) {       map.put(k,t);    }    public V get(K k) {        return map.get(k);    }}

下面是介面執行個體化的代碼,做java ssh web開發一般都是這個樣子

public interface IService { //簡單業務介面    public void login(String u,String passwd);}import roboguice.inject.ContextSingleton;public class MyService implements  IService { @Inject    private MyCache<String,String> myCache; //和上一次的對象是同一個,單列對象    private Context context;    public MyService() {    }    public MyService(Context context) {        this.context = context;    }    @Override    public void login(String u, String passwd) {        Log.d("", "Service Login>>>>" + context + " " + myCache);         //業務代碼xxxx    }}

實現上面的介面實列化 需要配置註冊

import android.content.Context;import android.util.Log;import com.google.inject.AbstractModule;/** * Created by gaofeng on 15-5-13. */public class CustomModule2 extends AbstractModule {    private Context context;//系統會自己傳入上下文    public CustomModule2(Context context) {        this.context = context;    }    @Override    protected void configure() {       Log.d("", "CustomModule2>>>>>>>");       bind(IService.class).toInstance(new MyService(context));//註冊    }}

還需要配置在 res/values/ 中添加檔案roboguice.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="roboguice_modules">        <item>com.example.gaofeng.roboguice_demo.CustomModule2</item>    </string-array></resources>

掌握基本的用法就能減少大量的代碼。
不用自己去new對象,單列也很好弄,不用自己定義static對象

參考: https://github.com/roboguice/roboguice/wiki

Android roboguice 開源架構使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.