1. 程式人生 > >設計模式-工廠方法(Factory Method)

設計模式-工廠方法(Factory Method)

log face inf str ace 對象實例 method tee 就是

2018-1-20 by Atlas


  • 應用場景

Template Method Pattern是在父類建立處理邏輯的大綱骨架,而在子類補充具體的處理內容。把Template Method Pattern應用在生成對象實例方面,以Template Method Pattern架構獲取產生對象實例的工廠,就是Factory Method Pattern。

  • UML 類圖

技術分享圖片

  • 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 DefaultResourceLoader
        implements 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過程刪減後的骨架。

設計模式-工廠方法(Factory Method)