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
Android: Daggear2でDI
Search
Sato Shun
May 13, 2015
Programming
190
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
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.6k
Kotlin Fest 2019: Kotlin型実践入門
satoshun
9
7k
R8/ProGuard 徹底比較
satoshun
8
4.3k
Android: Introduction Android Support Annotations
satoshun
0
160
Other Decks in Programming
See All in Programming
数百円から始めるRuby電子工作
tarosay
0
120
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
370
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
620
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
200
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
2
310
地域 SRE コミュニティ最前線 - ホンマでっかSRE勉強会
tk3fftk
0
290
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
180
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
790
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
510
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
930
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.5k
『コードを書く以外の』エンジニアリング〜課金基盤移行プロジェクト推進のためのTips4選
yuriko1211
0
560
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
200
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.7k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
820
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
420
Building a Scalable Design System with Sketch
lauravandoore
463
34k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
45k
Product Roadmaps are Hard
iamctodd
55
12k
For a Future-Friendly Web
brad_frost
183
10k
Facilitating Awesome Meetings
lara
57
7k
The SEO identity crisis: Don't let AI make you average
varn
0
520
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