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
57
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
220
Toothpick & Dependency Injection
dlemures
1
2k
Other Decks in Programming
See All in Programming
CSC509 Lecture 14
javiergs
PRO
0
140
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
Zoneless Testing
rainerhahnekamp
0
120
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
命名をリントする
chiroruxx
1
390
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
useSyncExternalStoreを使いまくる
ssssota
6
1k
선언형 UI에서의 상태관리
l2hyunwoo
0
150
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Agile that works and the tools we love
rasmusluckow
328
21k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Building Applications with DynamoDB
mza
91
6.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Building an army of robots
kneath
302
44k
Building Adaptive Systems
keathley
38
2.3k
Music & Morning Musume
bryan
46
6.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Faster Mobile Websites
deanohume
305
30k
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