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

Toothpick - A fresh approach to DI

Toothpick - A fresh approach to DI

Presented at Mobile Era 2016, Andevcon SF 2016 and Andevcon Washington 2017

C716975ef0b4ed35553572ff44ea1bed?s=128

Daniel Molinero Reguera

November 04, 2016
Tweet

Transcript

  1. A FRESH APPROACH TO DI TOOTHPICK https://github.com/stephanenicolas/toothpick/

  2. ABOUT US Senior Android Dev @ Groupon
 ~20 years of

    Java coding OSS: Dart, TP, BoundBox, … STEPHANE NICOLAS DANIEL MOLINERO REGUERA Android Dev @ Groupon
 Turbulence Ninja OSS: Dart, TP. +stephane @D_Lemures
  3. 5IF"OESPJE5FBNJT)JSJOH
 jobs.groupon.com/careers/

  4. THE TALK WHY DI ? WHY TOOTHPICK? TOOTHPICK FEATURES 


    ADVANCED FEATURES 
 
 CONCLUSION SCOPE MODULES & BINDINGS SCOPE TREE SCOPE SINGLETONS MVP & STATE PRESERVATION MULTIPLE ACTIVITIES FLOWS
  5. WHY DI ?

  6. WHY DI ? DECOUPLE

  7. WHY DI ? DECOUPLE USES

  8. WHY DI ? DECOUPLE REUSE USES

  9. WHY DI ? DECOUPLE REUSE USES USES USES

  10. WHY DI ? DECOUPLE REUSE TEST USES USES USES

  11. WHY DI ? DECOUPLE REUSE TEST USES USES USES MOCK

    USES
  12. WHY TOOTHPICK?

  13. WHY TOOTHPICK? SIMPLER

  14. WHY TOOTHPICK? SIMPLER BETTER TEST SUPPORT

  15. WHY TOOTHPICK? SIMPLER BETTER TEST SUPPORT EVEN FASTER

  16. TOOTHPICK FEATURES public class SmoothieMachine {
 
 @Inject IceMachine iceMachine;


    
 public void doSmoothie() {
 iceMachine.makeIce();
 }
 } @Singleton
 public class IceMachine {
 Foo foo;
 
 @Inject
 public IceMachine(Foo foo) {
 this.foo = foo;
 }

  17. TOOTHPICK FEATURES public class SmoothieMachine {
 
 @Inject IceMachine iceMachine;


    
 public void doSmoothie() {
 iceMachine.makeIce();
 }
 } @Singleton
 public class IceMachine {
 Foo foo;
 
 @Inject
 public IceMachine(Foo foo) {
 this.foo = foo;
 }
 JSR 330 annotations→nothing new here
  18. TOOTHPICK FEATURES SCOPE SCOPE

  19. TOOTHPICK FEATURES SCOPE SCOPE MAKE INJECTIONS

  20. TOOTHPICK FEATURES SCOPE SCOPE MAKE INJECTIONS public class MyApplication extends

    Application { @Inject Machine machine;
 
 @Override protected void onCreate() {
 super.onCreate();
 
 Scope appScope = Toothpick.openScope(this); appScope.installModules(…); Toothpick.inject(this, appScope);
 } }
  21. TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE

  22. TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE BINDINGS

  23. TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE BINDINGS MODULES

  24. TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE BINDINGS MODULES public

    class MyApplication extends Application { @Inject Machine machine;
 
 @Override protected void onCreate() {
 super.onCreate();
 
 Scope appScope = Toothpick.openScope(this); appScope.installModules(new Module() {{
 bind(Machine.class).to(IceMachine.class); }});
 Toothpick.inject(this, appScope);
 } }
  25. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 1
 SCOPE SCOPE TREE

  26. TOOTHPICK FEATURES APPLICATION
 SCOPE SCOPE TREE

  27. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 2
 SCOPE SCOPE TREE

  28. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 2
 SCOPE SERVICE 1
 SCOPE

    SCOPE TREE
  29. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 2
 SCOPE SERVICE 1
 SCOPE

    FRAGMENT 1
 SCOPE SCOPE TREE
  30. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 2
 SCOPE SERVICE 1
 SCOPE

    FRAGMENT 1
 SCOPE FRAGMENT 2
 SCOPE SCOPE TREE
  31. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 2
 SCOPE SERVICE 1
 SCOPE

    FRAGMENT 3
 SCOPE FRAGMENT 1
 SCOPE FRAGMENT 2
 SCOPE SCOPE TREE
  32. TOOTHPICK FEATURES public class LemonActivity extends Activity {
 
 @Inject

    Machine machine; @Inject Context context;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Scope scope = Toothpick.openScopes(getApplication(), this);
 scope.installModules(…);
 Toothpick.inject(this, scope);
 } } SCOPE TREE
  33. TOOTHPICK FEATURES public class LemonActivity extends Activity {
 
 @Inject

    Machine machine; @Inject Context context;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Scope scope = Toothpick.openScopes(getApplication(), this);
 scope.installModules(…);
 Toothpick.inject(this, scope);
 } } SCOPE TREE APPLICATION
 SCOPE ACTIVITY
 SCOPE
  34. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY
 SCOPE SCOPE SINGLETONS

  35. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY
 SCOPE APP SINGLETONS SCOPE SINGLETONS

  36. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY
 SCOPE APP SINGLETONS ACTIVITY SINGLETONS

    SCOPE SINGLETONS
  37. TOOTHPICK FEATURES public class Module extends Module {
 public Module()

    {
 bind(Machine.class).toInstance(new Ice Machine());
 }
 } SCOPE SINGLETONS SINGLETONS CAN BE DEFINED IN MODULES
  38. TOOTHPICK FEATURES @Singleton
 public class IceMachine {
 } @ActivitySingleton
 public

    class SmoothieMachine {
 } APPLICATION
 SCOPE ACTIVITY
 SCOPE SCOPE SINGLETONS OR USING ANNOTATIONS
  39. ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION

  40. ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION

  41. ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION

  42. APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION ACTIVITY

    SCOPE
  43. APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION ACTIVITY

    SCOPE
  44. APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP & STATE

    PRESERVATION ACTIVITY SCOPE
  45. PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP

    & STATE PRESERVATION ACTIVITY SCOPE
  46. PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP

    & STATE PRESERVATION
  47. PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP

    & STATE PRESERVATION ACTIVITY SCOPE
  48. ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION public class MyActivity

    extends Activity {
 
 @Inject MyPresenter presenter; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Scope scope = openScopes(getApplication(), 
 PresenterSingleton.class, 
 this);
 Toothpick.inject(this, scope);
 } }
  49. @PresenterSingleton
 public class MyPresenter {
 String dealId;
 int quantity;
 }

    PRESENTER SCOPE MVP & STATE PRESERVATION ADVANCED TOOTHPICK FEATURES
  50. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS
  51. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS Purchase Flow
  52. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS Purchase Flow
  53. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS
  54. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS
  55. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS
  56. ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI

    ACTIVITY FLOWS
  57. APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE

    MULTI ACTIVITY FLOWS
  58. APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE

    MULTI ACTIVITY FLOWS
  59. APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE

    ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS
  60. APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE

    ACTIVITY 3 SCOPE ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS
  61. APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE MULTI ACTIVITY FLOWS

  62. APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES MULTI ACTIVITY FLOWS

  63. ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION public class MyActivity

    extends Activity {
 
 @Inject ShoppingCart shoppingCart; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Scope scope = openScopes(getApplication(), 
 FlowSingleton.class, 
 this);
 Toothpick.inject(this, scope);
 } }
  64. @FlowSingleton
 public class ShoppingCart {
 List<PurchaseItem> purchases…
 } FLOW SCOPE

    MULTI ACTIVITY FLOWS ADVANCED TOOTHPICK FEATURES
  65. CONCLUSION QUESTIONS ? COMMENTS ?

  66. CONCLUSION QUESTIONS ? COMMENTS ?

  67. A FRESH APPROACH TO DI TOOTHPICK https://github.com/stephanenicolas/toothpick/ +stephane @D_Lemures