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
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
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
200
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
120
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.9k
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
110
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
290
SLOをサービス品質の共通言語にするために 取り組んできたこと
wakana0222
0
360
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.6k
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
830
AIエージェントで 変わるAndroid開発環境
takahirom
1
430
act1-costs.pdf
sumedhbala
0
170
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
110
スマートグラスで並列バイブコーディング
hyshu
0
280
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
410
Chasing Engaging Ingredients in Design
codingconduct
0
230
Unsuck your backbone
ammeep
672
58k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
310
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
180
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
550
Joys of Absence: A Defence of Solitary Play
codingconduct
1
410
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
170
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
170
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