Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection on Android

Dependency Injection on Android

Unit testing is a crucial tool for building quality software. In order to be able to write unit tests, your classes need to operate independently. Dependency injection is a technique that provides the decoupling your classes need to be easily unit tested. We’ll explore the concept of dependency injection, designing classes using the technique and apply it directly to Android development. The session will also cover the popular Android-focused dependency injection frameworks RoboGuice and Dagger, looking at how those tools make dependency injection easier and cleaner.

Avatar for David Truxall

David Truxall

May 17, 2014
Tweet

More Decks by David Truxall

Other Decks in Programming

Transcript

  1. Definition Method for providing a class objects it depends upon

    instead of the class creating those objects
  2. Preliminary Concepts - Interfaces public interface DataProvider { void addTask(ToDo

    item); void deleteAll(); void deleteTask(final long id); List<ToDo> findAll(); }
  3. Preliminary Concepts - Mock Objects Simulated objects that mimic the

    behavior of real objects in controlled a way
  4. public class MockDataSource implements DataProvider { @Override public List<ToDo> findAll()

    { List<ToDo> data = new ArrayList<ToDo>(); ToDo item = new ToDo(); item.setId(11l); item.setTitle("Title 1"); data.add(item); // Add more objects... return data; } // Override More methods>.... }
  5. public interface Bird { public String call(); public void fly();

    } public class Crow implements Bird { @Override public String call() { return "Caw Caw"; } @Override public void fly() { flapWings(); } //More code below... }
  6. public String birdCall(String birdType){ Bird bird = null; if(birdType.equals("Owl")) {

    bird = new Owl(); } else if(birdType.equals("BlueJay")) { bird = new BlueJay(); } else if(birdType.equals("Crow")) { bird = new Crow(); } if(bird == null) {return "";} return bird.call().toUpperCase(); } BAD!
  7. Constructor public class Example { private Bird mBird; public Example(Bird

    bird) { mBird = bird; } public String birdCall(){ return mBird.call().toUpperCase(); } }
  8. Setter public class Example { private Bird mBird; public void

    setBird(Bird bird) { mBird = bird; } public String birdCall(){ return mBird.call().toUpperCase(); } }
  9. @Override protected void onCreate(Bundle savedInstanceState) { // extend the application-scope

    object graph with // the modules for this activity mObjectGraph = ((Injector) getApplication()).getObjectGraph(); mObjectGraph.plus(getModules()); mObjectGraph.inject(this); super.onCreate(savedInstanceState); //Do your other Activity stuff now... }
  10. Resources Inversion of Control Containers and the Dependency Injection pattern

    A Beginner's Guide to Dependency Injection Clean Code in Android Applications When To Mock Create a new Android project with Robolectric unit test suppor fb-android-dagger Dependency Injection with Dagger on Android Android Apps with Dagger