錦囊妙計之策略方法模式的應用

1、策略模式完善模板方法模式處理DAO中的查詢方法

 

     建議先看模模板方法模式在項目中的應用一節,再與本節對比.

 

    1.1 新建RowMapper接口,定義mapRow(ResultSet rs)方法.

   

public interface RowMapper {
	public Object mapRow(ResultSet rs) throws SQLException;
}

    1.2把抽象類AbstractDao改成JdbcDaoTemplate類.

   

public class JdbcDaoTemplate {
  //RowMapper是一個接口類,調用此方法時需傳入對應的實現類
	public Object find(String sql, Object[] args, RowMapper rowMapper) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++)
				ps.setObject(i + 1, args[i]);
			rs = ps.executeQuery();
			Object obj = null;
			if (rs.next()) {
				obj = rowMapper.mapRow(rs);
			}
			return obj;
		} catch (SQLException e) {
			throw new DaoException(e.getMessage(), e);
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}
}

   1.3 將UserDaoImpl類改成如下 :

  

public class UserDaoImpl {
	JdbcDaoTemplate template = new JdbcDaoTemplate();

	public User findUser(String loginName, String password) {
		String sql = "select id, name, money, birthday  from user where name=?";
		Object[] args = new Object[] { loginName };
		RowMapper mapper = new UserRowMapper();
		Object user = this.template.find(sql, args, mapper);
		return (User) user;
	}
  //根據用戶id查找對應的用戶名
	public String findUserName(int id) {
		String sql = "select name from user where id=?";
		Object[] args = new Object[] { id };
		//通過匿名內部類實現RowMapper接口 策略二:返回的是一個子符串
		Object name = this.template.find(sql, args, new RowMapper() {

			public Object mapRow(ResultSet rs) throws SQLException {
				return rs.getString("name");
			}
		});
		return (String) name;
	}
}
//內部類實現的RowMapper接口  策略一:返回的是一個User實體對象
class UserRowMapper implements RowMapper {
	public Object mapRow(ResultSet rs) throws SQLException {
		User user = new User();
		user.setId(rs.getInt("id"));
		user.setName(rs.getString("name"));
		user.setMoney(rs.getFloat("money"));
		user.setBirthday(rs.getDate("birthday"));
		return user;
	}

}

    RowMapper 接口對應的角色是抽象策略,此角色定義了策略類所需的接口.

    UserRowMapper 對應的是具體策略角色,該角色給出對抽象策略的具體實現.

    JdbcDaoTemplate 對應的是環境角色,持有一個抽象策略接口的引用.

    UserDaoImpl 

是使者該策略的角色.

 

2、策略模式結構圖:

 

 代碼表現如下:

 

 2.1 定義接口

 

/**
 * 抽象策略(角色):此角色給出所有的具體策略類所需的接口
 * @author Administrator
 *
 */
public interface Strategy {
	
	//抽象策略(算法)
	public  String strategyInterface();
}

 

  2.2 編寫環境角色

  

/**
 * 環境(角色):持有一個Strategy類的引用
 * @author Administrator
 */
public class Context {
	private Strategy strategy;
	//通過將子類對象引用賦值給超類對象引用變量來實現動態方法調用
	public Context(Strategy strategy){
		//初始化時,傳入具體的策略對象
		this.strategy = strategy;
	}

	public String getResult(){
		return strategy.strategyInterface();
	}
}

  2.3編寫具體策略角色A

 

/**
 * 具體策略角色A
 * @author Administrator
 *
 */
public class ConcreteStrategyA implements Strategy {

	public String strategyInterface() {
		return "算法A的實現";
	}
}

   2.4編寫具體策略角色B

  

/**
 * 具體策略角色B
 * @author Administrator
 *
 */
public class ConcreteStrategyB implements Strategy {

	public String strategyInterface() {
		return "算法B的實現";
	}

}

   2.5編寫使用者:測試類

  

public class StrateryTest {
	public static void main(String[] args) {
		Context context = new Context(StrategyFactory.getStrategyA());
		System.out.println(context.getResult());
		context = new Context(StrategyFactory.getStrategyB());
		System.out.println(context.getResult());
		context = new Context(StrategyFactory.getStrategyC());
		System.out.println(context.getResult());
	}
}

    打印結果:

  

算法A的實現
算法B的實現
算法C的實現

3、策略方法模式的優缺點:

     優缺: 當業務邏輯存在多個判斷語句時,使用該模式可以轉移if語句.

     缺點: 當if語句很多的時候,就會造成編寫很多的具體策略類.

 


 

 

 

發佈了15 篇原創文章 · 獲贊 3 · 訪問量 2100
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章