Slide 1

Slide 1 text

%BHHFS 0QUJPOBMCJOEJOHT

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

&OUSZ public class Entry {
 public final String title;
 public Entry(String title) {
 this.title = title;
 }
 }

Slide 7

Slide 7 text

.PEVMF BCTUSBDU @Module
 public abstract class MainModule {
 @BindsOptionalOf
 public abstract Entry optionalEntryProvider();
 }

Slide 8

Slide 8 text

$PNQPOFOU @Component(modules = {MainModule.class})
 public interface MainComponent {
 Optional entry();
 }

Slide 9

Slide 9 text

.BJO"DUJWJUZ public class MainActivity extends AppCompatActivity {
 …
 MainComponent mainComponent = DaggerMainComponent.create();
 
 Optional entry = mainComponent.entry();
 Log.e(“entry”, entry.or(new Entry(“default")).title); …
 } w EFGBVMUͱදࣔ͞ΕΔ

Slide 10

Slide 10 text

.PEVMF @Module
 public class EntryModule {
 @Provides
 public Entry provideEntry() {
 return new Entry(“entry");
 }
 }

Slide 11

Slide 11 text

$PNQPOFOU @Component(modules = {MainModule.class, EntryModule.class})
 public interface MainComponent {
 Optional entry();
 }

Slide 12

Slide 12 text

.BJO"DUJWJUZ public class MainActivity extends AppCompatActivity {
 …
 Optional entry = mainComponent.entry();
 Log.e(“entry”, entry.or(new Entry(“default")).title); …
 } w FOUSZͱදࣔ͞ΕΔ

Slide 13

Slide 13 text

$PNQPOFOU ͝ͱʹ ஋ΛมԽͤ͞Δ

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

0QUJPOBMΛίϯετϥΫλ Ͱड͚औΔΫϥε public class EntryManager {
 private final Entry mEntry;
 @Inject
 public EntryManager(Optional entryOptional) {
 mEntry = entryOptional.or(new Entry("default"));
 }
 
 public String getTitle() {
 return mEntry.title;
 }
 }

Slide 16

Slide 16 text

&OUSZΛఏڙ͠ͳ͍ $PNQPOFOU @Subcomponent
 public interface EmptyComponent {
 void inject(MainActivity activity);
 @Subcomponent.Builder
 interface Builder {
 EmptyComponent build();
 }
 }

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

&OUSZΛఏڙ͢Δ $PNQPOFOU @Module
 public class SavedEntryModule {
 @Provides Entry provideEntry() {
 return new Entry("saved");
 }
 }

Slide 19

Slide 19 text

"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
 }

Slide 20

Slide 20 text

·ͱΊ w 0QUJPOBMͷ஋ΛޙͰղܾͰ͖Δ w 4VC$PNQPOFOUຖʹࠩҟΛग़ͤΔ w !*OKFDU͕ॻ͔ΕͨϑΟʔϧυɺίϯετϥΫ λͰ΋ͦͷ0QUJPOBMΛड͚औΕΔ w ໪࿦$PNQPOFOU͔ΒήολͰऔΔ͜ͱ΋Ͱ ͖Δ

Slide 21

Slide 21 text

ࢀߟ w IUUQTHPPHMFHJUIVCJPEBHHFSVTFST HVJEF

Slide 22

Slide 22 text

͋Γ͕ͱ͏͍͟͝·ͨ͠