zoukankan      html  css  js  c++  java
  • 模板方法模式(Template Pattern)行为型(Behavior)很常用的一个设计模式

    之前看Spring源码,里面的AbstractXmlApplicationContext,就是使用的模板方法模式,所以在这里,打算,写个简单的demo出来玩玩

    定义:在抽象类里面公开定义它的抽象模板,在子类去实现它的抽象方法,然后业务按照模板去执行。

    代码实现:

    public class TemplateMethodPraticeDay6 {
    
        /**
         * 抽象类,在里面公开定义业务模板。
         */
        public static abstract class AbstractXmlFactory{
    
            public int loadBeanDefinitions(){
                String resources = getConfigResources();
                System.out.println("加载resources文件:"+resources);
                return 0;
            }
    
            public AbstractXmlFactory() {
            }
    
            public AbstractXmlFactory(AbstractXmlFactory abstractXmlFactory) {
                this();
                System.out.println("set parent的factory");
            }
    
            /**
             * 给子类去实现
             * @return
             */
            protected abstract String getConfigResources();
        }
    
        /**
         * 实现类,在里面实现getConfigResources()方法
         */
        public static class ClassPathXmlApplicationContext extends AbstractXmlFactory{
    
            private String[] resources;
    
            public ClassPathXmlApplicationContext() {
            }
    
            public ClassPathXmlApplicationContext(String[] resources) {
                this(resources,true,null);
            }
    
            public ClassPathXmlApplicationContext(String[] resources, boolean refresh, @Nullable ClassPathXmlApplicationContext context) {
                super(context);
                this.resources = resources;
                if (refresh){
                    System.out.println("销毁已存在的BeanFactory,创建新的BeanFactory");
                }
            }
    
            @Override
            protected String getConfigResources() {
                return Arrays.toString(resources);
            }
        }
    
        public static void main(String[] args) {
            AbstractXmlFactory context = new ClassPathXmlApplicationContext(new String[]{"beanDefinition.xml"});
            context.loadBeanDefinitions();
        }
    }
  • 相关阅读:
    C# Win7系统下为应用程序取得管理员权限
    bootstrap 列函数
    bootstrap table offset 参数问题
    java 调用webservcie ,自己亲测可用
    sqlserver 生成数据字典
    css 色彩大全网址
    C#遍历指定文件夹中的所有文件和子文件夹
    ajax 跨域调用webservice 使用jsonp解决方法
    XML 类型数据转化为表
    SQL SERVER 查询未提交的事务
  • 原文地址:https://www.cnblogs.com/fuckingPangzi/p/15740260.html
Copyright © 2011-2022 走看看