Android: Daggear2でDI

Android: Daggear2でDI

2793f1fe246c9299c2416062d22bfb99?s=128

Sato Shun

May 13, 2015
Tweet

Transcript

  1. Dagger2ͰDI ʲୈ17ճʳpotatotips ࠤ౻ɹ൏ @satoshun_xyz

  2. ࠷ॳʹҰݴ • ઌ೔ݸਓతʹΞϓϦΛग़͠·ͨ͠

  3. ࠷ॳʹҰݴ reject͞Ε·ͨ͠….

  4. ࠷ॳʹҰݴ AndroidͰ΋reject͞ΕΔ͜ͱ͕͋Γ·͢

  5. ໨࣍ • DIͱ͸? • Dagger2ͷجຊతͳ࢖͍ํ

  6. DIͱ͸? ֓ཁ Ϋϥεؒͷґଘؔ܎Λղܾ͢Δύλʔϯ ΫϥεC ΫϥεB ΫϥεA ΫϥεA͸ΫϥεBʹ ґଘ͍ͯ͠Δ ΫϥεB͸ΫϥεCʹ ґଘ͍ͯ͠Δ

  7. DIͱ͸? ίʔυྫ class Blogger { public void post(String title) {

    // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ FileLogger logger = new FileLogger(“log.txt”); logger.logging(); } }
  8. DIͱ͸? ίʔυྫ class Blogger { public void post(String title) {

    // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ FileLogger logger = new FileLogger(“log.txt”); logger.logging(); } }
  9. DIͱ͸? ίʔυྫ class Blogger { FileLogger logger = new FileLogger(“log.txt”);

    public void post(String title) { // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ logger.logging(); } }
  10. DIͱ͸? ίʔυྫ class Blogger { FileLogger logger; public Blogger(FileLogger logger)

    { this.logger = logger; } public void post(String title) { // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ logger.logging(); } }
  11. DIͱ͸? ίʔυྫ class Blogger { FileLogger logger; public Blogger(FileLogger logger)

    { this.logger = logger; } public void post(String title) { // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ logger.logging(); } } logger = new FileLogger(“log”); blogger = new Blogger(logger); blogger.post(“Hello World”); ֎෦͔Β ґଘΛ஫ೖ
  12. DIͱ͸? ·ͱΊ • ֎෦͔ΒΠϯελϯεΛ஫ೖ͢Δ͜ͱʹΑΓ, ৼΔ ෣͍(ಈ࡞)Λίϯτϩʔϧ͢Δ͜ͱ͕ग़དྷΔ(ྫ͑ ͹, ϙϦϞʔϑΟζϜ) • ͔͠͠ΫϥΠΞϯτଆͷෛ୲͕େ͖͘ͳͬͯ͠

    ·͏ logger = new FileLogger(“log”); blogger = new Blogger(logger); blogger.post(“Hello World”);
  13. Dagger2ͷ࢖͍ํ େ͖͘෼͚ͯComponentͱModuleͷ2͕ͭ͋Δ

  14. Dagger2ͷجຊతͳ࢖͍ํ public class MainActivity extends ActionBarActivity { SharedPreferences mSharedPreferences; Gson

    gson; @Override protected void onCreate(Bundle saved) { mSharedPreferences = getSharedPreferences("app", Context.MODE_PRIVATE) gson = new GsonBuilder() .setFieldNamingPolicy( LOWER_CASE_WITH_UNDERSCORES) .create(); } }
  15. Dagger2: ఆٛฤ public class MainActivity extends ActionBarActivity { @Inject SharedPreferences

    mSharedPreferences; @Inject Gson gson; … }
  16. Dagger2: Moduleฤ @Module public class ApplicationModule { private final AndroidApplication

    application; public ApplicationModule(AndroidApplication application) { this.application = application; } @Provides Context provideContext() { return application; } @Provides @Singleton SharedPreferences provideSharedPreferences(Context context) { return context.getSharedPreferences("app", Context.MODE_PRIVATE); } } provideContextϝιου͔Β ࣗಈతʹҾ਺͕౉͞ΕΔ
  17. Dagger2: Moduleฤ @Module public class DataModule { @Provides @Singleton Gson

    provideGson() { return new GsonBuilder() .setFieldNamingPolicy( LOWER_CASE_WITH_UNDERSCORES) .create(); } }
  18. Dagger2: Componentฤ @Singleton @Component(modules = { ApplicationModule.class, DataModule.class }) public

    interface ApplicationComponent { void inject(MainActivity activity); } ΫϥεͰ࢖͍͍ͨModuleΛ Componentʹ·ͱΊΔ
  19. Dagger2: جຊ׬੒ public class MainActivity extends ActionBarActivity { @Inject SharedPreferences

    mSharedPreferences; @Inject Gson gson; … } ઌ΄ͲModuleͰఆٛ ͨ͠Πϯελϯε͕ Inject͞ΕΔ!
  20. ·ͱΊ • Dagger2Λ࢖͏͜ͱͰʮઃఆΛར༻͔Β෼཭͢Δʯ ͕ग़དྷΔ • ςετ΋͠΍͘͢ͳΔ • Dagger1ΑΓDagger2͸ύϑΥʔϚϯε΋༏Ε͍ͯ Δ(Google͞Μ͍Θ͘) •

    ͜Ε͔Β͸Dagger2ͷ࣌୅(๻ͷҙݟ) • AndroidͰ΋reject͞ΕΔ͜ͱ͕͋Δ
  21. ࢀߟ • http://google.github.io/dagger/ • https://github.com/satoshun/dagger2-sample