dependency injection in android with dagger

24
Dependency Injection with Dagger by Lope Emano

Upload: lope-emano

Post on 22-Jun-2015

817 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Dependency Injection in Android with Dagger

Dependency Injection with Dagger

by Lope Emano

Page 2: Dependency Injection in Android with Dagger

What is Dependency Injection?

Page 3: Dependency Injection in Android with Dagger

is a software design pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client's state. The pattern separates the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the dependency inversion and single responsibility principles.

-Wikipedia

Page 4: Dependency Injection in Android with Dagger
Page 5: Dependency Injection in Android with Dagger

- James Shore

Dependency Injection is a 25-dollar term for a 5-cent concept.Dependency injection means giving an object its instance variables.

Page 6: Dependency Injection in Android with Dagger

How to explain dependency injection to a 5-year old?

Page 7: Dependency Injection in Android with Dagger

When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired.

What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.

Page 8: Dependency Injection in Android with Dagger

“Dependency injection isn’t an end goal, it is a means to an end - a means to achieving loosely coupled code”

Page 9: Dependency Injection in Android with Dagger

Loose Coupling

Page 10: Dependency Injection in Android with Dagger

What do you do when your laptop keyboard gets broken?

Page 11: Dependency Injection in Android with Dagger

Tightly coupled code

1. Unscrew laptop2. Remove cover3. Remove broken keyboard4. Position and install new keyboard to data cable5. Close cover6. Screw laptop

Page 12: Dependency Injection in Android with Dagger

Loosely coupled code1. Plug external keyboard to usb port

Page 13: Dependency Injection in Android with Dagger

The usb port1. Is agnostic to any device that connects to it as long as it

satisfies its specifications2. USB 3.0 ports are USB 2.0 compatible3. Today you use it for just about anything- mouse,

trackpad, coffee heaters, lighting, charging phones, android development, etc..

Page 14: Dependency Injection in Android with Dagger

Let’s see what this looks like in pseudocode! :)

Page 15: Dependency Injection in Android with Dagger

interface USBPluggable{openInputStream(), deviceType()}

class Keyboard implements USBPluggable {}class AndroidPhoneWire implements USBPluggable {}class CoffeeCupHeater implements USBPluggable {}class Keyboard implements USBPluggable {}

Page 16: Dependency Injection in Android with Dagger

interface USBPluggable{openInputStream(), deviceType()}

class Keyboard implements USBPluggable {}class AndroidPhoneWire implements USBPluggable {}class CoffeeCupHeater implements USBPluggable {}class Keyboard implements USBPluggable {}

Page 17: Dependency Injection in Android with Dagger

Code depends upon abstractions instead of implementations

class Computer(){Computer(USBPluggable port1){

port1.openInputSream();port1.deviceType();

}}

Page 18: Dependency Injection in Android with Dagger

Code that depends upon implementation instead of abstraction

class Computer(){Computer(Keyboard keyboardPort){

KeyboardInputStream ks = keyboardPort.openInputSream();InputStream is = ComputerUtils.convertStream(ks);

}}

Page 19: Dependency Injection in Android with Dagger

Wait, so what does dagger have to do with this?

Page 20: Dependency Injection in Android with Dagger

The problem

We need to figure out the dependencies a class needs

We need to figure out how to instantiate them

Page 21: Dependency Injection in Android with Dagger

A Solution : Dagger

We need to figure out the dependencies a class needs

We need to figure out how to instantiate them

Uses @Inject annotation to specify that this object needs to be injected with a provided instance

Uses @Modules and @Provides annotations to instantiate objects that are to be injected

Page 22: Dependency Injection in Android with Dagger

That’s it! Now let’s have a look at documentation

http://square.github.io/dagger/

Page 23: Dependency Injection in Android with Dagger

Non-demo code demo! :)