Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Android: Daggear2でDI
Search
Sato Shun
May 13, 2015
Programming
200
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Android: Daggear2でDI
Sato Shun
May 13, 2015
More Decks by Sato Shun
See All by Sato Shun
Kotlin Nativeにおけるfrozen状態と並行処理について
satoshun
0
1.1k
DroidKaigi 2020: System UIをコントロールして、 画面を最大限に生かしたアプリを構築する / Build apps that make the best uses of device screens by controlling System UI
satoshun
4
2k
タップルでたっぷりDaggerを使って マルチモジュール構築している話 :)
satoshun
2
2.7k
Kotlin Fest 2019: Kotlin型実践入門
satoshun
9
7.1k
R8/ProGuard 徹底比較
satoshun
8
4.3k
Android: Introduction Android Support Annotations
satoshun
0
160
Other Decks in Programming
See All in Programming
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
160
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
360
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
17k
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
200
気持ちぃ〜角丸 Squircle
ryu1013
0
320
Groovy as a human and AI friendly programming language
paulk
0
100
AI-Powered Domain-Driven Design
ziobrando
0
220
最近”作らせてる” ローカルAIアプリの話
akit37
0
490
phperだってパンチカードでコーディングしたい
o0h
PRO
0
380
残高管理から台帳サービスへの進化
artoy
1
210
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
180
Railsバージョンを4倍にした話
optim
0
520
Featured
See All Featured
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
280
We Are The Robots
honzajavorek
0
400
Discover your Explorer Soul
emna__ayadi
2
1.3k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
530
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
Facilitating Awesome Meetings
lara
57
7.2k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
310
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
sira's awesome portfolio website redesign presentation
elsirapls
0
460
Transcript
Dagger2ͰDI ʲୈ17ճʳpotatotips ࠤ౻ɹ൏ @satoshun_xyz
࠷ॳʹҰݴ • ઌݸਓతʹΞϓϦΛग़͠·ͨ͠
࠷ॳʹҰݴ reject͞Ε·ͨ͠….
࠷ॳʹҰݴ AndroidͰreject͞ΕΔ͜ͱ͕͋Γ·͢
࣍ • DIͱ? • Dagger2ͷجຊతͳ͍ํ
DIͱ? ֓ཁ ΫϥεؒͷґଘؔΛղܾ͢Δύλʔϯ ΫϥεC ΫϥεB ΫϥεA ΫϥεAΫϥεBʹ ґଘ͍ͯ͠Δ ΫϥεBΫϥεCʹ ґଘ͍ͯ͠Δ
DIͱ? ίʔυྫ class Blogger { public void post(String title) {
// Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ FileLogger logger = new FileLogger(“log.txt”); logger.logging(); } }
DIͱ? ίʔυྫ class Blogger { public void post(String title) {
// Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ FileLogger logger = new FileLogger(“log.txt”); logger.logging(); } }
DIͱ? ίʔυྫ class Blogger { FileLogger logger = new FileLogger(“log.txt”);
public void post(String title) { // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ logger.logging(); } }
DIͱ? ίʔυྫ class Blogger { FileLogger logger; public Blogger(FileLogger logger)
{ this.logger = logger; } public void post(String title) { // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ logger.logging(); } }
DIͱ? ίʔυྫ class Blogger { FileLogger logger; public Blogger(FileLogger logger)
{ this.logger = logger; } public void post(String title) { // Կ͔ϝΠϯͷॲཧ // ... // fileʹlogΛऔΔ logger.logging(); } } logger = new FileLogger(“log”); blogger = new Blogger(logger); blogger.post(“Hello World”); ֎෦͔Β ґଘΛೖ
DIͱ? ·ͱΊ • ֎෦͔ΒΠϯελϯεΛೖ͢Δ͜ͱʹΑΓ, ৼΔ ͍(ಈ࡞)Λίϯτϩʔϧ͢Δ͜ͱ͕ग़དྷΔ(ྫ͑ , ϙϦϞʔϑΟζϜ) • ͔͠͠ΫϥΠΞϯτଆͷෛ୲͕େ͖͘ͳͬͯ͠
·͏ logger = new FileLogger(“log”); blogger = new Blogger(logger); blogger.post(“Hello World”);
Dagger2ͷ͍ํ େ͖͚ͯ͘ComponentͱModuleͷ2͕ͭ͋Δ
Dagger2ͷجຊతͳ͍ํ public class MainActivity extends ActionBarActivity { SharedPreferences mSharedPreferences; Gson
gson; @Override protected void onCreate(Bundle saved) { mSharedPreferences = getSharedPreferences("app", Context.MODE_PRIVATE) gson = new GsonBuilder() .setFieldNamingPolicy( LOWER_CASE_WITH_UNDERSCORES) .create(); } }
Dagger2: ఆٛฤ public class MainActivity extends ActionBarActivity { @Inject SharedPreferences
mSharedPreferences; @Inject Gson gson; … }
Dagger2: Moduleฤ @Module public class ApplicationModule { private final AndroidApplication
application; public ApplicationModule(AndroidApplication application) { this.application = application; } @Provides Context provideContext() { return application; } @Provides @Singleton SharedPreferences provideSharedPreferences(Context context) { return context.getSharedPreferences("app", Context.MODE_PRIVATE); } } provideContextϝιου͔Β ࣗಈతʹҾ͕͞ΕΔ
Dagger2: Moduleฤ @Module public class DataModule { @Provides @Singleton Gson
provideGson() { return new GsonBuilder() .setFieldNamingPolicy( LOWER_CASE_WITH_UNDERSCORES) .create(); } }
Dagger2: Componentฤ @Singleton @Component(modules = { ApplicationModule.class, DataModule.class }) public
interface ApplicationComponent { void inject(MainActivity activity); } ΫϥεͰ͍͍ͨModuleΛ Componentʹ·ͱΊΔ
Dagger2: جຊ public class MainActivity extends ActionBarActivity { @Inject SharedPreferences
mSharedPreferences; @Inject Gson gson; … } ઌ΄ͲModuleͰఆٛ ͨ͠Πϯελϯε͕ Inject͞ΕΔ!
·ͱΊ • Dagger2Λ͏͜ͱͰʮઃఆΛར༻͔Β͢Δʯ ͕ग़དྷΔ • ςετ͘͢͠ͳΔ • Dagger1ΑΓDagger2ύϑΥʔϚϯε༏Ε͍ͯ Δ(Google͞Μ͍Θ͘) •
͜Ε͔ΒDagger2ͷ࣌(ͷҙݟ) • AndroidͰreject͞ΕΔ͜ͱ͕͋Δ
ࢀߟ • http://google.github.io/dagger/ • https://github.com/satoshun/dagger2-sample