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

TP-Mobile-Era-2016-final-compressed.pdf

 TP-Mobile-Era-2016-final-compressed.pdf

Introduction to Toothpick: a fresh approach to DI
(Java / Android)

stephanenicolas

November 04, 2016
Tweet

More Decks by stephanenicolas

Other Decks in Programming

Transcript

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

    Java coding OSS: Dart, TP STEPHANE NICOLAS DANIEL MOLINERO REGUERA Android Dev @ Groupon Best level 3 ever OSS: Dart, +stephane)nicolas @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;
 }
 JSR 330 annotations→nothing new here
  4. 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);
 } }
  5. 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);
 } }
  6. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 1
 SCOPE ACTIVITY 2
 SCOPE

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

    SmoothieMachine smoothieMachine;
 
 @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
  8. TOOTHPICK FEATURES public class Module extends Module {
 public Module1()

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

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

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

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