模板方法设计模式的使用

1.什么是模板设计模式

定义:有一个抽象父类定义了模板方法,子类继承父类的方法,按照各自的需求来实现父类的方法。

2.类图如下:

image

3.代码示例:

package com.buka.designer.template;

/** 编程是门艺术,设计源于生活!

*/

public abstract class Ticket {

    //模板方法

    public void buyTicket() {

        login();

        selectTicket();

        pay();

    }

    public final void login() {

        System.out.println("登录。。。");

    }

    //子类必须实现

    public abstract void selectTicket();

    //子类可以不实现

    public void pay() {

        System.out.println("支付宝。。。");

    }

}

package com.buka.designer.template;

/**

* 火车票

*/

public class TrainTicket extends Ticket{

    @Override

    public void selectTicket() {

        System.out.println("选择火车票。。。");

    }

    @Override

    public void pay() {

        System.out.println("使用微信支付。。。");

    }

}

package com.buka.designer.template;

/**

* 船票

*/

public class ShipTicket extends Ticket{

    @Override

    public void selectTicket() {

        System.out.println("选择船票。。。");

    }

}

package com.buka.designer.template;

/**

* 测试

*/

public class TemplateTest {

    public static void main(String[] args) {

        Ticket trainTicket = new TrainTicket();

        trainTicket.buyTicket();

        Ticket shipTicket = new ShipTicket();

        shipTicket.buyTicket();

    }

}

4.使用场景:

spring源码中使用模板方法设计模式,例如:AbstractXmlApplicationContext作为父类,其子类有ClassPathXmlApplicationContext,FileSystemXmlApplicationContext等等。

FileSystemXmlApplicationContext集成getResourceByPath方法:

/* Resolve resource paths as file system paths.

* 

Note: Even if a given path starts with a slash, it will get * interpreted as relative to the current VM working directory. * This is consistent with the semantics in a Servlet container. * @param path path to the resource * @return the Resource handle * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath */ @Override protected Resource getResourceByPath(String path) { if (path.startsWith("/")) { path = path.substring(1); } return new FileSystemResource(path); } }

ClassPathXmlApplicationContext类继承了AbstractXmlApplicationContext的getConfigResources方法

@Override

@Nullable

protected Resource[] getConfigResources() {

return this.configResources;

}

XmlWebApplicationContext类继承了父类的loadBeanDefinitions,getDefaultConfigLocation两个方法。

/**

* Loads the bean definitions via an XmlBeanDefinitionReader.

* @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader

* @see #initBeanDefinitionReader

* @see #loadBeanDefinitions

*/

@Override

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {

// Create a new XmlBeanDefinitionReader for the given BeanFactory.

XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

// Configure the bean definition reader with this context's

// resource loading environment.

beanDefinitionReader.setEnvironment(getEnvironment());

beanDefinitionReader.setResourceLoader(this);

beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

// Allow a subclass to provide custom initialization of the reader,

// then proceed with actually loading the bean definitions.

initBeanDefinitionReader(beanDefinitionReader);

loadBeanDefinitions(beanDefinitionReader);

}

/**

* The default location for the root context is "/WEB-INF/applicationContext.xml",

* and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet"

* (like for a DispatcherServlet instance with the servlet-name "test").

*/

@Override

protected String[] getDefaultConfigLocations() {

if (getNamespace() != null) {

return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};

}

else {

return new String[] {DEFAULT_CONFIG_LOCATION};

}

}

你可能感兴趣的:(模板方法设计模式的使用)