Need help in explaining Java code involving Generics
I came across these lines of code:
import org.apache.shiro.util.Factory;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
.....
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
Can you explain this code i开发者_开发技巧n particular what Factory<SecurityManager>
means and why there is Generics reference <SecurityManager>
and why do I need it?
Generics are compile time markers to provide your compiler typing informations and templates. They don't exist in runtime. You could even ignore generic information in your code:
Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
In this case you get a small warning from your IDE (Eclipse) because of unchecked type, but you can supress it with SupressWarning annotation.
@SuppressWarnings("unchecked")
Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
Your code will be still compiled without any problem. For an example you have an Calculate class:
class Calculate<T extends Number> {
private T value;
public T add(T value1, T value2) {
return
}
}
that's a template and you can use your calculation utility with every type extends Number.
Calculate<Integer> calculator = new Calculate<Integer>();
calculator.add(1 , 2);
or a double version;
Calculate<Double> calculator = new Calculate<Double>();
calculator.add(1.2d , 2.3d);
Generics make your code flexible and let you write such templates.
Factory is a idiom in Software Engineering to create objects using these factory classes. I would make a research about Factory Pattern, Dependency Inversion , Holywood Principle. There are many threads around here about (Abstract) Factory Pattern or even on Wikipedia.
The generic reference is here used to avoid unchecked casts. This is what the generic-omitted equivalent might look like
Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = (SecurityManager) factory.getInstance();
So in this case, Factory<SecurityManager>
means that the factory should return a SecurityManager
instead of an Object
.
A factory class is a design pattern which is meant for a class that constructs instances of other classes. For example, a class called PointFactory would typically have a method called getinstance returning a new object of type Point. The factory class is used to hide the direct usage of the constructor of the class from you.
Sometime you have one factory class which can generate many types of objects. In this case we have a Factory class whose generic type is SecurityManager, and since the Factory class's getInstance method returns a new object from the generic type, then on the line below it in fact returns a new object of type ScurityManager.
The IniSecurityManagerFactory is class which extends Factory since as the name implies, it is a factory which is meant for generating SecurityManager's.
See http://en.wikipedia.org/wiki/Abstract_factory_pattern
Edit: added a bit more info
This is a common design pattern called Abstract Factory: http://en.wikipedia.org/wiki/Abstract_factory_pattern
And as far as i see you tell the factory, that you want to produce SecurityManagers
精彩评论