用匕首改造班级的HashMap [英] HashMap of Retrofit Classes with Dagger

查看:216
本文介绍了用匕首改造班级的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点新依赖注入,我有一个疑问。
在我的应用我有一个HashMap来存储改造内置类(如高速缓存),但我现在搬到DI与匕首,我想知道我怎么能实现相同的行为。

I'm kinda new to Dependency Injection and I have a doubt. In my app I have a HashMap to store built classes (like a cache) for RetroFit, but now I'm moving to DI with Dagger and I'd like to know how can I achieve the same behaviour.

我的code:

private Map<String, Object> restInstances;
public <T> T getRestClient(Class<T> clazz) {
        T client = null;

        if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
            return client;
        }

        client = restAdapter.create(clazz);
        restInstances.put(clazz.getCanonicalName(), client);
        return client;
    }

与DI启动后,我的模块类:

After starting with DI, my "module" class:

 @Provides @Singleton
    public JobManager providesJobManager(){
        Configuration config = new Configuration.Builder(app)
                .minConsumerCount(1).maxConsumerCount(3).loadFactor(3).customLogger(new CustomLogger() {
                    private static final String TAG = "JOBS";

                    @Override
                    public boolean isDebugEnabled() {
                        return true;
                    }

                    @Override
                    public void d(String text, Object... args) {
                        Log.d(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(Throwable t, String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }
                })
                .consumerKeepAlive(120).build();

        return new JobManager(app, config);
    }

    @Provides @Singleton
    public RestAdapter providesRestAdapter()
    {
        restInstances = new HashMap<String, Object>();
        return new RestAdapter.Builder()
                .setEndpoint("http://192.168.0.23:9000/api")
                .setLogLevel(RestAdapter.LogLevel.FULL).build();
    }

所以,我怎么能注入这个缓存哈希我的应用程序?

So, how can I inject this "cache" hash to my app ?

感谢

推荐答案

您可以创建一个类,它的唯一目的是提供REST接口的类。
这里的接口/执行

You could create a class which it's sole purpose would be to provide rest interface classes. Here's the interface / implementation

public interface RestApiProvider {
    public <T> T getRestClient(Class<T> clazz);
}

public class RestApiProviderImpl implements RestApiProvider {
    private Map<String, Object> restInstances = new HashMap<String, Object>();
    private RestAdapter restAdapter;

    @Inject
    RestApiProvider(RestAdapter restAdapter) {
        this.restAdapter = restAdapter;
    }

    public <T> T getRestClient(Class<T> clazz) {
        T client = null;

        if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
            return client;
        }

        client = restAdapter.create(clazz);
        restInstances.put(clazz.getCanonicalName(), client);
        return client;
    }

}

在你的模块,你将不得不

In your module you would have

@Provides @Singleton
public RestAdapter providesRestAdapter()
{
    return new RestAdapter.Builder()
            .setEndpoint("http://192.168.0.23:9000/api")
            .setLogLevel(RestAdapter.LogLevel.FULL).build();
}

@Provides @Singleton
public RestApiProvider providesRestApiProvider(RestApiProviderImpl impl) {
    return impl;
}

这工作的方式是从模块的RestAdapter提供商将被用作在RestApiProviderImpl实例依赖。
现在,任何地方,你就需要获得一个RESTAPI类实例你只需要注入你RestApiProvider。

The way this works is the the RestAdapter Provider from your module would be used as dependency in your RestApiProviderImpl instance. Now anywhere you'd need to get a RestApi class instance you'd simply need to inject your RestApiProvider.

@Inject
RestApiProvider restApiProvider;

// Somewhere in your code
RestApiClassOfSomeSort instance = restApiProvider.getRestClient(RestApiClassOfSomeSort.class);
instance.// do what you need!

这篇关于用匕首改造班级的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆