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 |
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...!! :)
Reading your blog left me with some useful info and this was an awesome blog.
ReplyDeleteSpoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
Best Spoken English Classes in Chennai
Best Spoken English Institute in Chennai
best body massager
ReplyDeletefriendship whatsapp groups
ReplyDeleteGreat Blog!!! Was an interesting blog with a clear concept. And will surely help many to update them.
ReplyDeleteAndroid Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery
Mmorpg Oyunlar
ReplyDeleteinstagram takipçi satın al
TİKTOK JETON HİLESİ
Tiktok Jeton Hilesi
antalya saç ekimi
referans kimliği nedir
instagram takipçi satın al
Mt2 Pvp Serverlar
instagram takipçi satın al
beykoz mitsubishi klima servisi
ReplyDeleteüsküdar mitsubishi klima servisi
pendik vestel klima servisi
tuzla samsung klima servisi
çekmeköy vestel klima servisi
ataşehir vestel klima servisi
çekmeköy bosch klima servisi
çekmeköy arçelik klima servisi
ataşehir arçelik klima servisi
Good content. You write beautiful things.
ReplyDeletetaksi
hacklink
mrbahis
vbet
vbet
sportsbet
mrbahis
hacklink
sportsbet
Success Write content success. Thanks.
ReplyDeletekıbrıs bahis siteleri
deneme bonusu
betpark
canlı poker siteleri
kralbet
betmatik
betturkey
thanks for sharing this is very helpful
ReplyDeletebetmatik
kralbet
betpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
L5MV4
elf bar
ReplyDeletebinance hesap açma
sms onay
SG3
slot siteleri
ReplyDeletebetpark
tipobet
mobil ödeme bahis
betmatik
kralbet
kibris bahis siteleri
poker siteleri
bonus veren siteler
DX0
Great post, keep posting more. Java Classes In Pune
ReplyDeleteçeşme
ReplyDeletemardin
başakşehir
bitlis
edremit
JKWLDW
salt likit
ReplyDeletesalt likit
12F
çorlu
ReplyDeleteniğde
urfa
aksaray
hatay
CJ68İ
bayburt
ReplyDeletebeykoz
beyoğlu
bilecik
bingöl
UQJQU
mecidiyeköy
ReplyDeletemaltepe
beşiktaş
alsancak
adana
Y71WSX
شركة تنظيف سجاد بالجبيل 7wdKXbQTg2
ReplyDelete