-1-20 by Atlas
设计思想
应用场景核心工厂类不再负责所有产品的创建,而是将具体创建的工作交给子类去做,成为一个抽象工厂角色,仅负责给出具体工厂类必须实现的接口,而不接触哪一个产品类应当被实例化这种细节。
UML 类图Template Method Pattern是在父类建立处理逻辑的大纲骨架,而在子类补充具体的处理内容。把Template Method Pattern应用在生成对象实例方面,以Template Method Pattern架构获取产生对象实例的工厂,就是Factory Method Pattern。
Product(产品)参与者标准示例框架的部分。这个抽象类规定此Pattern所产生的对象实例应有的接口,具体内容则由子类的ConcreteProduct参与者规定。Creator(生产者)参与者
框架的部分。这是产生Product参与者的抽象类。具体的内容由子类的ConcreteCreator参与者决定。
Creator参与者对于实际产生的ConcreteProduct参与者完全一无所知。Creator参与者唯一直到的是只要调用Product参与者和产生对象的方法,就能产生Product。ConcreteProduct(具体产品)参与者
实际处理内容的部分。规定具体的产品样式。ConcreteCreator(具体生产者)参与者
实际处理内容的部分。规定制造实际产品的类。
public abstract class Product {public abstract void use();}public abstract class Factory {public final Product create(String owner) {Product p = createProduct(owner);registerProduct(p);return p;}protected abstract Product createProduct(String owner);protected abstract void registerProduct(Product product);}public class IDCard extends Product {private String owner;public IDCard(String owner){System.out.println("创建" + owner + "的卡。");this.owner = owner;}public void use() {System.out.println("使用" + owner + "的卡。");}public String getOwner() {return owner;}}public class IDCardFactory extends Factory {private Vector<String> owners = new Vector<String>();protected Product createProduct(String owner) {return new IDCard(owner);}protected void registerProduct(Product product) {IDCard card = (IDCard) product;owners.add(card.getOwner());}public Vector<String> getOwners() {return owners;}}
案例鉴赏Product定义产品骨架。
Factory定义工厂生产过程骨架。
IDCard具体实现产品的内容。
IDCardFactory具体实现工厂生产IDCard对象实例。
public class ContextLoader {protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {// ...wac.refresh();// ...}}public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {// ...void refresh() throws BeansException, IllegalStateException;ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;// ...}public abstract class AbstractApplicationContext extends DefaultResourceLoaderimplements ConfigurableApplicationContext, DisposableBean {// ...public void refresh() throws BeansException, IllegalStateException {// ...ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();}protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {refreshBeanFactory();ConfigurableListableBeanFactory beanFactory = getBeanFactory();// ...return beanFactory;}public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;// ...}public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {// ...protected final void refreshBeanFactory() throws BeansException {// ...DefaultListableBeanFactory beanFactory = createBeanFactory();// ...}protected DefaultListableBeanFactory createBeanFactory() {return new DefaultListableBeanFactory(getInternalParentBeanFactory());}public final ConfigurableListableBeanFactory getBeanFactory() {// ...return this.beanFactory;}// ...}
spring框架初始化ApplicationContext创建BeanFactory过程删减后的骨架。