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

Dagger2 Optional bindings

8928b5277aa7234dbca4ff782952343a?s=47 funnelbit
November 28, 2016

Dagger2 Optional bindings

8928b5277aa7234dbca4ff782952343a?s=128

funnelbit

November 28, 2016
Tweet

More Decks by funnelbit

Other Decks in Technology

Transcript

  1. %BHHFS 0QUJPOBMCJOEJOHT

  2. ࣗݾ঺հ ๺ଜྋ גࣜձࣾ͸ͯͳ )BUFOBGVOOFMCJU 5XJUUFS!FYQFSPQFSP

  3. w %SPJEP,BJHJʹొஃ͠·͢ w λΠτϧେن໛ΞϓϦͷϦϊϕʔγϣϯ

  4. 0QUJPOBMΛఆٛ͠ ޙͰ ղܾͯ͠΋Β͏ !#JOET0QUJPOBM0G

  5. !#JOET0QUJPOBM0G w 0QUJPOBMCJOEJOHΛ࣮ݱ͢Δػೳ w 0QUJPOBM5Λฦ͢ఆٛΛ༻ҙ͢Δ w ಉ͡ίϯϙʔωϯτ͔ɺαϒίϯϙʔωϯτͰ ஋ΛຬͨͤΔ w %BHHFSd

  6. &OUSZ public class Entry {
 public final String title;
 public

    Entry(String title) {
 this.title = title;
 }
 }
  7. .PEVMF BCTUSBDU @Module
 public abstract class MainModule {
 @BindsOptionalOf
 public

    abstract Entry optionalEntryProvider();
 }
  8. $PNQPOFOU @Component(modules = {MainModule.class})
 public interface MainComponent {
 Optional<Entry> entry();


    }
  9. .BJO"DUJWJUZ public class MainActivity extends AppCompatActivity {
 …
 MainComponent mainComponent

    = DaggerMainComponent.create();
 
 Optional<Entry> entry = mainComponent.entry();
 Log.e(“entry”, entry.or(new Entry(“default")).title); …
 } w EFGBVMUͱදࣔ͞ΕΔ
  10. .PEVMF @Module
 public class EntryModule {
 @Provides
 public Entry provideEntry()

    {
 return new Entry(“entry");
 }
 }
  11. $PNQPOFOU @Component(modules = {MainModule.class, EntryModule.class})
 public interface MainComponent {
 Optional<Entry>

    entry();
 }
  12. .BJO"DUJWJUZ public class MainActivity extends AppCompatActivity {
 …
 Optional<Entry> entry

    = mainComponent.entry();
 Log.e(“entry”, entry.or(new Entry(“default")).title); …
 } w FOUSZͱදࣔ͞ΕΔ
  13. $PNQPOFOU ͝ͱʹ ஋ΛมԽͤ͞Δ

  14. &OUSZΛఏڙ͢Δ 4VC$PNQPOFOU &OUSZΛఏڙ͠ͳ͍ 4VC$PNQPOFOU 0QUJPOBMΛ༻ҙ͢Δ $PNQPOFOU 0QUJPOBMΛίϯετϥΫλ Ͱड͚औΔΫϥε "DUJWJUZ 0QUJPOBM5

    03
  15. 0QUJPOBMΛίϯετϥΫλ Ͱड͚औΔΫϥε public class EntryManager {
 private final Entry mEntry;


    @Inject
 public EntryManager(Optional<Entry> entryOptional) {
 mEntry = entryOptional.or(new Entry("default"));
 }
 
 public String getTitle() {
 return mEntry.title;
 }
 }
  16. &OUSZΛఏڙ͠ͳ͍ $PNQPOFOU @Subcomponent
 public interface EmptyComponent {
 void inject(MainActivity activity);


    @Subcomponent.Builder
 interface Builder {
 EmptyComponent build();
 }
 }
  17. &OUSZΛఏڙ͢Δ $PNQPOFOU @Subcomponent(modules = SavedEntryModule.class)
 public interface SavedEntryComponent {
 void

    inject(MainActivity activity);
 @Subcomponent.Builder
 interface Builder {
 SavedEntryComponent build();
 }
 }
  18. &OUSZΛఏڙ͢Δ $PNQPOFOU @Module
 public class SavedEntryModule {
 @Provides Entry provideEntry()

    {
 return new Entry("saved");
 }
 }
  19. "DUJWJUZ public class MainActivity extends AppCompatActivity {
 @Inject EntryManager mEntryManager;


    …
 DaggerMainComponent
 .create()
 .emptyComponent()
 .build()
 .inject(this);
 Log.e("title", mEntryManager.getTitle()); // default
 
 DaggerMainComponent
 .create()
 .savedEntryComponent()
 .build()
 .inject(this);
 Log.e("title", mEntryManager.getTitle()); // saved
 }
  20. ·ͱΊ w 0QUJPOBMͷ஋ΛޙͰղܾͰ͖Δ w 4VC$PNQPOFOUຖʹࠩҟΛग़ͤΔ w !*OKFDU͕ॻ͔ΕͨϑΟʔϧυɺίϯετϥΫ λͰ΋ͦͷ0QUJPOBMΛड͚औΕΔ w ໪࿦$PNQPOFOU͔ΒήολͰऔΔ͜ͱ΋Ͱ

    ͖Δ
  21. ࢀߟ w IUUQTHPPHMFHJUIVCJPEBHHFSVTFST HVJEF

  22. ͋Γ͕ͱ͏͍͟͝·ͨ͠