Slide 1

Slide 1 text

Advanced Dagger for Android Android Testing Bootcamp #2 yuyakaido

Slide 2

Slide 2 text

ࣗݾ঺հ • ւ౻༏໻ʢ͔͍Ͳ͏Ώ͏΍ʣ • גࣜձࣾΤ΢ϨΧ • ໿1೥ͷΠϯλʔϯΛܦͯɺ2015೥4݄ʹ৽ଔೖࣾ • ςετ͓͡͞Μ  yuyakaido yuyakaido

Slide 3

Slide 3 text

גࣜձࣾΤ΢ϨΧ • ࣗࣾαʔϏεͷاըɾ։ൃɾӡӦ • ΦϯϥΠϯσʔςΟϯάαʔϏεɿ • Χοϓϧઐ༻ΞϓϦɿ • ࣾһ਺ɿ104໊ • IAC  ΤϯδχΞ 51ਓ ඇΤϯδχΞ 53ਓ

Slide 4

Slide 4 text

໨࣍ • DIͱ͸ • جૅฤ • Ԡ༻ฤ • ·ͱΊ 

Slide 5

Slide 5 text

DIͱ͸ • DI = Dependency Injection • ೔ຊޠͰ͸ɺґଘੑ஫ೖ • ґଘੑͱ͸ 

Slide 6

Slide 6 text

ґଘੑ • Կ͸ͱ΋͋ΕɺίʔυΛݟͯΈΔ • GithubRepositoryΫϥε͸2ͭͷΫϥεʹґଘ • ΫϥεA͕ΫϥεBʹґଘ͍ͯ͠Δ͜ͱ͕ґଘੑ  public class GithubRepository {
 private GithubClient githubClient = new GithubClient();
 private GithubDao githubDao = new GithubDao();
 }

Slide 7

Slide 7 text

ґଘੑ • ґଘੑ͕ϕλॻ͖ͩͱԿ͕Ϛζ͍ͷ͔ • Ϣχοτςετ͕ࠔ೉ • ґଘઌͷΫϥε͕ਖ਼ৗʹಈ͍͍ͯΔඞཁੑ • Կͱ͔ͯ͠ґଘੑΛഉআ͍ͨ͠ • ͱΓ͋͑ͣηολʔΛఆٛ͢Ε͹͍͍ͷͰ͸ʁ 

Slide 8

Slide 8 text

ґଘੑ • ଟগґଘ౓͸Լ͕͕ͬͨɺࠜຊղܾʹͳͬͯͳ͍ • Ϣχοτςετ͕ࠔ೉ • ґଘઌͷΫϥε͕ਖ਼ৗʹಈ͍͍ͯΔඞཁੑ  public class GithubRepository {
 private GithubClient githubClient;
 private GithubDao githubDao;
 
 public void setGithubClient(GithubClient githubClient) {
 this.githubClient = githubClient;
 }
 
 public void setGithubDao(GithubDao githubDao) {
 this.githubDao = githubDao;
 }
 }

Slide 9

Slide 9 text

public class GithubRepository {
 private GithubClient githubClient;
 private GithubDao githubDao;
 
 public GithubRepository(GithubClient client, GithubDao dao) {
 this.githubClient = client;
 this.githubDao = dao;
 }
 } public interface GithubClient { /* লུ */ } public interface GithubDao { /* লུ */ } ґଘੑ  • Ϣχοτςετ͕ࠔ೉ • GithubClientɺGithubDaoΛϞοΫ͢Ε͹ GithubRepositoryͷϢχοτςετ͕Մೳ • ґଘΫϥε͕ਖ਼ৗʹಈ࡞͍ͯ͠Δඞཁੑ • ͱΓ͋͑ͣϞοΫͯ͠͠·͑͹ಈ͘

Slide 10

Slide 10 text

DIͱ͸ • DI = Dependency Injection • ೔ຊޠͰ͸ɺґଘੑ஫ೖ • DI = ґଘΛϕλॻ͖Λආ͚ΔͨΊͷ࢓૊Έ 

Slide 11

Slide 11 text

Dagger • ಥવͰ͕͢ɺDagger͸2ͭ͋Γ·͢ • https://github.com/square/dagger • ΦϦδφϧ͸ͬͪ͜ • https://github.com/google/dagger • square/daggerΛϕʔεʹͯ͠Google͕ख ΛೖΕͨ΋ͷ 

Slide 12

Slide 12 text

جૅฤ • Inject • Module • Component • جຊతͳ࢖͍ํ 

Slide 13

Slide 13 text

Inject • @InjectΛॻ͘ͱΠϯελϯε͕஫ೖ͞ΕΔ  public class GithubRepository {
 private GithubClient githubClient;
 private GithubDao githubDao;
 
 
 public GithubRepository(GithubClient client, GithubDao dao) {
 this.githubClient = client;
 this.githubDao = dao;
 }
 }

Slide 14

Slide 14 text

Inject • @InjectΛॻ͘ͱΠϯελϯε͕஫ೖ͞ΕΔ  public class GithubRepository {
 private GithubClient githubClient;
 private GithubDao githubDao;
 
 @Inject
 public GithubRepository(GithubClient client, GithubDao dao) {
 this.githubClient = client;
 this.githubDao = dao;
 }
 }

Slide 15

Slide 15 text

Module • ੜ੒͢ΔΠϯελϯεΛఆٛ͢ΔΫϥε  @Module
 public class GithubModule {
 
 @Provides
 public GithubClient provideGithubClient() {
 return new GithubClientImpl();
 }
 
 @Provides
 public GithubDao provideGithubDao() {
 return new GithubDaoImpl();
 } 
 }

Slide 16

Slide 16 text

Component • ModuleͱInjectର৅Ϋϥεͷؔ܎Λఆٛ  @Component(modules = {GithubModule.class})
 public interface AppComponent {
 void inject(MainActivity mainActivity);
 }

Slide 17

Slide 17 text

جຊతͳ࢖͍ํ • DaggerAppComponentΛॳظԽ  public class Genesis extends Application {
 
 private AppComponent appComponent;
 
 public static AppComponent getAppComponent(Context context) {
 Genesis genesis = (Genesis) context.getApplicationContext();
 return genesis.appComponent;
 }
 
 @Override
 public void onCreate() {
 super.onCreate();
 appComponent = DaggerAppComponent.builder().build();
 }
 
 }

Slide 18

Slide 18 text

جຊతͳ࢖͍ํ • Inject͢Δ  public class MainActivity extends Activity {
 
 @Inject
 GithubRepository githubRepository;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Genesis.getAppComponent(this).inject(this);
 }
 
 }

Slide 19

Slide 19 text

Ԡ༻ฤ • Dependencies • Subcomponent 

Slide 20

Slide 20 text

લఏ • ΞϓϦͷن໛ʹൺྫͯ͠ɺModule΋૿͑Δ • Module͕૿͑ΔͱComponent͕ڊେԽ͍ͯ͘͠ 

Slide 21

Slide 21 text

Dependencies Subcomponent ͱ͸ • ΞϓϦͷن໛ʹൺྫͯ͠ɺModule΋૿͑Δ • Moduleͷ؅ཧํ๏ • Dependencies • Subcomponent 

Slide 22

Slide 22 text

Dependencies • ґଘ͍ͯ͠ΔModuleΛྻڍ  @Component(dependencies = {GithubModule.class})
 public interface AppComponent {
 void inject(MainActivity mainActivity);
 }

Slide 23

Slide 23 text

Dependencies • ґଘ͍ͯ͠ΔModuleΛྻڍ  @Component(dependencies = {GithubModule.class})
 public interface AppComponent {
 void inject(MainActivity mainActivity);
 }

Slide 24

Slide 24 text

Subcomponent • ComponentΛ֊૚Խ͢Δ  @ScreenScope
 @Subcomponent(modules = {InstagramModule.class})
 public interface InstagramComponent {
 void inject(InstagramActivity instagramActivity);
 }

Slide 25

Slide 25 text

Subcomponent • ComponentΛ֊૚Խ͢Δ  @ScreenScope
 @Subcomponent(modules = {InstagramModule.class})
 public interface InstagramComponent {
 void inject(InstagramActivity instagramActivity);
 }

Slide 26

Slide 26 text

Dependencies Subcomponent • Dependencies • ґଘ͍ͯ͠ΔModuleΛྻڍ • Subcomponent • ComponentΛ֊૚Խ 

Slide 27

Slide 27 text

Dependencies Subcomponent ͷҧ͍ • Dependencies • શͯͷModule͕1ͭͷComponentʹॴଐ • Subcomponent • Componentࣗମ͕֊૚Խ͞ΕɺͦΕͧΕ ͷComponent͕ModuleΛ؅ཧ  ͳΔ΄Ͳɺ෼͔ΒΜ

Slide 28

Slide 28 text

Dependencies  AppComponent FooModule BarModule

Slide 29

Slide 29 text

Subcomponent  AppComponent FooComponent BarComponent

Slide 30

Slide 30 text

Dependencies Subcomponent • ͲͪΒΛ࢖͏΂͖͔ • ن໛͕খ͚͞Ε͹ɺDependencies • ͋Δఔ౓ͷن໛Λ૝ఆ͢ΔͳΒɺSubcomponent • Dependenciesͩͱ1ͭͷComponent͕શͯͷґ ଘΠϯελϯεΛ؅ཧ͢Δ͜ͱʹɺɺ 

Slide 31

Slide 31 text

·ͱΊ • DIͱ͸Կͳͷ͔ • ґଘੑΛϕλॻ͖͠ͳ͍ͨΊͷ࢓૊Έ • Daggerجૅฤ • ModuleͱComponent • DaggerԠ༻ฤ • DependenciesͱSubcomponent 

Slide 32

Slide 32 text

ࢀߟ • ཁ͢Δʹ DI ͬͯԿͳͷͱ͍͏࿩ • http://nekogata.hatenablog.com/entry/ 2014/02/13/073043 • Android: Dagger2 - Subcomponent vs. dependencies • http://yuki312.blogspot.jp/2016/02/android-dagger2- subcomponent-vs.html 

Slide 33

Slide 33 text

Thank you :) Credit: NASA Earth Observatory/NOAA NGDC