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

Android: Daggear2でDI

Android: Daggear2でDI

Sato Shun

May 13, 2015
Tweet

More Decks by Sato Shun

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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”);
    ֎෦͔Β
    ґଘΛ஫ೖ

    View full-size slide

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

    View full-size slide

  13. Dagger2ͷ࢖͍ํ
    େ͖͘෼͚ͯComponentͱModuleͷ2͕ͭ͋Δ

    View full-size slide

  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();
    }
    }

    View full-size slide

  15. Dagger2: ఆٛฤ
    public class MainActivity extends ActionBarActivity {
    @Inject
    SharedPreferences mSharedPreferences;
    @Inject
    Gson gson;

    }

    View full-size slide

  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ϝιου͔Β
    ࣗಈతʹҾ਺͕౉͞ΕΔ

    View full-size slide

  17. Dagger2: Moduleฤ
    @Module
    public class DataModule {
    @Provides
    @Singleton
    Gson provideGson() {
    return new GsonBuilder()
    .setFieldNamingPolicy(
    LOWER_CASE_WITH_UNDERSCORES)
    .create();
    }
    }

    View full-size slide

  18. Dagger2: Componentฤ
    @Singleton
    @Component(modules = {
    ApplicationModule.class,
    DataModule.class
    })
    public interface ApplicationComponent {
    void inject(MainActivity activity);
    }
    ΫϥεͰ࢖͍͍ͨModuleΛ
    Componentʹ·ͱΊΔ

    View full-size slide

  19. Dagger2: جຊ׬੒
    public class MainActivity extends ActionBarActivity {
    @Inject
    SharedPreferences mSharedPreferences;
    @Inject
    Gson gson;

    }
    ઌ΄ͲModuleͰఆٛ
    ͨ͠Πϯελϯε͕
    Inject͞ΕΔ!

    View full-size slide

  20. ·ͱΊ
    • Dagger2Λ࢖͏͜ͱͰʮઃఆΛར༻͔Β෼཭͢Δʯ
    ͕ग़དྷΔ
    • ςετ΋͠΍͘͢ͳΔ
    • Dagger1ΑΓDagger2͸ύϑΥʔϚϯε΋༏Ε͍ͯ
    Δ(Google͞Μ͍Θ͘)
    • ͜Ε͔Β͸Dagger2ͷ࣌୅(๻ͷҙݟ)
    • AndroidͰ΋reject͞ΕΔ͜ͱ͕͋Δ

    View full-size slide

  21. ࢀߟ
    • http://google.github.io/dagger/
    • https://github.com/satoshun/dagger2-sample

    View full-size slide