Dagger2 source and analytical use (b)

Continued articles
4.Dagger2 Use Development:

Example _4.1Dagger2 single-mode:
In java, there are many ways to achieve single-mode embodiment, such as:

利用  synchronized锁实现橘子类的单例
public class Orange{

    private static Orange instance;

    private Orange() {
    }

    public static Orange getInstance() {
        if ( instance == null ) {
            synchronized ( Orange.class ) {
                if ( instance == null ) {
                    instance = new Orange();
                }
            }
        }

        return instance;
    }
}

In Dagger2, we can achieve this:

(1) First we @Sington mark NOTE To obtain the singleton class

@Singleton
public class Orange
{
    @Inject
    public Orange() {
        Log.d("111", "Orange: 我是橘子");
    }
}

Then @Sington mark our need to use a single case of dependent Component class notes.

@Singleton
@Component(modules = {DaggerModule.class})
public interface MainActivityComponent
{
    //为传入的MainActivity对象注入所需的依赖knife,并且为knife注入所需要的的依赖
    void inject(MainActivity mainActivity);
}

Then we MainActivity in

@Inject
Apple apple1;

@Inject
Apple apple2;

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            log.d("lll",apple1.toString());
            log.d("lll",apple2.toString());
            }
  }

Then we will find the object number of the output is the same. We successfully achieved with Dagger2 Singleton pattern.
If you are using a provider inject the way objects in @Provider added over the annotation @Sington notes can be.

_4.2Dagger2 implement lazy loading:
Sometimes we have to improve the loading speed of the software, make some non-essential classes lazy loading, that is, when it is used again loaded.
In Dagger2, we achieved:

@Inject
Lzay<T> t;

//Lzay是泛型类,只要传入我们需要的类即可

_4.3Dagger2 achieve mandatory reload:
Sometimes we want to get every time a new object when, in Dagger2, we achieved:

    @Inject
    Provider<T> t;

    //Provider是泛型类,只要传入我们需要的类即可

Dependencies between: _4.4Component

We can provide a dependency on a Compontent1 A, in another Compontent2 may be provided dependent B, C. And there is a demand that we need to use ABC three-dependent, we can make Compontent2 rely Compontent1. In this way we can use to get Compontent2 directly dependent Compontent1 in.
For example, we now have a special offer that rely on fruit

@Component(modules = {DaggerModule.class} ,dependencies = MainActivityComponent.class)
public interface FruitComponent
{
   Orange getOrange();
}

This alone is not enough, we need to pass a MainActivityComponent when used in this FruitComponent.

       MainActivityComponent mainActivityComponent  = MainActivityComponent.builder()
                .build()
                .inject(this);
                
		DaggerFruitComponent
		.builder()
		.mainActivityComponent (mainActivityComponent )
        .build();

Distinguishing manner when the same type of method returns _4.5Provider dependent

In the same module, Dagger is dependent on the type of the selected Provider. But sometimes in the same Module will return two of the same two methods rely on, this time we will use @Named comment. @Named very simple to use, we add comment below ** @ Named in @Inject notes, then Modul in @Provider comment below have joined @ Named ** annotation can be as follows:

@Inject
@Named("bnana1")
Bnana bnana1;

@Inject
@Named("bnana2")
Bnana bnana2;


@Module
public class DaggerModule {

    @Provides
    @Named("bnana1")
    public Bnana getBnana()
    {
        log.d("111,"我是bnana1")
        return new Bnana();
    }
    
    @Provides
    @Named("bnana2")
    public Bnana getBnana()
    {
        log.d("111,"我是bnana2")
        return new Bnana();
    }
}

Here, Dagger2 to use presentation is over, the next article we learn together about Dagger2 source.

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/103591861