#1 ์ด์ ๊ธ
[Android] Dagger2 - ๊ธฐ์ด
#1 ์ด์ ๊ธ ์์กด์ฑ ์ฃผ์ (Dependency Injection)#1 ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ด๋?#1-1 Dependent์ Dependencyfun main() { val car = Car() car.startCar() } class Car { private val engine = Engine() private val airbag = Airbag() private
kenel.tistory.com
์ ๊ฒ์๊ธ์ ์์ฑ๋ ์ฑ์ ์ผ๋ถ ์์ ํด์, 'Activity์ ์์กด์ฑ์ ์ฃผ์ 'ํด๋ณธ๋ค. ์ด ๋ง์ ์๋ฏธ๋ #3-1์ ๋์จ๋ค.
#2 @Component ์ธํฐํ์ด์ค ์ getter์ ๋ถํธํจ
// package com.example.activitydependencyinjection
import dagger.Component
@Component
interface CarComponent {
fun getCar(): Car
// fun getMotorcycle(): Motorcycle
// fun getBicycle(): Bicycle
// ...
}
์ด์ ๊ธ์ @Component๋ ์์ ๊ฐ์ด getCar()๋ผ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค. ํ์ง๋ง, ํ๋ก์ ํธ๊ฐ ์ ์ ์ปค์ง๋ฉด getMotorcycle(), getBicycle() ๋ฐ์์ getter ํจ์๋ค์ด ๊ณ์ ๋์ด๋ ๊ฒ์ด๋ค. ์ด๋ ๊ฒ ๋๋ฉด, ํ์ํ Dependent๋ฅผ ์ฐพ๊ธฐ ์ํด์ getter ํจ์ ๋ชฉ๋ก์ ๋ค์ ๊ฑฐ๋ฆฌ๋ ๋นํจ์จ์ ์ธ ์ํฉ์ด ์ด๋๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ dagger๋ Activity์ ์์กด์ฑ์ ์ฃผ์ ํ๋ ๊ธฐ๋ฅ์ ์ง์ํ๋ค.
#3 Activity์ Dependency๋ฅผ ์ฃผ์ ํ๊ธฐ
#3-1 @Component ์ธํฐํ์ด์ค ์์
// package com.example.activitydependencyinjection
import dagger.Component
@Component
interface CarComponent {
fun inject(mainActivity: MainActivity)
}
๊ทธ๊ฒ์ ๋ฐ๋ก ์ ์ฝ๋์ฒ๋ผ ์ปดํฌ๋ํธ ํด๋์ค๋ฅผ ์์ ํ๋ ๊ฒ์ด๋ค. inject()๋ getter ํจ์๋ค์ ํ๋์ ํจ์๋ก ํตํฉํ ํจ์๋ค. getCar()์ ๊ฒฝ์ฐ Car๋ฅผ ์ต์ข Dependent๋ก ์ผ์ ์์กด์ฑ ์ฃผ์ ์ ์ํํ์๋ค. inject() ๋ํ MainActivity๋ฅผ ์ต์ข Dependent๋ก ์ผ์ ์์กด์ฑ ์ฃผ์ ์ ์ํํ๋ค๋ ์๋ฏธ๋ค.
ํ์ง๋ง inject()๋ getCar()์ ๋ฌ๋ฆฌ ๋ฐํํ์ด ์๋ค. ์ด๋ inject()์ ๋ชฉ์ ๊ณผ ๊ด๋ จ์ด ์๋ค. inject()๋ MainActivity๋ผ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ ๋ชฉ์ ์ด ์๋๋ผ, ์ด๋ฏธ ์์ฑ๋ MainActivity์ ํํ์ ์ผ๋ก Dependency๋ฅผ ์ฃผ์ ํ๋ ๋ฉ์๋๋ค. ๋ฐ๋ผ์ MainActivity์ Dependency๋ค์ MainActivity์ ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํ๋์ง ์๊ณ , lateinit varํ์ผ๋ก ์ ์ธ๋์ด์ผ ํ๋ค. #3-2์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ํด๋น Dependency๋ฅผ ํ์ธํ ์ ์๋ค.
Car์ Dependency๋ค์ธ Airbag, Battery, Engine์ @Inject ์ด๋ ธํ ์ด์ ์ ํตํด ์์กด์ฑ ์ฃผ์ ์ (dagger ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ์ ์๊ฒ) ๋ช ์ํํ๋ ๊ฒ์ฒ๋ผ, MainActivity์ Dependency๋ค์๊ฒ๋ ์์กด์ฑ ์ฃผ์ ๋์์์ ๋ช ์ํํด์ผ ํ๋ค. ์๋ ์ฝ๋๋ฅผ ๋ณด์.
#3-2 MainActivity.kt ์์
// package com.example.activitydependencyinjection
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import javax.inject.Inject
class MainActivity : AppCompatActivity() {
@Inject
lateinit var car: Car
override fun onCreate(savedState: Bundle?) {
...
/*
...
*/
DaggerCarComponent.create().inject()
car.startCar()
}
}
์ด๋ ๊ฒ lateinit varํ ํ๋กํผํฐ๋ฅผ ์ ์ธํ๊ณ @Inject ์ด๋ ธํ ์ด์ ์ ๋ถ์ธ๋ค. @Inject๊ฐ ์์ฑ์์ ๋ถ์ ๊ฒฝ์ฐ์๋ Constructor Injection(์ด ๋งํฌ์ #2-1 ์ฐธ์กฐ)์ด ์ํ๋์๋ ๊ฒ์ฒ๋ผ, @Inject๊ฐ ํ๋(์ฝํ๋ฆฐ์ Field๊ฐ ์์ผ๋ฏ๋ก, ๊ทธ ๋์ ํ๋กํผํฐ)์ ๋ถ์ ๊ฒฝ์ฐ์๋ Field Injection(์ด ๋งํฌ์ #2-3 ์ฐธ์กฐ)์ด ์ํ๋๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก, Activity์์ ์ฌ์ฉํ Dependent๋ค์ @Inject ์ด๋ ธํ ์ด์ ๊ณผ ํจ๊ป ์ ์ธ๋ง ํด๋๋ฉด, inject() ๋ฉ์๋์ ์ํด Dependency๊ฐ ์์์ ์ฃผ์ ๋๋ค. getter๋ฅผ ์ผ์ผํ ์ฌ์ฉํ ํ์๊ฐ ์๋ค.
#3-3 ์์กด์ฑ ๊ทธ๋ํ
Activity์ ์์กด์ฑ ์ฃผ์ ์ ํ๋ ๊ฒฝ์ฐ๋ฅผ ์์กด์ฑ ๊ทธ๋ํ๋ก ๋ํ๋ด๋ฉด ์์ ๊ฐ๋ค. ๋ณธ ๊ฒ์๊ธ์ MainActivity๊ฐ ์ค์ ๋ก Motorcycle์ด๋ Bicycle ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒ์ ์๋์ง๋ง, ์ดํด๋ฅผ ๋๊ธฐ ์ํด ์๋ ๊ฒ์ฒ๋ผ ํฌํจ์ํจ ๊ทธ๋ํ๋ค.
#4 ์๋ ํ์ธ (๋ก๊ทธ ๋ฉ์์ง)
Crankshaft is ready
Cylinder is ready
Piston is ready
Engine is ready
Airbag is ready
Battery is ready
Car is ready
#5 ์์ฝ
์กํฐ๋นํฐ๋ Dependent๊ฐ ๋ ์ ์๋ค.
#6 ์์ฑ๋ ์ฑ
android-practice/dagger2/ActivityDependencyInjection at master · Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Unit Testing - ๊ฐ์์ ํ๊ฒฝ ์ค์ (2) | 2024.07.03 |
---|---|
[Android] Dagger2 - Application ํ์ฉํ๊ธฐ (0) | 2024.06.25 |
[Android] Dagger2 - ์ธํฐํ์ด์ค ๊ตฌํ์ฒด ์ฃผ์ (@Binds) (0) | 2024.06.25 |
[Android] Dagger2 - ๋งค๊ฐ๋ณ์ ๋์ ํ ๋น (0) | 2024.06.24 |
[Android] Dagger2 - @Provides (0) | 2024.06.24 |