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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Sato Shun
May 13, 2015
Programming
180
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
RTSPクライアントを自作してみた話
simotin13
0
600
Webフレームワークの ベンチマークについて
yusukebe
0
160
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.8k
Lessons from Spec-Driven Development
simas
PRO
0
190
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
780
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
210
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
190
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
230
New "Type" system on PicoRuby
pocke
1
860
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
170
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
Site-Speed That Sticks
csswizardry
13
1.2k
WCS-LA-2024
lcolladotor
0
630
Claude Code のすすめ
schroneko
67
230k
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
240
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
Balancing Empowerment & Direction
lara
6
1.2k
We Are The Robots
honzajavorek
0
250
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