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

Daniel Molinero Reguera

November 04, 2016
Tweet

More Decks by Daniel Molinero Reguera

Other Decks in Programming

Transcript

  1. 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
  2. THE TALK WHY DI ? WHY TOOTHPICK? TOOTHPICK FEATURES 


    ADVANCED FEATURES 
 
 CONCLUSION SCOPE MODULES & BINDINGS SCOPE TREE SCOPE SINGLETONS MVP & STATE PRESERVATION MULTIPLE ACTIVITIES FLOWS
  3. 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;
 }

  4. 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
  5. 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);
 } }
  6. 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);
 } }
  7. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 2
 SCOPE SERVICE 1
 SCOPE

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

    FRAGMENT 3
 SCOPE FRAGMENT 1
 SCOPE FRAGMENT 2
 SCOPE SCOPE TREE
  9. 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
  10. 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
  11. TOOTHPICK FEATURES public class Module extends Module {
 public Module()

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

    class SmoothieMachine {
 } APPLICATION
 SCOPE ACTIVITY
 SCOPE SCOPE SINGLETONS OR USING ANNOTATIONS
  13. 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);
 } }
  14. @PresenterSingleton
 public class MyPresenter {
 String dealId;
 int quantity;
 }

    PRESENTER SCOPE MVP & STATE PRESERVATION ADVANCED TOOTHPICK FEATURES
  15. APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE

    ACTIVITY 3 SCOPE ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS
  16. 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);
 } }