对于模板设计模式而言,一般有两种方式
1、基于继承的实现
2、基于组合的实现
基于继承实现模板设计:
package org.zttc.itat.template;
/**
* 基于继承实现模板设计模式
* @author Administrator
*
*/
public abstract class MyJdbcTemplateByIn {
private void beginConnection() {
System.out.println("begin connection");
}
private void closeConnection() {
System.out.println("close connection");
}
public abstract void run();
/**
* 在模板方法中有一种函数叫做钩子函数,钩子函数的作用是让实现类可以通过一些方法来控制模板中的流程
* @return
*/
public abstract boolean isLog();
public void execute() {
beginConnection();
if(isLog()) {
System.out.println("加入了日志");
}
run();
closeConnection();
}
}
package org.zttc.itat.template;
public class RoleDao extends MyJdbcTemplateByIn {
@Override
public void run() {
System.out.println("role add");
}
@Override
public boolean isLog() {
return false;
}
}
package org.zttc.itat.template;
public class MessageDao extends MyJdbcTemplateByIn {
@Override
public void run() {
System.out.println("msg add");
}
@Override
public boolean isLog() {
return true;
}
}
写测试类进行测试:
package org.zttc.itat.test;
import org.junit.Test;
import org.zttc.itat.template.MessageDao;
import org.zttc.itat.template.MyJdbcTemplateByIn;
import org.zttc.itat.template.RoleDao;
public class TestTemplate {
@Test
public void test01() {
MyJdbcTemplateByIn mt = new RoleDao();
mt.execute();
MyJdbcTemplateByIn msgt = new MessageDao();
msgt.execute();
}
}
对象mt调用execute(),RoleDao类继承MyJdbcTemplateByIn类,实际调用RoleDao类的execute(),
execute()中有钩子函数,通过钩子函数来判定是否打印“加入了日志”。
public abstract boolean isLog();
public void execute() {
beginConnection();
if(isLog()) {
System.out.println("加入了日志");
}
run();
closeConnection();
}
基于组合实现模板设计:
首先写一个接口:
package org.zttc.itat.template;
public interface MyCallback {
public void doInTemplate();
}
写模板
package org.zttc.itat.template;
public class MyJdbcTemplate {
private void beginConnection() {
System.out.println("begin connection");
}
private void closeConnection() {
System.out.println("close connection");
}
/**
* 调用方法,传入一个钩子函数的接口
*/
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);
}
});
}
}
写RoleDao
package org.zttc.itat.template;
public class RoleDao extends MyJdbcTemplateByIn {
private MyJdbcTemplate mt = new MyJdbcTemplate();
public void add(int id){
mt.add(id);
}
public void delete(int id) {
mt.delete(id);
}
}
写测试类进行测试
package org.zttc.itat.test;
import org.junit.Test;
import org.zttc.itat.template.RoleDao;
public class TestTemplate {
@Test
public void test02() {
RoleDao rd = new RoleDao();
rd.add(1);
}
}
测试类的RoleDao对象rd调用add(),RoleDao类内部使用设计模板实现add(),
而设计模板中的add()和delete()都调用execute(),execute()参数中是钩子函数的接口,重写接口方法并执行。
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);
}
});
}