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 同意,不得擅自转载本文,否则后果自负
原文地址:https://www.cnblogs.com/xin1006/p/3383628.html