I am reading a blog from http://www.iteye.com/problems/27338. And this paper enlightens me on the code refactoring subject and so on.
if dont use the callback, the usual code flow would be the Oject frist be created firstyl and then passed to the create method.
In that article, the writer says that this method use the Command Pattern. I am not sured about it. so I then look up the discription of Command pattern in the <Design Patterns in Java>. It writes "The ordinary way to cause a method to execute is to call it. there may be times, though, when u cant control the timing of or the context in which a method should execute. In these situations, you cam encapsulate a method inside an object. By storing the information necessary for invoking a method in an object, you can pass the method as a parameter, allowing the client or a service to determine when to invoke the method. In a word, The intent of the Command pattern is to encapsulate a request in an object."
From this reference, the CallBack is similar to the Command. Then look the orginal code in spring:
A class can provide a hook - a way to insert custom code - by invoking a supplied command at a specific point in a procedure. The command pattern is similar to the Factory Method pattern in which a client konws when an action is requried but doesnot know exactly which operation to call.
A classic example of the usefulness of Command comes with menus. A menu item know when to execute an action but dont know which method to call. Command lets u parameterize a menu with the method calls that correspond to menu label.
public static void main(String[] args) {
CreateBean bean = new CreateBean();
final String className = "com.test.AbstOO";
bean.create(className, new ObjectFactory() {
public Object getObject() {
try {
return Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
});
}
public class CreateBean {
public void create(String name, ObjectFactory factory) {
System.out.println("name=" + name);
Object obj = factory.getObject();
System.out.println("obj Name=" + obj.getClass().getName());
}
}
if dont use the callback, the usual code flow would be the Oject frist be created firstyl and then passed to the create method.
In that article, the writer says that this method use the Command Pattern. I am not sured about it. so I then look up the discription of Command pattern in the <Design Patterns in Java>. It writes "The ordinary way to cause a method to execute is to call it. there may be times, though, when u cant control the timing of or the context in which a method should execute. In these situations, you cam encapsulate a method inside an object. By storing the information necessary for invoking a method in an object, you can pass the method as a parameter, allowing the client or a service to determine when to invoke the method. In a word, The intent of the Command pattern is to encapsulate a request in an object."
From this reference, the CallBack is similar to the Command. Then look the orginal code in spring:
1.if (mergedBeanDefinition is Singleton) {
sharedInstance = getSingleton(beanName, new ObjectFactory() {
public Object getObject() throws BeansException {
try {
return createBean(beanName, mergedBeanDefinition, args);
}
catch (BeansException ex) {
destroySingleton(beanName);
throw ex;
}
}
});
bean = getObjectForBeanInstance(sharedInstance, name, mergedBeanDefinition);
}
A class can provide a hook - a way to insert custom code - by invoking a supplied command at a specific point in a procedure. The command pattern is similar to the Factory Method pattern in which a client konws when an action is requried but doesnot know exactly which operation to call.
A classic example of the usefulness of Command comes with menus. A menu item know when to execute an action but dont know which method to call. Command lets u parameterize a menu with the method calls that correspond to menu label.