Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Square/Dagger, Google/Dagger

Square/Dagger, Google/Dagger

6dd0483f1353a4a359e92633cfd65c64?s=128

Daichi Furiya (Wasabeef)

September 24, 2014
Tweet

Transcript

  1. Android Square/Dagger potatotips #9 CyberAgent, Inc. ߱໼ େ஍

  2. About Me Wasabeef (D.Furiya) @wasabeef_jp wasabeef http://wasabeef.jp

  3. Dagger (ObjectGraph) ɾίϯύΠϧ࣌ݕূɿϞδϡʔϧͱ஫ೖΛݕূ͢Δ ɹΞϊςʔγϣϯϓϩηοα ! ɾ࣮ߦ࣌ʹ͸ɺϦϑϨΫγϣϯΛར༻͠ͳ͍ ! ɾϝϞϦ΁ͷӨڹ͸ۃΘ͔ͣʹ͢Δ ! ɾViewͷInject͸ग़དྷͳ͍

  4. Dagger ͷߏ੒ཁૉ

  5. @Module + @Provides ґଘؔ܎Λఏڙ͢ΔͨΊͷ࢓૊Έɾઃఆ

  6. ObjectGraph ґଘؔ܎؅ཧͱΠϯδΣΫλ

  7. @Inject ґଘؔ܎Λཁٻ͢ΔͨΊͷ࢓૊Έ

  8. @Module + @Provides ͷఆٛ

  9. ! public class ApiModule { ! public RestAdapter provideRestAdapter(OkHttpClient client)

    { return new RestAdapter.Builder() .setEndpoint(“http://wasabeef.jp”) .setClient(new OkClient(client)) .build(); } ! ! public OkHttpClient provideOkHttpClient() { return new OkHttpClient(); } }
  10. @Module( injects = ExampleActivity.java ) public class ApiModule { !

    public RestAdapter provideRestAdapter(OkHttpClient client) { return new RestAdapter.Builder() .setEndpoint(“http://wasabeef.jp”) .setClient(new OkClient(client)) .build(); } ! ! public OkHttpClient provideOkHttpClient() { return new OkHttpClient(); } }
  11. @Module( injects = ExampleActivity.java ) public class ApiModule { @Provides

    public RestAdapter provideRestAdapter(OkHttpClient client) { return new RestAdapter.Builder() .setEndpoint(“http://wasabeef.jp”) .setClient(new OkClient(client)) .build(); } ! @Provides public OkHttpClient provideOkHttpClient() { return new OkHttpClient(); } }
  12. @Module( injects = ExampleActivity.java ) public class ApiModule { @Provides

    @Singleton public RestAdapter provideRestAdapter(OkHttpClient client) { return new RestAdapter.Builder() .setEndpoint(“http://wasabeef.jp”) .setClient(new OkClient(client)) .build(); } ! @Provides @Singleton public OkHttpClient provideOkHttpClient() { return new OkHttpClient(); } }
  13. ObjectGraph + @Inject ͷఆٛ

  14. public class ExampleActivity extends Activity { ! ! RestAdapter mRestAdapter;

    ! @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ! mRestAdapter = new RestAdapter.Builder() .setEndpoint(“http://wasabeef.jp”) .setClient(new OkClient(new OkHttpClient())) .build(); } }
  15. public class ExampleActivity extends Activity { private ObjectGraph mObjectGraph; !

    RestAdapter mRestAdapter; ! @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ! mRestAdapter = new RestAdapter.Builder() .setEndpoint(“http://wasabeef.jp”) .setClient(new OkClient(new OkHttpClient())) .build(); } }
  16. public class ExampleActivity extends Activity { private ObjectGraph mObjectGraph; !

    RestAdapter mRestAdapter; ! @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ! mObjectGraph = ObjectGraph.create(new ApiModule()); mObjectGraph.inject(this); ! ! } }
  17. public class ExampleActivity extends Activity { private ObjectGraph mObjectGraph; !

    @Inject RestAdapter mRestAdapter; ! @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ! mObjectGraph = ObjectGraph.create(new ApiModule()); mObjectGraph.inject(this); ! ! } }
  18. ObjectGraph.plus

  19. public class SignUpActivity extends Activity { private ObjectGraph mObjectGraph; !

    @Inject RestAdapter mRestAdapter; ! @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ! ExampleApp app = (ExampleApp) getApplication(); mObjectGraph = app.getObjectGraph(); mObjectGraph.plus(new SignUpModule()); mObjectGraph.inject(this); } }
  20. Google/Dagger

  21. Square/Daggerͱͷҧ͍ ɾτϨʔαϏϦςΟ “Open Declaration”΍“Find usages”ͳͲͰɺݺͼग़͠ݩͷτϨʔε͕ग़དྷΔ ! ɾ໌֬ͳAPI “@Module”ΛΘ͔Γ΍͘͢͠ɺ”@Component”, “@Subcomponent”ͷ௥Ճ !

    ɾύϑΥʔϚϯε޲্ Reflection͸࢖Θͳ͍ ࣗಈੜ੒͞ΕΔίʔυΛखॻ͖ͷΑ͏ʹͳίʔυ඼࣭ʹ͚ۙͮΔ
  22. Google/Dagger ! ։ൃதͳͷͰ ػೳFix͸΋͏গ͔͔͠Γͦ͏ɻ

  23. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠