Java learning 5 - looking at the factory method pattern from Spring source

Java learning 5 - looking at the factory pattern from the Spring source code

 

 

What is the factory method mode

Factory method pattern is to define an interface to create an object, but the subclass decides which class to instantiate. Factory methods let classes defer instantiation to subclasses.

 

Learning factory method pattern from Spring source

Spring source code covers a variety of software design patterns, of which factory pattern is the most used.

The "interface for creating objects" mentioned above that needs to be explained here is a generalized interface, which can be interface or abstract class.

It's easy to see the source code. There are many factory classes. Here I start with the implementation abstract class AbstractApplicationContext of ApplicationContext interface.

AbstractApplicationContext is an abstract class. Look for its abstract method,

public abstract class AbstractApplicationContext extends DefaultResourceLoader
		implements ConfigurableApplicationContext {

            ....................................


            @Override
	public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;


            ....................................


}

It has two direct subclasses, AbstractRefreshableApplicationContext and GenericApplicationContext, which are also abstract classes, but both implement this abstract method,

//The implementation of GenericApplicationContext
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
		return this.beanFactory;
}


//Implementation of AbstractRefreshableApplicationContext class
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
		synchronized (this.beanFactoryMonitor) {
			if (this.beanFactory == null) {
				throw new IllegalStateException("BeanFactory not initialized or already closed - " +
						"call 'refresh' before accessing beans via the ApplicationContext");
			}
			return this.beanFactory;
		}
}

Obviously, the AbstractApplicationContext class defines an abstract method to create an object, and then instantiates it by its subclass, which is very consistent with the definition of the factory method pattern.

Look at the source code again.

//Methods in AbstractRefreshableApplicationContext class
//The instance is created here. The DefaultListableBeanFactory class is a subclass of ConfigurableListableBeanFactory
protected DefaultListableBeanFactory createBeanFactory() {
		return new DefaultListableBeanFactory(getInternalParentBeanFactory());
}


//GenericApplicationContext is initialized by the constructor
public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
}

Of course, I just list the instantiation location here for the moment, but the whole process is a bit winding. In the future, I will do a detailed process about the Spring IOC container initializing objects through XML or comments.

I think it's boring to just look at the design mode. It may work better to combine books and source code. Fortunately, Spring is a better framework, in which you can learn a lot. Looking back, we can say that according to the definition of the factory method pattern, it is 100% in line with its definition. In this case, the design of the class is loosely coupled with the real implementation. In another case, you only need to write a class or a group of classes to implement it, and you do not need to modify the original code, which is also in line with the opening and closing principle of the design, that is, to close the modification, Open to expansion.

 

Published 100 original articles, won praise 22, visited 310000+
Private letter follow

Tags: Spring Java xml

Posted on Sat, 01 Feb 2020 04:41:22 -0500 by linoukus