模板方法设计模式的使用

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.

* <p>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};

}

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在项目中经常会遇到一个类的某些方法和另一个类的某些方法功能是相同的,只有部分方法是不同的。这个时候就可以使用模板方...
    AWeiLoveAndroid阅读 3,975评论 0 1
  • 设计模式概述 在学习面向对象七大设计原则时需要注意以下几点:a) 高内聚、低耦合和单一职能的“冲突”实际上,这两者...
    彦帧阅读 9,153评论 0 14
  • spring源码分析(二) 目录五、Spring 源码解读--5.1、什么是IOC/DI--5.2、Spring ...
    毛子果阅读 3,093评论 0 0
  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 14,208评论 6 86
  • 从影片的名字,感知到剧情的冲突:神秘与巨星?不禁让人联想到万物都有一体两面性:微笑与挣扎,放弃与选择,勇敢与妥协。...
    漫步光之河阅读 1,898评论 0 1