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

android-testing-bootcamp-2

 android-testing-bootcamp-2

Yuya Kaido

June 09, 2016
Tweet

More Decks by Yuya Kaido

Other Decks in Programming

Transcript

  1. ґଘੑ • Կ͸ͱ΋͋ΕɺίʔυΛݟͯΈΔ • GithubRepositoryΫϥε͸2ͭͷΫϥεʹґଘ • ΫϥεA͕ΫϥεBʹґଘ͍ͯ͠Δ͜ͱ͕ґଘੑ  public class

    GithubRepository {
 private GithubClient githubClient = new GithubClient();
 private GithubDao githubDao = new GithubDao();
 }
  2. ґଘੑ • ଟগґଘ౓͸Լ͕͕ͬͨɺࠜຊղܾʹͳͬͯͳ͍ • Ϣχοτςετ͕ࠔ೉ • ґଘઌͷΫϥε͕ਖ਼ৗʹಈ͍͍ͯΔඞཁੑ  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;
 }
 }
  3. 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ͷϢχοτςετ͕Մೳ • ґଘΫϥε͕ਖ਼ৗʹಈ࡞͍ͯ͠Δඞཁੑ • ͱΓ͋͑ͣϞοΫͯ͠͠·͑͹ಈ͘
  4. Inject • @InjectΛॻ͘ͱΠϯελϯε͕஫ೖ͞ΕΔ  public class GithubRepository {
 private GithubClient

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

    githubClient;
 private GithubDao githubDao;
 
 @Inject
 public GithubRepository(GithubClient client, GithubDao dao) {
 this.githubClient = client;
 this.githubDao = dao;
 }
 }
  6. Module • ੜ੒͢ΔΠϯελϯεΛఆٛ͢ΔΫϥε  @Module
 public class GithubModule {
 


    @Provides
 public GithubClient provideGithubClient() {
 return new GithubClientImpl();
 }
 
 @Provides
 public GithubDao provideGithubDao() {
 return new GithubDaoImpl();
 } 
 }
  7. جຊతͳ࢖͍ํ • 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();
 }
 
 }
  8. جຊతͳ࢖͍ํ • 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);
 }
 
 }
  9. Dependencies Subcomponent ͷҧ͍ • Dependencies • શͯͷModule͕1ͭͷComponentʹॴଐ • Subcomponent •

    Componentࣗମ͕֊૚Խ͞ΕɺͦΕͧΕ ͷComponent͕ModuleΛ؅ཧ  ͳΔ΄Ͳɺ෼͔ΒΜ
  10. ࢀߟ • ཁ͢Δʹ 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