From the course: Android App Development: Design Patterns for Mobile Architecture

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Dependency injection

Dependency injection - Android Tutorial

From the course: Android App Development: Design Patterns for Mobile Architecture

Start my 1-month free trial

Dependency injection

- [Narrator] The next thing we need to do is decouple our classes from the creation of any other dependency. If you look in the model layer this is a great example. On line 19 through 21 you can see that we are newing up each of those layers within this class, and that just means that we are tightly coupled within this class. We're going to use dependency injection to solve this, which is basically us injecting each of our dependencies through either a constructor method or some other method that we're able to get access to. The reason why we can't always use the constructor method is in activities they're created by intents, which means that we don't actually have any control over the creation of that. But, we can have them ask the registry to register them as one of the first things and call some sort of configure method. So first, let's go ahead and create that dependency registry. We're going to come in here and just create a new package. We'll just call this Dependencies. If you…

Contents