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

Toothpick & Dependency Injection

Toothpick & Dependency Injection

Toothpick is a scope tree based Dependency Injection library for Java. It is a full-featured, runtime based, but reflection free, implementation of JSR 330.

Presented at Droidcon Berlin 2016

Daniel Molinero Reguera

June 15, 2016
Tweet

More Decks by Daniel Molinero Reguera

Other Decks in Programming

Transcript

  1. Dependency Injection
    & Toothpick
    https://github.com/stephanenicolas/toothpick

    View full-size slide

  2. 2/27
    About us
    Stéphane Nicolas
    Senior Android Dev at
    Groupon
    stephanenicolas
    snicolas
    +stephane nicolas
    Daniel Molinero
    Android Dev at
    Groupon
    dlemures
    +DaniLemures
    @D_Lemures

    View full-size slide

  3. • Introduction
    • Dependency Injection
    • Libraries
    • Toothpick
    • Toothpick Example
    • Bonus: Advanced scenarios
    3/27

    View full-size slide

  4. Dependencies?
    • Any object can be a dependency
    • A dependency of X is an object used by X to
    accomplish its task
    4/27
    Introduction

    View full-size slide

  5. Dependencies?
    • Any object can be a dependency
    • A dependency of X is an object used by X to
    accomplish its task
    4/27
    Introduction
    Snow Cone
    Granizado
    Granité
    Slushie
    ٩ဉ
    Granita

    View full-size slide

  6. Dependencies?
    • Snow Cone Machine.
    • Dependencies: Ice Machine + Syrup Dispenser
    5/27
    Introduction

    View full-size slide

  7. Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    6/27
    Introduction

    View full-size slide

  8. 7/27
    Introduction
    Disadvantages
    • The class is highly coupled to its dependencies
    • It depends on its dependencies implementation details
    • Breaks single responsibility principle
    • Non reusable code
    • Non testable code

    View full-size slide

  9. 8/27
    Dependency Injection
    What is Dependency Injection (DI)?
    • Software design pattern
    • Implements inversion of control to resolve
    dependencies
    • Dependencies are passed to the object, it does not
    build or find them
    • Separates responsibilities of use and construction
    • An object is independent of its dependencies
    implementation details

    View full-size slide

  10. Dependency Injection
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    9/27

    View full-size slide

  11. Dependency Injection
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    9/27

    View full-size slide

  12. Dependency Injection
    Advantages
    • Decreases coupling between the object and its
    dependencies
    • It is configurable. It does not need to know about a
    concrete implementation
    • Reusable code
    • Testable code
    10/27

    View full-size slide

  13. 11/27
    Libraries
    But…
    • Is everything so easy and perfect?
    • What about something like:

    View full-size slide

  14. 11/27
    Libraries
    But…
    • Is everything so easy and perfect?
    • What about something like:
    Can someone do it for me??

    View full-size slide

  15. 11/27
    Libraries
    But…
    • Is everything so easy and perfect?
    • What about something like:
    Can someone do it for me??
    YES

    View full-size slide

  16. 12/27
    Libraries
    Dependency Injection Libraries
    • They create and inject dependencies for us
    • Just specify what are the dependencies and how to
    build them
    • Two main libraries for Android:
    • Roboguice
    • Dagger

    View full-size slide

  17. 13/27
    Libraries
    Roboguice
    • Android oriented
    • It uses Guice, a Google DI library
    • Does most of work at runtime
    • Really simple and powerful solution

    View full-size slide

  18. 13/27
    Libraries
    Roboguice
    • Android oriented
    • It uses Guice, a Google DI library
    • Does most of work at runtime
    • Really simple and powerful solution
    • Uses reflection heavily, being quite slow
    • Uses Guava, with thousand of methods,
    contributing to reaching the dex limit and
    big APKs

    View full-size slide

  19. 14/27
    Libraries
    Dagger
    • Java and Android oriented
    • Does most of work at compile time
    • Reflection replaced by annotation processing
    • Great performance

    View full-size slide

  20. 14/27
    Libraries
    Dagger
    • Java and Android oriented
    • Does most of work at compile time
    • Reflection replaced by annotation processing
    • Great performance
    • Harder to use and require boiler plate code
    • Dependency Injection graph is completely static,
    defined at compile time

    View full-size slide

  21. 15/27
    Libraries
    High level overview
    Ease of
    use
    Speed
    Roboguice
    Dagger

    View full-size slide

  22. 15/27
    Libraries
    High level overview
    Ease of
    use
    Speed
    Roboguice
    Dagger
    Toothpick

    View full-size slide

  23. 16/27
    Toothpick
    What is Toothpick?
    • Scope tree based Dependency Injection library
    • Full featured implementation of the JSR 330
    • Runtime based, but does not use reflection. Replaced
    by annotation processing
    • Almost as fast as Dagger, even faster in some cases
    • As simple as Roboguice, with few rules
    • Advanced testing support

    View full-size slide

  24. 16/27
    Toothpick
    What is Toothpick?
    • Scope tree based Dependency Injection library
    • Full featured implementation of the JSR 330
    • Runtime based, but does not use reflection. Replaced
    by annotation processing
    • Almost as fast as Dagger, even faster in some cases
    • As simple as Roboguice, with few rules
    • Advanced testing support

    View full-size slide

  25. Toothpick
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    17/27

    View full-size slide

  26. Toothpick
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    17/27
    Specify
    Dependencies

    View full-size slide

  27. Toothpick
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    17/27
    Specify
    Dependencies
    Inject all
    dependencies
    and
    transitive
    dependencies

    View full-size slide

  28. Toothpick
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    18/27

    View full-size slide

  29. Toothpick
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    18/27
    Transitive
    Dependencies

    View full-size slide

  30. Toothpick
    Snow Cone Machine
    • Dependencies: Ice Machine + Syrup Dispenser
    18/27
    Annotated
    Constructor
    Transitive
    Dependencies

    View full-size slide

  31. 19/27
    Toothpick
    Snow Cone
    Machine
    Syrup
    Dispenser
    Snow Cone Machine
    • Transitive dependencies
    Ice
    Machine
    Pump
    Transitive
    Dependency

    View full-size slide

  32. 19/27
    Toothpick
    Snow Cone
    Machine
    Syrup
    Dispenser
    Snow Cone Machine
    • Transitive dependencies
    Ice
    Machine
    Pump
    Transitive
    Dependency

    View full-size slide

  33. 20/27
    Toothpick Example
    Requirements
    • Different flavor Snow Cone Machines: Lemon and
    Apple
    • Only one Ice Machine, it is shared
    • It should be an easy and fast solution:

    View full-size slide

  34. 20/27
    Toothpick Example
    Requirements
    • Different flavor Snow Cone Machines: Lemon and
    Apple
    • Only one Ice Machine, it is shared
    • It should be an easy and fast solution:
    Toothpick to the rescue!!

    View full-size slide

  35. 21/27
    Toothpick Example
    2 Snow Cone Machines: Lemon and Apple
    • Scopes are used to define overrides: bindings
    • Bindings are attached to the Scopes through modules

    View full-size slide

  36. 21/27
    Toothpick Example
    2 Snow Cone Machines: Lemon and Apple
    • Scopes are used to define overrides: bindings
    • Bindings are attached to the Scopes through modules

    View full-size slide

  37. 21/27
    Toothpick Example
    2 Snow Cone Machines: Lemon and Apple
    • Scopes are used to define overrides: bindings
    • Bindings are attached to the Scopes through modules

    View full-size slide

  38. 22/27
    Toothpick Example
    2 Snow Cone Machines: Lemon and Apple

    View full-size slide

  39. 23/27
    Toothpick Example
    Scope tree based
    D -> E
    X -> Y
    A -> B
    T -> Z

    View full-size slide

  40. 24/27
    Toothpick Example
    Only one Ice Machine, it is shared
    • The Ice Machine is a Singleton
    • Use @Singleton annotation

    View full-size slide

  41. 25/27
    Bonus: Advanced scenarios
    • MVP, keeping the presenter state
    • Nowadays -> Retained fragments, Loaders, …
    • Toothpick -> Use a scope to retain the instance of
    the Presenter
    • User flow data
    • Nowadays -> Send data over and over through
    Intents, Database, …
    • Toothpick -> Use a common scope for the whole
    flow to keep flow related data

    View full-size slide

  42. 26/27
    Conclusion
    • Toothpick is scope tree based DI framework
    • Advanced testing support
    • Nice scenarios: MVP, Flow Scope, …
    • Fast and simple

    View full-size slide

  43. 26/27
    Conclusion
    • Toothpick is scope tree based DI framework
    • Advanced testing support
    • Nice scenarios: MVP, Flow Scope, …
    • Fast and simple How Fast?

    View full-size slide

  44. 26/27
    Conclusion
    • Toothpick is scope tree based DI framework
    • Advanced testing support
    • Nice scenarios: MVP, Flow Scope, …
    • Fast and simple How Fast?

    View full-size slide

  45. 27/27
    Conclusion
    1000 injections
    • Dagger 1: 33 ms
    • Dagger 2: 31 ms
    • Toothpick: 35 ms
    6400 injections
    • Dagger 1: 45 ms
    • Dagger 2: 42 ms
    • Toothpick: 66 ms

    View full-size slide

  46. The Android Team is Hiring
    https://jobs.groupon.com/#/categories/engineering

    View full-size slide