Hi all.....
I recently came to learn about a new technology and frameworks which i gonna share about here........
First tech is Guice.....
Guice is a DI(Dependency Injection) Framework. There are many DI frameworks, Guice is one of them which is used by google.
Advantage of using DI, increases the performance of the system and it's easy to learn.
Before explaining about Guice , first i'll explain why we go for DI.
In OOPs language we use classes, interface and concepts like inheritance to increase the performance but is it really increasing the performance of the system, the answer is partly no. but how?
lets see an example explaining this slow performance
Suppose we have four classes and name it A,B,C and D.
In which we inherited A in B, B in C, and C in D.
1.Here the class D may not use methods of A and B instead memory is allocated for all the methods inside D
2.Suppose class D wants to use class A's method, it has to move from bottom to top hirerachy.
because of these reasons , obviously reducing the performance we go for DI.
So now we see what is DI and using a simple DI usage.
Here we are creating a dependency for a class only if it is neccesary using interfaces. so unneccessary hirerachy is removed.
Which obviously increases the performance.
Now we see a simple example
1. Create an interface.
package add.service;
I recently came to learn about a new technology and frameworks which i gonna share about here........
First tech is Guice.....
Guice is a DI(Dependency Injection) Framework. There are many DI frameworks, Guice is one of them which is used by google.
Advantage of using DI, increases the performance of the system and it's easy to learn.
Before explaining about Guice , first i'll explain why we go for DI.
In OOPs language we use classes, interface and concepts like inheritance to increase the performance but is it really increasing the performance of the system, the answer is partly no. but how?
lets see an example explaining this slow performance
Suppose we have four classes and name it A,B,C and D.
In which we inherited A in B, B in C, and C in D.
1.Here the class D may not use methods of A and B instead memory is allocated for all the methods inside D
2.Suppose class D wants to use class A's method, it has to move from bottom to top hirerachy.
because of these reasons , obviously reducing the performance we go for DI.
So now we see what is DI and using a simple DI usage.
Here we are creating a dependency for a class only if it is neccesary using interfaces. so unneccessary hirerachy is removed.
Which obviously increases the performance.
Now we see a simple example
1. Create an interface.
package add.service;
public interface Add {
public int add(int a, int b);
}2.Create a class which implements the interfacepackage add.service;public class SimpleAdd implements Add{
public int add(int a, int b) {
return a + b;
}
}
3. Now we are going to inject the dependency to the class by creating a Modulepackage add.service;import com.google.inject.Binder;
import com.google.inject.Module;
public class AddModule implements Module{
public void configure(Binder binder) {
binder.bind(Add.class).to(SimpleAdd.class);
}
}
4. Now we are creating the client and using the DI
package add.service;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class AddClient {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new AddModule());
Add add = injector.getInstance(Add.class);
System.out.println(add.add(10, 54));
}
}
So the Add functionality is directly linked using the binder and doing so increaes the performance. But we can't bind a single interface to multiple classes as same a s above.So how to bind mulitple classes? will be posted in future.