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 in 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:
In this case you get a small warning from your IDE (Eclipse) because of unchecked type, but you can supress it with SupressWarning annotation.
Your code will be still compiled without any problem.
For an example you have an Calculate class:
that’s a template and you can use your calculation utility with every type extends Number.
or a double version;
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.