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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
160
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
4
640
スマートグラスで並列バイブコーディング
hyshu
0
140
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
5.9k
net-httpのHTTP/2対応について
naruse
0
480
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
10
4.1k
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
540
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
RTSPクライアントを自作してみた話
simotin13
0
600
New "Type" system on PicoRuby
pocke
1
920
AI時代のUIはどこへ行く?その2!
yusukebe
21
7.1k
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
240
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
140
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
720
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Writing Fast Ruby
sferik
630
63k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
Optimizing for Happiness
mojombo
378
71k
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