`

读《研磨设计模式》-代码笔记-模板方法模式

阅读更多
声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 模板方法模式:
 * 采用继承的方式(Template类是一个抽象类)实现这一点:将逻辑(算法)框架放在抽象基类中,并定义好细节的接口,子类中实现细节
 * 与工厂方法模式非常相似。只不过模板方法模式关注算法,而工厂方法关注对象的创建(factoryMethod()里面是返回一个产品对象)
 * 与策略模式相比,策略模式的策略是平行、平等的,而且一个策略是一个完整的算法,相互之间可切换;
 * 但模板方法模式的算法框架是固定的,只是算法的部分细节不同
 * 
 * 以下代码考虑这样一个需求:
 * 验证用户登录,分为普通用户和特殊用户,特殊用户的密码是加密的
 */

//============1.=================
//如果需要更多验证(例如页面上的验证码),简单起见,可分别extends LoginModel和LoginTemplate
class LoginModel {
	
	private String userID;
	private String password;

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}


abstract class LoginTemplate {
	
	public abstract LoginModel findLoginModel(String userID);

	//普通用户登陆(子类)时,密码不加密,直接返回密码就OK。特殊用户则覆盖此方法加密密码
	public String encryptPassword(String password) {
		return password;
	}
	
	public final boolean loginVerify(LoginModel lm) {
		boolean pass = false;
		LoginModel dblm = this.findLoginModel(lm.getUserID());
		if (dblm != null) {
			String pwd = encryptPassword(lm.getPassword());
			lm.setPassword(pwd);
			pass = this.match(lm, dblm);
		}
		return pass;
	}
	
	public boolean match(LoginModel lm, LoginModel dblm) {
		return lm.getUserID().equals(dblm.getUserID()) 
				&& lm.getPassword().equals(dblm.getPassword());		
		
		//...可有更多验证
	}
}


class NormalLoginTemplate extends LoginTemplate {
	
	public LoginModel findLoginModel(String userID) {
	
		//模拟从数据库取数据
		LoginModel dblm = new LoginModel();
		dblm.setUserID(userID);
		dblm.setPassword("passwordInDB");
		return dblm;
	}
	
	//encryptPassword方法就不需要重写
}


class SpecialLoginTemplate extends LoginTemplate {
	
	public LoginModel findLoginModel(String userID) {
		
		//模拟从数据库取数据
		LoginModel dblm = new LoginModel();
		dblm.setUserID(userID);
		dblm.setPassword("passwordInDB");
		return dblm;
	}
	
	//重写方法。密码加密。match()方法是比较加密后的密码是否一致
	public String encryptPassword(String pwd) {
		System.out.println("encrypting password...");
		return pwd;		//实际应用中返回加密后的密码。这里简单地返回原密码
	}
	
}


//============2.=================
/*
 * 用接口回调的方法来实现
 * 
 * 这下面代码看着有些别扭:LoginCallBack里面转调Template的方法,但Template里面又转调LoginCallBack的方法
 * 但正是这样,通过LoginCallBack的不同实现,达到了模板模式“子类实现细节”的目的
 */
interface LoginCallBack {
	
	LoginModel findLoginModel(String userID);
	
	//注意传递了一个LoginTemplate2
	String encryptPassword(String pwd, LoginTemplate2 template);
	
	boolean match(LoginModel lm, LoginModel lmdb, LoginTemplate2 template);
	
}


class LoginTemplate2 {
	
	//传入一个LoginCallBack,实际调用时以内部类的形式实现接口里的方法
	public final boolean verifyLogin(LoginModel lm, LoginCallBack callback) {
		LoginModel lmdb = callback.findLoginModel(lm.getUserID());
		String password = callback.encryptPassword(lm.getPassword(), this);
		lmdb.setPassword(password);
		return callback.match(lm, lmdb, this);
	}
	
	public boolean match(LoginModel lm, LoginModel dblm) {
		return lm.getUserID().equals(dblm.getUserID())
				&& lm.getPassword().equals(dblm.getPassword());
	}
	
	//可根据实际情况看是否要重写
	public String encryptPassword(String pwd) {
		return pwd;
	}
}


//客户端。测试
public class TemplateMethodPattern {

	public static void main(String[] args) {
		
		//测试情况1
		//制造测试数据-一般用户登录
		LoginModel userA = new LoginModel();
		userA.setUserID("user");
		userA.setPassword("passwordInDB");
		
		LoginTemplate template = new NormalLoginTemplate();
		boolean pass = template.loginVerify(userA);
		
		System.out.println(userA.getUserID() + " login success?" + pass);
		
		//制造测试数据-特殊用户登录
		LoginModel userB = new LoginModel();
		userB.setUserID("admin");
		userB.setPassword("passwordInDB");
		
		LoginTemplate sTemplate = new SpecialLoginTemplate();
		pass = sTemplate.loginVerify(userB);
		System.out.println(userB.getUserID() + " login success?" + pass);
		
		
		//测试情况2-接口回调实现
		LoginTemplate2 template2 = new LoginTemplate2();
		
		boolean pass2 = template2.verifyLogin(userA, new LoginCallBack(){

			public String encryptPassword(String pwd, LoginTemplate2 template) {
				return template.encryptPassword(pwd);       //自己不需要实现,转调template里面的默认实现
			}

			public LoginModel findLoginModel(String userID) {
				//从数据库取LoginModel,简单示意一下
				LoginModel userAA =new LoginModel();
				userAA.setUserID(userID);
				userAA.setPassword("passwordInDB");
				return userAA;
			}

			public boolean match(LoginModel lm, LoginModel lmdb,
					LoginTemplate2 template) {
				return template.match(lm, lmdb);
			}
			
		});
		System.out.println(userA.getUserID() + " login success?" + pass2);
		
		pass2 = template2.verifyLogin(userB, new LoginCallBack(){

			//特殊用户登陆,重写加密密码
			public String encryptPassword(String pwd, LoginTemplate2 template) {
				System.out.println("encrypting password...");
				String encryptedPwd = pwd;		//模拟加密
				return encryptedPwd;
			}

			public LoginModel findLoginModel(String userID) {
				//从数据库取LoginModel,简单示意一下
				LoginModel userBB =new LoginModel();
				userBB.setUserID(userID);
				userBB.setPassword("passwordInDB");
				return userBB;
			}

			public boolean match(LoginModel lm, LoginModel lmdb,
					LoginTemplate2 template) {
				return template.match(lm, lmdb);
			}
			
		});
		System.out.println(userB.getUserID() + " login success?" + pass);
		
		//书上认为Collections.sort是模板模式的一种实现,我觉得这一点不好理解
		//stackoverflow上有帖子认为:All non-abstract methods of java.util.AbstractList是模板模式
		List<LoginModel> list = new ArrayList<LoginModel>();
		list.add(userA);
		list.add(userB);
		Collections.sort(list, new Comparator<LoginModel>(){
			public int compare(LoginModel arg0, LoginModel arg1) {
				return 0;
			}
		});
	}

}


/*
很早就知道jdbc用到template模式了,但之前还不是很清楚
其实就是把公用的操作(获取数据库连接,执行sql语句,为sql语句参数赋值等等)写到抽象的Template去,
具体的sql以及sql的参数由子类覆写提供

书上的例子有点复杂,我参照网上写了一个简单的示意:
这个JDBCTemplate只实现查找功能,其他操作略去(增、删、改、数据库连接的释放等)
*/
abstract class DaoTemplate {
	
	/**
	 * 根据条件查询(如果指定条件)
	 * @param sql
	 * @param args 条件参数
	 * @return
	 */
	public Object findObject(String sql, Object[] args){
		Object obj = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			
			//简单模拟获取Connection操作,更多情况下是写一个JdbcUtil
			conn = DriverManager.getConnection("url", "user", "pwd");		
			ps = conn.prepareStatement(sql);
			
			//设置查询参数(条件)
			for (int i = 0, size = args.length; i < size; i++) {
				ps.setObject(i + 1, args[i]);
			}
			
			rs = ps.executeQuery();
			
			if (rs.next()) {
				obj = rowMapper(rs);		//对结果集进行处理,返回实际的业务对象
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return obj;
	}
	
	abstract protected Object rowMapper(ResultSet rs);
}


class LoginModelDaoImpl extends DaoTemplate {

	public LoginModel findLoginModel(String userID) throws SQLException {
		String sql = "select userid,password from loginmodel where userid=?";
		Object[] args = new String[]{userID};
		LoginModel lm =(LoginModel) super.findObject(sql, args);
		return lm;
	}
	
	@Override
	protected Object rowMapper(ResultSet rs){
		LoginModel lm = new LoginModel();
		try {
			lm.setUserID(rs.getString("userid"));
			lm.setPassword(rs.getString("password"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lm;
	}
	
}

1
7
分享到:
评论

相关推荐

    研磨设计模式-配套源代码

    研磨设计模式-配套源代码研磨设计模式-配套源代码

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码 UTF-8格式

    研磨设计模式第二版-配套源代码 UTF-8格式 研磨设计模式第二版-配套源代码 UTF-8格式

    研磨设计模式-陈臣.epub

    按照上面的理解,设计模式指的就是设计方面的模板,也即设计方面的方式或方法。 设计模式:是指在软件开发中,经过验证的,用于解决在特定环境下、重复出现的、特定问题的解决方案。 1.设计模式是解决方案 根据上...

    研磨设计模式-陈臣.王斌.扫描高清版PDF

    设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;...

    研磨设计模式-陈臣pdf

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式书-配套源代码(全)

    1:本源代码是《研磨设计模式》一书的配套源代码 2:每个模式的示例源代码放在一个单独的文件夹下,以该模式的英文名称命名 3:每个模式下分成多个example,按照书的示例顺序分别命名为example1、example2.........

    研磨设计模式-陈臣.mobi kindle版

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式--chjavach的博客文章

    单例模式、工厂方法模式、策略模式、命令模式和桥接模式。

    研磨设计模式 源代码

    研磨设计模式 源代码 书上面的例子都在里面

    研磨设计模式-part2

    第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式...

    研磨设计模式源码

    研磨设计模式的配套源码,请下载,不要想了,解压后,直接放到eclipse下面即可运行!

    研磨设计模式-part4

    第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式...

    研磨设计模式-part3

    第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式...

    研磨设计模式全部源代码

    研磨设计模式全部源代码,个人收集,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!

    研磨设计模式带书签完整版228M.7z.001

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式(完整带书签).part2.pdf

    第16章 模板方法模式(Template Method) 第17章 策略模式(Strategy) 第18章 状态模式(State) 第19章 备忘录模式(Memento) 第20章 享元模式(Flyweight) 第21章 解释器模式(Interpreter) 第22章 装饰模式...

    研磨设计模式 完美书签 完整(一)

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式PDF

    研磨设计模式PDF

Global site tag (gtag.js) - Google Analytics