Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Toothpick - A fresh approach to DI
Search
Daniel Molinero Reguera
November 04, 2016
Programming
0
120
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
Share
More Decks by Daniel Molinero Reguera
See All by Daniel Molinero Reguera
TP3 & KTP: Simple, fast, and boilerplate-free DI for Kotlin
dlemures
0
52
Toothpick - A fresh approach to DI - Short Version
dlemures
0
53
Toothpick - A fresh approach to DI (Including Unit Testing)
dlemures
1
1.1k
Toothpick Bad Practices 🙅 and Nice Tricks 👌
dlemures
0
91
Dart & Henson - Better Android Intents
dlemures
1
210
Toothpick & Dependency Injection
dlemures
1
2k
Other Decks in Programming
See All in Programming
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
C#/.NETのこれまでのふりかえり
tomokusaba
1
150
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
380
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.5k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
240
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
役立つログに取り組もう
irof
26
8.5k
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
7
2.7k
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
7
420
offers_20241022_imakiire.pdf
imakurusu
2
360
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
1.7k
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Scaling GitHub
holman
458
140k
How to Ace a Technical Interview
jacobian
275
23k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Thoughts on Productivity
jonyablonski
67
4.3k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Practical Orchestrator
shlominoach
186
10k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
Producing Creativity
orderedlist
PRO
341
39k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Transcript
A FRESH APPROACH TO DI TOOTHPICK https://github.com/stephanenicolas/toothpick/
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
5IF"OESPJE5FBNJT)JSJOH jobs.groupon.com/careers/
THE TALK WHY DI ? WHY TOOTHPICK? TOOTHPICK FEATURES
ADVANCED FEATURES CONCLUSION SCOPE MODULES & BINDINGS SCOPE TREE SCOPE SINGLETONS MVP & STATE PRESERVATION MULTIPLE ACTIVITIES FLOWS
WHY DI ?
WHY DI ? DECOUPLE
WHY DI ? DECOUPLE USES
WHY DI ? DECOUPLE REUSE USES
WHY DI ? DECOUPLE REUSE USES USES USES
WHY DI ? DECOUPLE REUSE TEST USES USES USES
WHY DI ? DECOUPLE REUSE TEST USES USES USES MOCK
USES
WHY TOOTHPICK?
WHY TOOTHPICK? SIMPLER
WHY TOOTHPICK? SIMPLER BETTER TEST SUPPORT
WHY TOOTHPICK? SIMPLER BETTER TEST SUPPORT EVEN FASTER
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; }
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
TOOTHPICK FEATURES SCOPE SCOPE
TOOTHPICK FEATURES SCOPE SCOPE MAKE INJECTIONS
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); } }
TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE
TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE BINDINGS
TOOTHPICK FEATURES SCOPE, MODULES & BINDINGS SCOPE BINDINGS MODULES
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); } }
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY 1 SCOPE SCOPE TREE
TOOTHPICK FEATURES APPLICATION SCOPE SCOPE TREE
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY 2 SCOPE SCOPE TREE
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY 2 SCOPE SERVICE 1 SCOPE
SCOPE TREE
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY 2 SCOPE SERVICE 1 SCOPE
FRAGMENT 1 SCOPE SCOPE TREE
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY 2 SCOPE SERVICE 1 SCOPE
FRAGMENT 1 SCOPE FRAGMENT 2 SCOPE SCOPE TREE
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY 2 SCOPE SERVICE 1 SCOPE
FRAGMENT 3 SCOPE FRAGMENT 1 SCOPE FRAGMENT 2 SCOPE SCOPE TREE
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
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
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY SCOPE SCOPE SINGLETONS
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY SCOPE APP SINGLETONS SCOPE SINGLETONS
TOOTHPICK FEATURES APPLICATION SCOPE ACTIVITY SCOPE APP SINGLETONS ACTIVITY SINGLETONS
SCOPE SINGLETONS
TOOTHPICK FEATURES public class Module extends Module { public Module()
{ bind(Machine.class).toInstance(new Ice Machine()); } } SCOPE SINGLETONS SINGLETONS CAN BE DEFINED IN MODULES
TOOTHPICK FEATURES @Singleton public class IceMachine { } @ActivitySingleton public
class SmoothieMachine { } APPLICATION SCOPE ACTIVITY SCOPE SCOPE SINGLETONS OR USING ANNOTATIONS
ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION
ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION
ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION
APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION ACTIVITY
SCOPE
APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES MVP & STATE PRESERVATION ACTIVITY
SCOPE
APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP & STATE
PRESERVATION ACTIVITY SCOPE
PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP
& STATE PRESERVATION ACTIVITY SCOPE
PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP
& STATE PRESERVATION
PRESENTER INSTANCE APPLICATION SCOPE PRESENTER SCOPE ADVANCED TOOTHPICK FEATURES MVP
& STATE PRESERVATION ACTIVITY SCOPE
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); } }
@PresenterSingleton public class MyPresenter { String dealId; int quantity; }
PRESENTER SCOPE MVP & STATE PRESERVATION ADVANCED TOOTHPICK FEATURES
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS Purchase Flow
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS Purchase Flow
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS
ACTIVITY 1 ADVANCED TOOTHPICK FEATURES ACTIVITY 3 ACTIVITY 2 MULTI
ACTIVITY FLOWS
APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE
MULTI ACTIVITY FLOWS
APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE
MULTI ACTIVITY FLOWS
APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE
ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS
APPLICATION SCOPE ACTIVITY 1 SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE
ACTIVITY 3 SCOPE ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS
APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES FLOW SCOPE MULTI ACTIVITY FLOWS
APPLICATION SCOPE ADVANCED TOOTHPICK FEATURES MULTI ACTIVITY FLOWS
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); } }
@FlowSingleton public class ShoppingCart { List<PurchaseItem> purchases… } FLOW SCOPE
MULTI ACTIVITY FLOWS ADVANCED TOOTHPICK FEATURES
CONCLUSION QUESTIONS ? COMMENTS ?
CONCLUSION QUESTIONS ? COMMENTS ?
A FRESH APPROACH TO DI TOOTHPICK https://github.com/stephanenicolas/toothpick/ +stephane @D_Lemures