Android RoboGuice 使用指南2

转载于:http://www.imobilebbs.com/wordpress/archives/2480


Android RoboGuice 使用指南(8):Provider Bindings

如果@Provides方法很复杂的话,可以将这些代码移动到单独的类中。这个类需要实现Guice的Provider 接口,该接口定义如下:

1 publicinterface Provider<T> {
2 T get();
3 }

为一个generic 接口。

本例我们定义一个PathProvider,用于返回一个Path对象:

1 publicclass PathProvider implementsProvider<Path>{
2  
3 privateString pathdata
4 ="M 60 20 Q -40 70 60 120 Q 160 70 60 20 z";
5 @Override
6 publicPath get() {
7 returnPath.fromString(pathdata);
8 }
9  
10 }

然后在Module中定义从Path类到Provider的绑定:

1 bind(Path.class).toProvider(PathProvider.class);

然后使用绘制这个Path:

1 publicclass ProviderBindingsDemo extendsGraphics2DActivity{
2  
3 @InjectPath path;
4  
5 protectedvoid drawImage(){
6  
7 AffineTransform mat1;
8  
9 // Colors
10 Color redColor = newColor(0x96ff0000,true);
11 Color greenColor = newColor(0xff00ff00);
12 Color blueColor = newColor(0x750000ff,true);
13  
14 mat1 = newAffineTransform();
15 mat1.translate(30,40);
16 mat1.rotate(-30* Math.PI / 180.0);
17  
18 // Clear the canvas with white color.
19 graphics2D.clear(Color.WHITE);
20  
21 graphics2D.setAffineTransform(newAffineTransform());
22 SolidBrush brush = newSolidBrush(greenColor);
23 graphics2D.fill(brush, path);
24 graphics2D.setAffineTransform(mat1);
25  
26 brush = newSolidBrush(blueColor);
27 com.mapdigit.drawing.Pen pen
28 =newcom.mapdigit.drawing.Pen(redColor, 5);
29 graphics2D.setPenAndBrush(pen, brush);
30 graphics2D.draw(null, path);
31 graphics2D.fill(null, path);
32  
33 }
34  
35 }


本例下载

Android RoboGuice 使用指南(9):Untargetted Bindings

在创建Bindings时,也可以不给出绑定的目标,通常用于含有@ImplementedBy 和@ProvidedBy (后面介绍)的实类(Concrete classes 或type)。 Untargeted bindings 目的是通知Injector 某个类类型,从而Injector可以预先准备某个依赖。Untargetted Bindings不含to语句。

例如:

1 bind(MyConcreteClass.class);
2 bind(AnotherConcreteClass.class).in(Singleton.class);

但如果此时需要同时使用binding annotations 时,需要为绑定添加目标,即使是绑定到同一个实类,如:

1 bind(MyConcreteClass.class)
2 .annotatedWith(Names.named("foo"))
3 .to(MyConcreteClass.class);
4 bind(AnotherConcreteClass.class)
5 .annotatedWith(Names.named("foo"))
6 .to(AnotherConcreteClass.class)
7 .in(Singleton.class);

Android RoboGuice 使用指南(10): Just-in-time Bindings

Injector 通过检查bindings 定义来创建某个类型的实例对象。定义在Module中的绑定称为“明确声明绑定(Explicit bindings”。Injector 会首先使用带有Explicit Bindings为某个类型创建实例对象。 当但某个类型没有明确定义绑定时,Injector 试图构造“即时绑定(Just-in-time Bindings),JIT Bindings 也成为隐含绑定(implicit bindings).

Eligible Constructor

Injector 通过使用类的injectable constructor 来创建该类的实例对象。injectable constructor 可以为该类定义的public 不带参数的构造函数或是带有@Injector 标记的构造函数。

比如Android RoboGuice 使用指南(4):Linked Bindings中MyRectangle的无参数构造函数:

1 publicclass MyRectangle extendsRectangle{
2 publicMyRectangle(){
3 super(50,50,100,120);
4 }
5 ...
6 }

Android RoboGuice 使用指南(6):Instance Bindings 定义的含@Injector 标记的构造函数:

1 publicclass MySquare extendsMyRectangle {
2 @Injectpublic MySquare(@Named("width")intwidth){
3 super(width,width);
4 }
5 }

@ImplementedBy

该标记通知Injector某个类型的缺省实现,其功能和Linked Bindings 类似,例如:

1 @ImplementedBy(PayPalCreditCardProcessor.class)
2 publicinterface CreditCardProcessor {
3 ChargeResult charge(String amount, CreditCard creditCard)
4 throwsUnreachableException; }

1 bind(CreditCardProcessor.class)
2 .to(PayPalCreditCardProcessor.class);

等效。 如果某个类型同时含有@ImplementedBy 和bind 定义,将优先使用bind 中的定义。

注: @ImplementedBy 定义了从Interface到实现的依赖,一般不建议使用。

@ProvidedBy

@ProvidedBy 通知Injector 某个类型使用那个缺省Provider来创建实例对象,例如:

1 @ProvidedBy(DatabaseTransactionLogProvider.class)
2 publicinterface TransactionLog {
3 voidlogConnectException(UnreachableException e);
4 voidlogChargeResult(ChargeResult result);
5 }

和下面Binding等效:

1 bind(TransactionLog.class)
2 .toProvider(DatabaseTransactionLogProvider.class);

和@ImplementedBy 一样,如果同时定义了@ProvidedBy和bind,模块中定义的bind 优先

Android RoboGuice 使用指南(11): Scopes

缺省情况下,Guice每次都创建类的一个新的实例对象给需要该类实例的地方。可以使用Scopes来修改这个缺省行为,Scope允许在一定范围内重用类实例。Roboguice中常用的有两种:

  • @Singleton 整个Application生命周期中使用同一实例对象
  • @ContextScoped 同一个Context(如Activity)中共享某一实例对象。

使用Scope 的方法为使用相应的标记,如:

1 @Singleton
2 publicclass InMemoryTransactionLog implementsTransactionLog {
3 // everything here should be threadsafe!
4 }

或者在Module中使用bind 语句:

1 bind(TransactionLog.class)
2 .to(InMemoryTransactionLog.class)
3 .in(Singleton.class);

如果使用@Provides,可以有:

1 @Provides@Singleton
2 TransactionLog provideTransactionLog() {
3 ...
4 }

如果某个类型使用某个你不想使用的Scope标记,可以将其绑定到Scopes.NO_SCOPE取消这个Scope定义。

Android RoboGuice 使用指南(12):如何绑定generic类型

如果需要注入某个参数化类型,比如List<String>:

1 classExample {
2 @Inject
3 voidsetList(List<String> list) {
4 ...
5 }
6 }

可以使用TypeLiteral 来创建这个绑定。TypeLiteral 为一特殊类型可以用于表示参数化类型。

1 @Overridepublic void configure() {
2 bind(newTypeLiteral<List<String>>() {})
3 .toInstance(newArrayList<String>()); }

或者使用@Provides 方法:

1 @ProvidesList<String> providesListOfString() {
2 returnnew ArrayList<String>();
3 }

到目前为止,基本介绍了Google Guice 的用法,上面用法也适用于Java SE,Java EE平台,更详细的可以参见英文文档 ,后面接着介绍和Android平台相关的Dependency Injection (Roboguice)的用法。

Android RoboGuice 使用指南(13):RoboGuice 功能描述

前面在Android RoboGuice 使用指南(1):概述 对应Roboguice做了简要的介绍,之后介绍了Google Guice的基本用法,Roboguice是基本Android和Google Guice开发的适用于Android平台的Dependency Injection 开发包,下图为使用Roboguice开发应用的基本框图:

Android应用程序可以直接使用Google Guice来为普通类进行注入操作,而对和Android平台相关的类如Activity,Context,Service,View等可以使用Roboguice 进行注入操作。

在例Android RoboGuice 使用指南(2):第一个例子Hello World 介绍了使用RoboGuice开发的步骤,原先从Activity派生的类一般需要改成从RoboActivity派生,并添加从RoboApplication派生的类作为Application应用的Application类,详细的对应表如下:

RoboGuice支持的标记如下:

  • @ContextScoped : 表示Scope为Context 范围 Android RoboGuice 使用指南(11): Scopes
  • @InjectExtra : Intent的getExtra 的注入标记
  • @InjectPreference: 注入Preference
  • @InjectResource: 注入Resource,如drawable, icon 等
  • @InjectView: 注入View
  • @Inject: Guice标记,可以注入Android平台支持的各种服务,比如LocationManager等。
  • @SharedPreferencesName: SharedPreferences 名称等

此外,RoboGuice还提供了简单的消息publish/subscribe 机制,以及可以支持Dependency Injection的RoboThread, RoboAsyncTask ,RoboLooperThread 等,将在后面的文章详细说明。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值