Android RoboGuice开源库 简化程序开发

在开发应用时一个基本原则是模块化,并且近最大可能性地降低模块 之间的耦合性。

   Dependency injection 大大降低了类之间的依赖性,可以通过annotation (Java)或是SeviceDepdendcy (.Net) 描述类之间的依赖性,避免了直接调用类似的构造函数或是使用Factory来参加所需的类,从而降低类或模块之间的耦合性,以提高代码重用并增强代码的可维护性。

   Google Guice提供了Java平台上一个轻量级的 Dependency injection 框架,并可以支持开发Android应用。本指南将使用Android平台来说明Google Guice的用法。
简单的来说:Guice 降低了Java代码中使用 new 和 Factory函数的调用。可以把Guice 的@Inject 看作 new 的一个替代品。使用Guice可能还需要写一些Factory方法,但你的代码不会依赖这些Factory方法来创建实例。 使用Guice 修改代码,单元测试已经代码重用变得更容易。


   RoboGuice 为Android平台上基于Google Guice开发的一个库,可以大大简化Android应用开发的代码和一些繁琐重复的代码。比如代码中可能需要大量使用findViewById在XML中查找一个View,并将其强制转换到所需类型,onCreate 中可能有大量的类似代码。RoboGuice 允许使用annotation 的方式来描述id于View之间的关系,其余的工作由roboGuice库来完成


例如:

package com.example.roboguice;

import java.util.List;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Application;
import android.app.KeyguardManager;
import android.app.NotificationManager;
import android.app.SearchManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.roboguice.annotation.PO;
import com.example.roboguice.imp.PhoneStyleTwo;
import com.example.roboguice.service.PhoneService;
import com.google.inject.Inject;
import com.google.inject.name.Named;

public class RoboDemoActivity extends RoboActivity {
	@Inject InjectDemo demo;
	@InjectView(R.id.tv_robo_lable) TextView t;
	@InjectResource(R.string.app_name) String myName;
	@InjectResource(R.drawable.ic_launcher) Drawable icon;
	@InjectView(R.id.img_robo_inject) ImageView testImg;
	
	@Inject ContentResolver contentResolver;
	@Inject AssetManager assetManager;
	@Inject Resources resources;
	@Inject LocationManager locationManager;
	@Inject WindowManager windowManager;
	@Inject LayoutInflater layoutInflater;
	@Inject ActivityManager activityManager;
	@Inject PowerManager powerManager;
	@Inject AlarmManager alarmManager;
	@Inject NotificationManager notificationManager;
	@Inject KeyguardManager keyguardManager;
	@Inject SearchManager searchManager;
	@Inject Vibrator vibrator;
	@Inject ConnectivityManager connectivityManager;
	@Inject WifiManager wifiManager;
	@Inject InputMethodManager inputMethodManager;
	@Inject SensorManager sensorManager;
	@Inject Application application;
//	@Inject PhoneService phoneService;
//	@Inject @PO PhoneService ps1;
//	@Inject @PW PhoneService ps2;
//	@Inject @Named("phOne") PhoneService ps1;
//	@Inject @Named("phTwo") PhoneService ps2;
	@Inject Context context;
	@Inject @Named("MyPhoneService") PhoneService myService;
	@Inject @PO PhoneService ms1;
	@Inject PhoneStyleTwo two;
	@Inject List<String> list;
	@Inject ContextWraper wraper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_robo);
		
//		System.out.println(list.getClass().getSimpleName());
//		myService.call();
//		ms1.call();
//		two.call();
//		isNull(context);
//		String s=t.getText().toString();
//		v(s);
//		v(myName);
//		testImg.setImageDrawable(icon);
//		vNull();
		t.setText("jingwen");
	}
	void v(Object o){
		System.out.println("o:"+o.toString());
	}
	void vNull(){
		isNull(contentResolver);
		isNull(sensorManager);
		isNull(inputMethodManager);
		isNull(wifiManager);
		isNull(connectivityManager);
		isNull(vibrator);
		isNull(searchManager);
		isNull(keyguardManager);
		isNull(notificationManager);
		isNull(alarmManager);
		isNull(powerManager);
		isNull(layoutInflater);
		isNull(windowManager);
		isNull(resources);
		isNull(assetManager);
		isNull(application);
	}
	void isNull(Object o)
	{
		System.out.println("0==null:"+(o==null));
	}
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值