Dagger Binds

This is my note for Udemy lesson: Advanced Android Bootcamp 2024.
Please read last article before reading this one.
Assuming class SmartPhone has another dependency Battery, and Battery is an interface, NickelCadmiumBattery is its implementation, we can use @Binds to bind interface and implementation.

1. Create Interface and Implementation

interface Battery{
    fun getPower()
}

class NickelCadmiumBattery @Inject constructor(): Battery {
    override fun getPower() {
        Log.i("MYTAG", "Power from NickelCadmiumBattery")
    }
}

2. Create NCBatteryModule

@Module
abstract class NCBatteryModule {
    @Binds
    abstract fun providesNcBattery(ncBattery: NickelCadmiumBattery): Battery
}

3. Modify SmartPhoneComponent

@Component(modules = [MemoryCardModule::class, NCBatteryModule::class])
interface SmartPhoneComponent {
    fun getSmartPhone(): SmartPhone
}