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

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

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

1f9cd9b9bad9aa6115a1ed72433c180d?s=128

stephanenicolas

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 STEPHANE NICOLAS DANIEL MOLINERO REGUERA Android Dev @ Groupon Best level 3 ever OSS: Dart, +stephane)nicolas @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 ? DECOUPLE REUSE TEST USES USES USES MOCK

    USES
  6. WHY TOOTHPICK? SIMPLER BETTER TEST SUPPORT EVEN FASTER

  7. 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
  8. 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);
 } }
  9. 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);
 } }
  10. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY 1
 SCOPE ACTIVITY 2
 SCOPE

    SERVICE 1
 SCOPE FRAGMENT 3
 SCOPE FRAGMENT 1
 SCOPE FRAGMENT 2
 SCOPE SCOPE TREE
  11. 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
  12. TOOTHPICK FEATURES APPLICATION
 SCOPE ACTIVITY
 SCOPE APP SINGLETONS ACTIVITY SINGLETONS

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

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

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

  16. ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION Landscape Portrait

  17. PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP

    & STATE PRESERVATION ACTIVITY SCOPE
  18. 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);
 } }
  19. @PresenterSingleton
 public class MyPresenter {
 String dealId;
 int quantity;
 }

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

    ACTIVITY FLOWS Purchase Flow !
  21. APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE

    ACTIVITY 3 SCOPE ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS
  22. 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);
 } }
  23. @FlowSingleton
 public class ShoppingCart {
 List<PurchaseItem> purchases…
 } FLOW SCOPE

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

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