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

From Old-School to IoC

From Old-School to IoC

On this paper I spoke about how you migrate your traditional architecture to a new and more powerfull using DIP (Dependency Inversion Pattern) and Dagger 2. You can see the sourcecode of this paper on my GitHub repository. http://github.com/txusballesteros/ioc

Af711eb0c3c1b567efeee6376462c6e1?s=128

Txus Ballesteros

October 01, 2015
Tweet

Transcript

  1. From Old-School to IoC The clean way

  2. Txus Ballesteros Android Developer at Redbooth @txusballesteros http://github.com/txusballesteros +txusballesteros txus.ballesteros@gmail.com

  3. What is IoC?

  4. Types of Dependency Injection

  5. by Constructor

  6. by Setter

  7. by Service Provider

  8. How do we do that?

  9. View Presenter Interactor Model The Architecture

  10. Old-School

  11. Old-School - Repository public class DemoRepository {
 public List<Employee> getEmployees()

    {
 //....
 }
 }
  12. Old-School - Interactor public class DemoInteractor {
 public List<Employee> getEmployees()

    {
 DemoRepository repository = new DemoRepository();
 return repository.getEmployees();
 }
 }
  13. Old-School - Presenter public class DemoPresenter {
 public List<Employee> getEmployees()

    {
 DemoInteractor interactor = new DemoInteractor();
 return interactor.getEmployees();
 }
 }
  14. public class DemoView {
 public void onShowList() {
 DemoPresenter presenter

    = new DemoPresenter();
 List<Employee> employees = presenter.getEmployees();
 renderEmployees(employees);
 }
 
 public void renderEmployees(List<Employee> employees) {
 //....
 }
 } Old-School - View
  15. None
  16. Let’s start with the refactor

  17. public class DemoRepository {
 public List<Employee> getEmployees() {
 //....
 }


    } IoC - Repository public class DemoRepository {
 public List<Employee> getEmployees() {
 //....
 }
 }
  18. public class DemoInteractor {
 public List<Employee> getEmployees() {
 DemoRepository repository

    = new DemoRepository();
 return repository.getEmployees();
 }
 } IoC - Interactor public class DemoInteractor {
 private final DemoRepository repository;
 
 public DemoInteractor(DemoRepository repository) {
 this.repository = repository;
 }
 
 public List<Employee> getEmployees() {
 List<Employee> employees = repository.getEmployees();
 return employees;
 }
 }
  19. public class DemoPresenter {
 public List<Employee> getEmployees() {
 DemoInteractor interactor

    = new DemoInteractor();
 return interactor.getEmployees();
 }
 } IoC - Presenter public class DemoPresenter {
 private final DemoView view;
 private final DemoInteractor interactor;
 
 public DemoPresenter(DemoView view, DemoInteractor interactor) {
 this.view = view;
 this.interactor = interactor;
 }
 
 public void onShowList() {
 List<Employee> employees = interactor.getEmployees();
 view.renderEmployees(employees);
 }
 }
  20. public class DemoView {
 public void onShowList() {
 DemoPresenter presenter

    = new DemoPresenter();
 List<Employee> employees = presenter.getEmployees();
 renderEmployees(employees);
 }
 
 public void renderEmployees(List<Employee> employees) {
 //....
 }
 } IoC - View public class DemoView {
 private DemoPresenter presenter;
 
 public DemoView() {
 final DemoRepository repository = new DemoRepository();
 final DemoInteractor interactor = new DemoInteractor(repository);
 presenter = new DemoPresenter(this, interactor);
 }
 
 public void onShowList() {
 presenter.onShowList();
 }
 
 public void renderEmployees(List<Employee> employees) {
 //....
 }
 }
  21. None
  22. DI + DIP = WIN

  23. DI - Repository Contract public interface DemoRepository {
 List<Employee> getEmployees();


    } public class RepositoryFactory {
 private static class LazyHolder {
 private static final RepositoryFactory INSTANCE = new RepositoryFactory();
 }
 
 public static RepositoryFactory getInstance() {
 return LazyHolder.INSTANCE;
 }
 
 public DemoRepository getDemoRepository() {
 return new DemoRepositoryImpl();
 }
 }
  24. public interface DemoRepository {
 List<Employee> getEmployees();
 } DI - Repository

    Factory public class RepositoryFactory {
 private static class LazyHolder {
 private static final RepositoryFactory INSTANCE = new RepositoryFactory();
 }
 
 public static RepositoryFactory getInstance() {
 return LazyHolder.INSTANCE;
 }
 
 public DemoRepository getDemoRepository() {
 return new DemoRepositoryImpl();
 }
 }
  25. public class DemoRepository {
 public List<Employee> getEmployees() {
 //....
 }


    } DI - Repository Implementation public class DemoRepositoryImpl implements DemoRepository {
 @Override
 public List<Employee> getEmployees() {
 //....
 }
 }
  26. public class InteractorFactory {
 private static class LazyHolder {
 private

    static final InteractorFactory INSTANCE = new InteractorFactory();
 }
 
 public static InteractorFactory getInstance() {
 return LazyHolder.INSTANCE;
 }
 
 public DemoInteractor getDemoInteractor(DemoRepository repository) {
 return new DemoInteractorImpl(repository);
 }
 } DI - Interactor Contract public interface DemoInteractor {
 List<Employee> getEmployees();
 }
  27. public interface DemoInteractor {
 List<Employee> getEmployees();
 } public class InteractorFactory

    {
 private static class LazyHolder {
 private static final InteractorFactory INSTANCE = new InteractorFactory();
 }
 
 public static InteractorFactory getInstance() {
 return LazyHolder.INSTANCE;
 }
 
 public DemoInteractor getDemoInteractor(DemoRepository repository) {
 return new DemoInteractorImpl(repository);
 }
 } DI - Interactor Factory
  28. public class DemoInteractor {
 private final DemoRepository repository;
 
 public

    DemoInteractor(DemoRepository repository) {
 this.repository = repository;
 }
 
 public List<Employee> getEmployees() {
 List<Employee> employees = repository.getEmployees();
 return employees;
 }
 } DI - Interactor Implementation public class DemoInteractorImpl implements DemoInteractor {
 private final DemoRepository repository;
 
 public DemoInteractorImpl(DemoRepository repository) {
 this.repository = repository;
 }
 
 public List<Employee> getEmployees() {
 List<Employee> employees = repository.getEmployees();
 return employees;
 }
 }
  29. public interface DemoPresenter {
 void onShowList();
 
 interface View {


    void renderList(List<Employee> employees);
 }
 } DI - Presenter Contract public class PresenterFactory {
 private static class LazyHolder {
 private static final PresenterFactory INSTANCE = new PresenterFactory();
 }
 
 public static PresenterFactory getInstance() {
 return LazyHolder.INSTANCE;
 }
 
 public DemoPresenter getDemoPresenter(DemoPresenter.View view, DemoInteractor interactor) {
 return new DemoPresenterImpl(view, interactor);
 }
 }
  30. public interface DemoPresenter {
 void onShowList();
 
 interface View {


    void renderList(List<Employee> employees);
 }
 } DI - Presenter Factory public class PresenterFactory {
 private static class LazyHolder {
 private static final PresenterFactory INSTANCE = new PresenterFactory();
 }
 
 public static PresenterFactory getInstance() {
 return LazyHolder.INSTANCE;
 }
 
 public DemoPresenter getDemoPresenter(DemoPresenter.View view, DemoInteractor interactor) {
 return new DemoPresenterImpl(view, interactor);
 }
 }
  31. public class DemoPresenter {
 private final DemoView view;
 private final

    DemoInteractor interactor;
 
 public DemoPresenter(DemoView view, DemoInteractor interactor) {
 this.view = view;
 this.interactor = interactor;
 }
 
 public void onShowList() {
 List<Employee> employees = interactor.getEmployees();
 view.renderEmployees(employees);
 }
 } DI - Presenter Implementation public class DemoPresenterImpl implements DemoPresenter {
 private final View view;
 private final DemoInteractor interactor;
 
 public DemoPresenterImpl(View view, DemoInteractor interactor) {
 this.view = view;
 this.interactor = interactor;
 }
 
 public void onShowList() {
 List<Employee> employees = interactor.getEmployees();
 view.renderList(employees);
 }
 }
  32. public class DemoView {
 private DemoPresenter presenter;
 
 public DemoView()

    {
 final DemoRepository repository = new DemoRepository();
 final DemoInteractor interactor = new DemoInteractor(repository);
 presenter = new DemoPresenter(this, interactor);
 }
 //....
 } DI - View Implementation public class DemoView implements DemoPresenter.View {
 private DemoPresenter presenter;
 
 public DemoView() {
 DemoRepository repository = RepositoryFactory.getInstance().getDemoRepository();
 DemoInteractor interactor = InteractorFactory.getInstance() .getDemoInteractor(repository);
 presenter = PresenterFactory.getInstance().getDemoPresenter(this, interactor);
 } //....
 }
  33. None
  34. Now, you’re Superman. but the boilerplate code it’s your Kryptonite.

  35. Let’s start with Dagger 2

  36. Main Grapho Child Grapho Dagger - Graphos

  37. Dagger - Repository Module @Module
 public class ModelModule {
 @Provides


    public DemoRepository providesDemoRepository(DemoRepositoryImpl repository) {
 return repository;
 }
 }
  38. Dagger - Repository Implementation @Singleton
 public class DemoRepositoryImpl implements DemoRepository

    {
 
 @Inject
 public DemoRepositoryImpl() { }
 
 @Override
 public List<Employee> getEmployees() {
 //....
 }
 }
  39. Dagger - Interactor Module @Module
 public class DomainModule {
 @Provides


    public DemoInteractor providesDemoInteractor(DemoInteractorImpl interactor) {
 return interactor;
 }
 }
  40. Dagger - Interactor Implementation public class DemoInteractorImpl implements DemoInteractor {


    private final DemoRepository repository;
 
 @Inject
 public DemoInteractorImpl(DemoRepository repository) {
 this.repository = repository;
 }
 
 public List<Employee> getEmployees() {
 List<Employee> employees = repository.getEmployees();
 return employees;
 }
 }
  41. Dagger - Presenter Module @Module
 public class PresenterModule {
 @Provides


    public DemoPresenter providesDemoPresenter(DemoPresenterImpl presenter) {
 return presenter;
 }
 }
  42. Dagger - Presenter Implementation public class DemoPresenterImpl implements DemoPresenter {


    private final View view;
 private final DemoInteractor interactor;
 
 @Inject
 public DemoPresenterImpl(View view, DemoInteractor interactor) {
 this.view = view;
 this.interactor = interactor;
 }
 
 public void onShowList() {
 List<Employee> employees = interactor.getEmployees();
 view.renderList(employees);
 }
 }
  43. Dagger - View Module @Module
 public class ViewModule {
 private

    DemoPresenter.View demoPresenterView;
 
 public ViewModule(DemoPresenter.View view) {
 demoPresenterView = view;
 }
 
 @Provides
 public DemoPresenter.View providesDemoPresenterView() {
 return demoPresenterView;
 }
 }
  44. Dagger - View Implementation public class DemoView implements DemoPresenter.View {


    @Inject DemoPresenter presenter;
 
 public DemoView() {
 DaggerMainComponent
 .builder()
 .viewModule(new ViewModule(this))
 .build()
 .inject(this);
 }
 
 public void onShowList() {
 presenter.onShowList();
 }
 
 @Override
 public void renderList(List<Employee> employees) {
 //....
 }
 }
  45. Dagger - Main Component @Singleton
 @Component(modules = MainModule.class)
 public interface

    MainComponent {
 void inject(DemoView view);
 
 DemoRepository getDemoRepository();
 }
  46. Dagger - Main Module @Module (includes = { ModelModule.class,
 DomainModule.class,


    PresenterModule.class,
 ViewModule.class }
 )
 public class MainModule { }
  47. None
  48. Thanks for your attention.

  49. Resources http://https://github.com/txusballesteros/ioc https://speakerdeck.com/txusballesteros/from-old-school-to-ioc