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

Txus Ballesteros

October 01, 2015
Tweet

More Decks by Txus Ballesteros

Other Decks in Programming

Transcript

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

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

    {
 DemoInteractor interactor = new DemoInteractor();
 return interactor.getEmployees();
 }
 }
  3. 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
  4. public class DemoRepository {
 public List<Employee> getEmployees() {
 //....
 }


    } IoC - Repository public class DemoRepository {
 public List<Employee> getEmployees() {
 //....
 }
 }
  5. 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;
 }
 }
  6. 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);
 }
 }
  7. 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) {
 //....
 }
 }
  8. 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();
 }
 }
  9. 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();
 }
 }
  10. public class DemoRepository {
 public List<Employee> getEmployees() {
 //....
 }


    } DI - Repository Implementation public class DemoRepositoryImpl implements DemoRepository {
 @Override
 public List<Employee> getEmployees() {
 //....
 }
 }
  11. 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();
 }
  12. 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
  13. 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;
 }
 }
  14. 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);
 }
 }
  15. 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);
 }
 }
  16. 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);
 }
 }
  17. 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);
 } //....
 }
  18. Dagger - Repository Module @Module
 public class ModelModule {
 @Provides


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

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


    public DemoInteractor providesDemoInteractor(DemoInteractorImpl interactor) {
 return interactor;
 }
 }
  21. 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;
 }
 }
  22. Dagger - Presenter Module @Module
 public class PresenterModule {
 @Provides


    public DemoPresenter providesDemoPresenter(DemoPresenterImpl presenter) {
 return presenter;
 }
 }
  23. 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);
 }
 }
  24. 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;
 }
 }
  25. 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) {
 //....
 }
 }
  26. Dagger - Main Component @Singleton
 @Component(modules = MainModule.class)
 public interface

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


    PresenterModule.class,
 ViewModule.class }
 )
 public class MainModule { }