今天要來講Dagger這個功能,主要透過外部來產生物件,且進行所謂的依賴注入(DI),可有效解決高耦合(依賴)的問題,可有效減少程式上的修改,那麼首先先加入依賴至gradle:
//dagger
implementation 'com.google.dagger:dagger:2.13'
annotationProcessor "com.google.dagger:dagger-compiler:2.13"
依賴注入(Dependency Injection),又簡稱DI,主要是在創建一個物件時,將他所依賴的對象可透過外部去進行產生,依賴指的是你創建這個物件所需要的零件,比如一台車,他的零件至少會包含引擎及輪胎,那麼也能代表車依賴於引擎和輪胎,而關於依賴注入也分成了三種模式:
class UserData{
private String account;
private String password;
//建構元account、password
public UserData(String account,String password){
this.account=account;
this.password=password;
}
}
class UserData{
private String account;
private String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
class UserData implements UserInterface{
@Override
public void setUser(String account, String password) {
}
}
interface UserInterface{
void setUser(String account, String password);
}
接著要來提有關於Dagger會用到的四大Annotation:
透過@Named這個annotation能取得對應的資料,操作順序是:從class Component去new物件並綁定->進入MainModel的constructor並設值->取得值後透過@Named提供一個新的MainModel物件出來->Inject注入class的物件中。
@Module
public class MainModel {
private int i;
private String s;
public MainModel(String s,int i){
this.s=s;
this.i=i;
}
@Provides
MainModel provideModel(@Named("stringS") String s,@Named("intI") int i){
return new MainModel(s,i);
}
@Named("intI")
@Provides
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
@Named("stringS")
@Provides
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
@Component(modules = MainModel.class)
public interface MainComponent {
void inject(MainActivity activity);
}
在設計完Component後,接著開始設計主程式,建議先進行build(ctrl+f9),這樣才會產生對應的Dagger Component:
接著就開始進行注入,等Component inject完畢後便可開始@Inject MainModel。
public class MainActivity extends AppCompatActivity {
@Inject
MainModel model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//new物件+接module
DaggerMainComponent.builder().mainModel(new MainModel("Str",0)).build().inject(this);
Log.d("MainModel","Int:"+model.getI()+"\n"
+"String:"+model.getS());
model.setI(10);
model.setS("Main");
Log.d("MainModel","Int:"+model.getI()+"\n"
+"String:"+model.getS());
}
}