模板设计模式的两种方式

对于模板设计模式而言,一般有两种方式
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);
			}
		});
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值