I don't dare to say that it is reproduced, because it is not reproduced all of the original text, it is the plagiarism of the original text.
Problem domain assumptions I implements a User Management System (UserManager), including:
User Identity Management (A IdentityManager) User Access Management (A AccessManager) variable user information (a UserInformationManager)
Note that my user management system does not want to rely on a particular storage mechanism and user information source.
achieve
If I use Pico's constructor oc, then my userManager will be like this.
Public class usermanager {
Private IdentityManager IdentityManager;
Private Accessmanager Accessmanager;
Private UserInformationManager UserInformationManager;
Public UserManager (IdentityManager IdentityManager,
Accessmanager Accessmanager,
UserInformationManager UserInformationManager) {
THIS.IDENTITYMANAGER = IdentityManager;
THIS.ACCESSMANAGER = AccessManager;
THIS.USERINFORMATIONMANAGER = USERINFORMATIONMANAGER;
}
............
}
- If I use Spring's bean oc, then my userManager will look like this:
Public class usermanager {
Private IdentityManager IdentityManager;
Private Accessmanager Accessmanager;
Private UserInformationManager UserInformationManager;
Public userManager () {}
Public void setidentityManager (IdentityManager IdentityManager) {
THIS.IDENTITYMANAGER = IdentityManager;
}
Public void setAccessmanager (accessmanager accessmanager) {
THIS.ACCESSMANAGER = AccessManager;
}
Public void setUserInformationManager
UserInformationManager UserInformationManager) {
THIS.USERINFORMATIONMANAGER = USERINFORMATIONMANAGER;
}
............
}
I use this three interfaces with the statement indicated by omitting, without considering how these interfaces implement the instance of the class is created and imputed.
After the UserManager above, after a few months, or give another company. A developer now finds that he needs to verify the user identity from an LDAP server, get access information from an existing system (relational database), and save specific user information in its own application. At this time, he wrote an LDAPIDENTITYMANAGER, a RELASSMANAGER and an XMLUSERINFORMATIONMANAGER.
registered
Now his specific implementation class is as follows:
LDAPIDENTITYMANAGER ImplementationArycessManager implementation AccessManager interface XMLUSERINFORMATIONMANAGER implementation UserInformationManager interface No matter what his implementation is, no matter how to change UserManager
If I use Pico, then I can
MutablePicocontainer Pico = New DefaultPicOContainer ();
Pico.registerComponent; UserManager.class;
Pico.registerComponent (ldapidentityManager.class);
Pico.registerComponent (RELATIONACCESSMANAGER.CLASS);
Pico.registerComponent (XMLUSERINFORMATIONMANAGER.CLASS);
UserManager Manager = (UserManager) Pico.getComponent (userManager.class);
If I use Spring, it is very likely to have a file, such as ApplicationContext.xml as follows:
Class = "Org.erptao.Flex.usercore.userManager"> Property> Property> Property> bean> Class = "Org.ERPTAO.HOTEL.USER.LDAPIDENTITYMANAGER"> bean> Class = "org.erptao.Hotel.user.RelationAccessmanager"> bean> Class = "Org.erptao.Hotel.user.xmluserInformationMAnager"> bean> And code that needs to be registered: InputStream IS = getClass (). GetResourceAsStream ("ApplicationContext.xml); XMLBeanFactory BF = New XMLBeanFactory (IS); UserManager Manager = (UserManager) bf.getbean ("UserManager"); Of course we can further encapsulate Public interface flexcontainer { Public Object GetComponent (Object Obj) Throws ComponentNotFoundException; } Now, different PICO or Spring processes mainly implement this interface, all other code take objects from this interface, for example Public Class SpringflexContainer Implements flexcontainer {protected applicationContext ApplicationContext; Public SpringContainerContext () { } public void setServletContext (servletContext context) { servletContext = context; SetApplicationContext WebApplicationContextUtils.GetWebApplicationContext (context)); } public object getcomponent (Object Key) throws componentNotFoundException { Try { IF (ApplicationContext == NULL) Throw new IllegalStateException "Spring Application Context Is Null"); IF (key == null) Throw New ComponentNotFoundException ("Key Cannot Be Null); IF (Key InstanceOf Class) { ...... } Return ApplicationContext.getBean (Key.toString ()); } catch (beansexception e) { Throw New ComponentNotFoundException "Cannot Get Component:" E.GetMessage (), e); } } ..... This process can not only concealed what containers we use, but also package specific interaction processes of various and other frameworks (such as web, J2EE containers), so your UserManager is completely out of the environment you need and other of the need for USERMANAGER. Assistant Manager. further Consider the above RelationAccessManager if it can use different ways, for example it wants to use AccessDao, then this DAO can use jdbaccessdao, JDoAccessDao or HibernateAccessDao, consider our UserManager, he also requires TransactionManager, etc. If you use Factory, then you don't know how much Factory, (in fact you don't know), then your code needs to create an object from this different factory, then you have to consider what Factory generated can give others What object, that is, you need to resolve dependencies, and you want to make a Pico or called XXX IOC container. So I chatted with Weihello, saying IOC container manufacturing products, business code uses products, garbage collector collects garbage, saying that IOC is good, garbage collection is also good, AOP is also good, EJB container is also, the purpose is only one : Responsible separation, let programmers pay more attention to core business. Other benefits test Private IdentityManager IdentityManager = New LDAPIDENTITYMANAGER (); Private Accessmanager Accessmanager = new rateAccessmanager (); Private userinformationManager UserInformationManager = New XMLUSERINFORMATIONMANAGER (); UserManager UserManager = New UserManager (IdentityManager, UserInformationManager); ... Do not rely on containers, do not need a specific operating environment Indirect benefits Due to the management of the container, it can Cache instance Add Proxy to implement interceptor and many more