Introduction to DI
In this section, we will look at what DI is, the benefits it provides, and how this concept is applied to an Android application. We will then look at some DI libraries and how they work.
When a class depends on functionality from another class, a dependency is created between the two classes. To invoke the functionality on the class you depend on, you will need to instantiate it, as in the following example:
class ClassA() { private val b: ClassB = ClassB() fun executeA() { b.executeB() } } class ClassB() { fun executeB() { } }
In this example, ClassA
creates a new instance of ClassB
, and then when executeA
is invoked, it will invoke executeB
. This poses a problem because ClassA
will have the extra responsibility of creating ClassB
. Let&apos...