• Spring-java-模板设计模式



     1,模板设计模式指的是将相应的模板方法提取出来在专门的位置定义,然后把相同调用过程操作,通过模板来实现
    对于模板设计模式而言,一般有两种实现方式

    1)基于继承的实现

    2)基于组合的实现

     Spring的JdbcTemplate就是通过基于组合实现的模板设计模式实现的

    2,基于继承的实现

    package com.yangw.spring.template;
    
    /**
     * 基于继承实现模板设计模式
     * @author Administrator
     */
    public abstract class MyJdbcTemplate1 {
    
        private void beginConnection(){
            System.out.println("begin ...");
        }
        private void closeConnection(){
            System.out.println("end ...");
        }
        //抽象方法
        protected abstract void run();
        //在模板中有一种方法叫钩子方法,作用是让实现类可以通过一些方法来控制模板的流程
        protected abstract boolean isLog();
        public void execute(){
            beginConnection();
            if(isLog()){
                System.out.println("add logger");
            }
            run();
            closeConnection();
        }
    }
    package com.yangw.spring.dao;
    
    import com.yangw.spring.template.MyJdbcTemplate1;
    
    public class RoleDao extends MyJdbcTemplate1 {
    
        @Override
        protected void run() {
            System.out.println("role add ");
    
        }
    
        /**
         * 实现类实现具体的钩子方法,控制模板流程
         */
        @Override
        protected boolean isLog() {
            
            return true;
        }
    
    }
    package com.yangw.spring.test;
    
    import org.junit.Test;import com.yangw.spring.dao.RoleDao;
    import com.yangw.spring.template.MyJdbcTemplate1;
    
    public class TestTemplate {
    
        @Test
        public void test01(){
            MyJdbcTemplate1 mt=new RoleDao();
            
            mt.execute();
            
        }
    }

    3, 基于组合的实现

    package com.yangw.spring.template;
    
    
    /**
     * 基于组合的模板设计模式
     * @author Administrator
     *
     */
    public class MyJdbcTemplate2 {
    
        private void beginConnection(){
            System.out.println("begin ...");
        }
        private void closeConnection(){
            System.out.println("end ...");
        }
        /**
         * 调用方法,传入一个钩子函数接口
         */
        public void execute (MyCallback call){
             beginConnection();
             //中间通过钩子勾起某一个函数
             call.doInTemplate();
             closeConnection();
        }
        
        /**
         * 将所有要实现的方法,全部放到模板中
         */
        public void add(final int id){
            execute(new MyCallback() {
                
                @Override
                public void doInTemplate() {
                    
                    System.out.println("add:"+id);
                }
            });
        }
        public void delete(final int id){
            execute(new MyCallback() {
                
                @Override
                public void doInTemplate() {
                    
                    System.out.println("delete:"+id);
                }
            });
        }
        
        
    }
    package com.yangw.spring.dao;
    
    import com.yangw.spring.template.MyJdbcTemplate2;
    
    public class MessageDao {
    
        private MyJdbcTemplate2 mt=new MyJdbcTemplate2();
        
        public  void addMsg(int id){
            mt.add(id);
        }
    }
    package com.yangw.spring.test;
    
    import org.junit.Test;
    
    import com.yangw.spring.dao.MessageDao;public class TestTemplate {
    
        @Test
        public void test02(){
            MessageDao msgDao=new MessageDao();
            
            msgDao.addMsg(1);
            
        }
    }
    ----------- 赠人玫瑰,手有余香     如果本文对您有所帮助,动动手指扫一扫哟   么么哒 -----------


    未经作者 https://www.cnblogs.com/xin1006/ 梦相随1006 同意,不得擅自转载本文,否则后果自负
  • 相关阅读:
    如何保证消息的可靠性传输?或者说,如何处理消息丢失的问题?
    如何保证消息不被重复消费?或者说,如何保证消息消费的幂等性?
    redis 的过期策略都有哪些?内存淘汰机制都有哪些?手写一下 LRU 代码实现?
    SpringBoot项目jar包启动脚本-windows环境
    在IDEA中将SpringBoot项目打包成jar包的方法
    Freemarker在replace替换是对NULL值的处理
    移动端页面内容设定字号相同,但是显示出来字体大小不同的问题
    解决UEditor编辑时,只添加视频内容,不添加文字,视频信息不能保存到数据库的问题
    springboot项目打war包发布到外置tomcat
    Ueditor富文本添加视频内容,视频不显示以及编辑富文本时,视频不显示解决方案
  • 原文地址:https://www.cnblogs.com/xin1006/p/3383628.html
Copyright © 2020-2023  润新知