What is RoboGuice? A brief introduction to RoboGuice.

04:57 Posted by Unknown 17 comments

What is RoboGuice?

RoboGuice is a Dependency Injection framework which allows Inversion of Control. Now, thats complicated! What is this Dependency Injection (DI)? It is the technique of passing a dependency (a service) to a client (which needs this service for its own stuff) automatically.

RoboGuice is a type of Dependency Injection framework for Android which injects the required dependencies in the classes in our projects. The roots of RoboGuice are in the famous Google Guice, which can be Googled easily.
RoboGuice
RoboGuice

How does it work?

The RoboGuice DI framework recognizes the required dependencies of your class through an annotation @Inject. Then it initializes those objects for us and injects them in our class.

Let us take an example (This example is just to explain the concepts. Real coding differs):

Say, I want to construct a car object. In order to construct it, I need the Wheels class and Color class.

The conventional way:
class Wheels {  
    int noOfWheels = 4;  
}
  
class Color {  
    String color = "red";  
}
  
class Car {  
    Wheels wheels;  
    Color color;  

    public Car(Wheels wheels, Color color) {  
        this.wheels = wheels;  
        this.color = color;  
    }  

    public void manufacture() {  
        fixWheels(wheels);  
        paintColor(color);  
    }  
}

class CarFactory {
    
    public void manufactureCar() {
        // Manufacture a new car here
        Wheels wheels = new Wheels();
        Color color = new Color();
        Car car = new Car(wheels, color);
        car.manufacture();
    }

}

The RoboGuice way:
class Wheels {  
    int noOfWheels = 4;  
}
  
class Color {  
    String color = "red";  
}
  
class Car {  
    Wheels wheels;  
    Color color;  

    @Inject
    public Car(Wheels wheels, Color color) {  
      this.wheels = wheels;  
      this.color = color;  
    }  

    public void manufacture() {  
      fixWheels(wheels);  
      paintColor(color);  
    }  
}

class CarFactory {
    
    @Inject Car car;

    public void manufactureCar() {
        // Manufacture a new car here
        car.manufacture();
        // See the difference, no need to initialize anything
        // DI takes care of everything...!!
    }

}

Here, RoboGuice identifies that Wheels and Color are required for the Car object and constructs them automatically and injects them in the Car class. Isn't it fantastic...?? For me its amazing stuff !!

Its all good. But I can still create the dependencies as per the conventional way. But why should I go with RoboGuice? Well, the answer is that as your code grows, the number of dependencies as well as the complexity of those dependencies grows. Let me explain you.

Say, as per the latest requirements, if the Wheels class requires a Wheel type (say, Alloy), then how would you go about it? You should change the Wheels class as well as the all the places where the Wheels object is created in the conventional coding. Now, imagine if your Wheels class is used by some tons of classes, you should go and change all of them. This is something very serious and hectic. Something like this:

class Wheels {  
    AlloyWheelType alloyWheelType;
    int noOfWheels = 4;

    public Wheels(AlloyWheelType alloyWheelType) {
        this.alloyWheelType = alloyWheelType
    }  
}
 
class CarFactory {
    
    public void manufactureCar() {
        // Note that you have to create AlloyWheelType instance
        // and also have to change the Wheels object creation.
        AlloyWheelType alloyWheelType = new AlloyWheelType();
        Wheels wheels = new Wheels(alloyWheelType);
        Color color = new Color();
        Car car = new Car(wheels, color);
        car.manufacture();
    }

}

Wont it be good if someone manages all these things for us? Here comes our saviour RoboGuice for us...!! It manages all those tons of classes for us...!! Here's how it does:

class Wheels {  
    AlloyWheelType alloyWheelType;
    int noOfWheels = 4;

    @Inject // Injects this automatically
    public Wheels(AlloyWheelType alloyWheelType) {
        this.alloyWheelType = alloyWheelType
    }  
}
 
class CarFactory {
    
    @Inject Car car;

    public void manufactureCar() {
        // You dont have to change anything at all...!!
        car.manufacture();
    }

}

So, if you use RoboGuice, you can manage all these kinds of dependencies and complexities easily in your Android projects for all the hundreds and thousands of classes you write. Also, it provides loose coupling between various modules of your project and hence maintenance of your code and quality improves.

Earlier in my Android projects, everytime I write a helper class or a class to delegate a task, I had to manually pass the Context object to all those classes. Thanks to RoboGuice, now it does it for me everytime.

Please provide your feedback below. Thank you!

HAPPY CODING...!! :)

17 comments: