- 论坛徽章:
- 0
|
最近做的论坛中,使用一个Servlet做为流程控制器,根据一个自己设定的ActionForward参数的值选用不同的Service处理方法。但是随着ActionForward的数量地增加,在Servlet不得不用大量的条件判断语句来选择合适的流程。仔细考虑后,打算使用策略模式对程序进行改造。
策略模式又称政策,它把算法包装起来,把使用算法的责任和算法本身分开。在下列情况下使用该模式:
- 许多相关的类仅仅是行为不同,策略模式可以动态地让一个对象在许多行为中选择一种。
- 系统需要动态选择几种算法中的一种。
- 算法使用客户不应该知道的数据。此时可以使用策略模式以避免暴露复杂的、与算法相关的数据结构。
- 一个类定义了多种行为,如果不恰当使用模式,这些行为只好使用多重条件语句实现。
此时如果使用策略模式,就可以避免难以维护的多重条件选择语句。
基于以上原理,重新设计后程序中的角色有:
- 环境角色(Context):LuntanActionForward即全局的流程控制器,持有LuntanServiceExecute(Strategy)类的引用;
- 抽象策略(Strategy):LuntanServiceExecute为抽象类,是业务处理的桥接;
- 实现策略(ConcreteStrategy):实现了抽象策略的相关行为。本例中为NewTopicService、TopicListService等。
程序片断如下:
// LuntanActionForward.java
public class LuntanActionForward extends HttpServlet {
/** 服务类名 */
Hashtable htClassName = null;
/**
* Initialize global variables
*
* @author zw_ren
*/
public void init(ServletConfig config) throws ServletException {
try {
super.init(config);
// 获得服务类Hashtable
htClassName = ServiceClassNameXML.getServiceClassName();
......
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Process the HTTP Post request
*
* @param HttpServletRequest
* @param HttpServletResponse
* @throws ServletException,
* IOException
* @author zw_ren
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
......
try {
......
/** 获得页面提交表单中的传递参数 */
Hashtable hashtable = getParameter(request, strLanguage);
htmlHashtable = this.luntanService(hashtable);
.......
} catch (Exception ex) {
/** 错误处理输出页面的文件名 */
htmlHashtable.put("OUTPUT_HTMLFILE", html_error);
htmlHashtable.put("%ERROR_EXCEPTION%", ex.toString());
} finally {
try {
/** 输出WEB页面 */
output_html = output_path
+ htmlHashtable.get("OUTPUT_HTMLFILE");
/** 设置显示页面/字符集属性 */
OutHTML outHtml = new OutHTML(response, strLanguage);
outHtml.outputHtml(output_html, htmlHashtable, rsBundle);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 各个页面的业务处理
*
*
* @param Hashtable
* @return Hashtable
*/
private Hashtable luntanService(Hashtable hashtable) throws Exception {
/** 设置页面显示项Hashtable */
Hashtable htmlHashtable = new Hashtable();
try {
/** 处理参数 */
String strForward = hashtable.get("ACTION_FORWARD").toString();
/** 业务逻辑操作处理类对象实例化 */
LuntanServiceExecute srvExe = LuntanServiceExecute.instance(
htClassName.get(strForward).toString());
/** 此处使用策略模式 */
htmlHashtable = srvExe.execute(hashtable);
} catch (Exception ex) {
/** 错误处理输出页面的文件名 */
htmlHashtable.put("OUTPUT_HTMLFILE", html_error);
htmlHashtable.put("%ERROR_EXCEPTION%", ex.toString());
}
return htmlHashtable;
}
/**
* 释放
*/
public void destroy() {
/** 调用父类的方法 */
super.destroy();
}
}
// LuntanServiceExecute.java
/*
* @(#)LuntanServiceExecute.java 1.0
* Created on 2005-1-27
*
* Copyright 2005 LCW. All rights reserved.
*/
package com.fu.luntan;
import java.util.*;
/**
* Title: 第一事业部技术论坛
* Description: 业务处理的桥接
* Copyright: Copyright (c) 2005
* Company: LCW
* @author ZW_Ren
* @version 1.0
*/
public abstract class LuntanServiceExecute {
/** 执行业务 */
public abstract Hashtable execute(Hashtable hashtable) throws Exception;
/** 释放执行业务所占用的资源 */
protected abstract void destroyService() throws Exception;
/**
* 对象实例化
*/
public static LuntanServiceExecute instance(String serviceClassName) throws Exception {
/** 初始化 */
try {
//创建LuntanServiceExecute子类的一个实例
Class c = Class.forName(serviceClassName);
LuntanServiceExecute service = (LuntanServiceExecute) c.newInstance();
return service;
}
catch (Exception e) {
System.err.println(
"LuntanServiceExecute Exception loading class: " +
e.getMessage());
e.printStackTrace();
}
return null;
}
}
// TopicListService.java
package com.fu.luntan.service;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import com.fu.luntan.Constant;
import com.fu.luntan.LuntanDisplayEdition;
import com.fu.luntan.LuntanServiceExecute;
import com.fu.luntan.domain.dao.LuntanDAO;
import com.fu.luntan.domain.dao.TopicListDAO;
/**
* Title: 第一事业部技术论坛
* Description: 主题列表
* Copyright: Copyright (c) 2005
* Company: LCW
* @author ZW_Ren
* @version 1.0
*/
public class TopicListService extends LuntanServiceExecute {
private TopicListDAO topicListDao = null;
private LuntanDAO luntanDao = null;
public TopicListService() {
topicListDao = new TopicListDAO();
luntanDao = new LuntanDAO();
}
/**
* 释放对象
*/
protected void destroyService() throws Exception {
if (topicListDao != null) {
topicListDao.destroyDao();
topicListDao = null;
}
if (luntanDao != null) {
luntanDao.destroyDao();
luntanDao = null;
}
}
public Hashtable execute(Hashtable hashtable) throws Exception {
/** 设置输出页面 */
String outputHtml = Constant.LUNTAN_TOPIC_HTM;
Hashtable htmlHashtable = new Hashtable();
try {
/** 定义每页主题的行数 */
int rows = 10;
/** 斑竹列表 */
String strManager = "";
/** 总页数 */
int intPageCount = 0;
/** 当前页 */
int intPage = 1;
/** 论坛标示 */
if (hashtable.get("id") == null){
outputHtml = Constant.LUNTAN_MAINMENU_HTM;
}else{
/** 论坛标示 */
String strLuntan = hashtable.get("id").toString();
/** 论坛斑竹列表 */
List retList = luntanDao.getManagerList(strLuntan);
if (retList.size() == 0){
outputHtml = Constant.LUNTAN_MAINMENU_HTM;
}else{
for (int i = 3; i " + retList.get(i)
+ " | ";
}
/** 返回论坛标示 */
htmlHashtable.put("%DATA_LUNTANFLAG%", strLuntan);
/** 返回论坛名称 */
htmlHashtable.put("%DATA_LUNTANNAME%", retList.get(1));
/** 返回论坛介绍 */
htmlHashtable.put("%DATA_LUNTANSHOW%", retList.get(2));
/** 返回斑竹列表 */
htmlHashtable.put("%DATA_MANAGERLIST%", strManager);
/** 计算总页数 */
intPageCount = Integer.parseInt(retList.get(0).toString());
if (intPageCount % rows == 0) {
intPageCount = intPageCount / rows;
} else {
intPageCount = intPageCount / rows + 1;
}
/** 主题页码 */
if (hashtable.get("page") != null){
/** 当前页 */
try{
intPage = Integer.parseInt(hashtable.get("page").toString());
}catch(Exception e){
intPage = 1;
}
}
/** 显示页码是否有效 */
if (intPage == 0) {
intPage = 1;
}else if(intPage > intPageCount) {
intPage = intPageCount;
}
/** 当前页码主题列表 */
int intMinRow = (intPage - 1) * rows;
int intMaxRow = intPage * rows;
/** 主题列表分页一览 */
Vector retVector = topicListDao.getTopicListInfo(strLuntan, intMinRow,
intMaxRow);
/** 取得一览信息并排版 */
String strTopicList = LuntanDisplayEdition.topicTitleList(retVector);
/** 返回主题列表 */
htmlHashtable.put("%DATA_TOPICLIST%", strTopicList);
/** 返回主题总数 */
htmlHashtable.put("%DATA_TOPICLISTCOUNT%", retList.get(0));
/** 返回本页主题 */
htmlHashtable.put("%DATA_PAGELISTCOUNT%", String.valueOf(retVector
.size()));
/** 返回当前页 */
htmlHashtable.put("%DATA_CURRENTPAGE%", intPage + "/"
+ intPageCount);
/** 主题分页列表一览 */
String strPageList = LuntanDisplayEdition.topicPageList(
intPageCount, intPage);
/** 返回分页信息 */
htmlHashtable.put("%DATA_TOPICPAGELIST%", strPageList);
}
}
} finally {
this.destroyService();
htmlHashtable.put("OUTPUT_HTMLFILE", outputHtml);
}
return htmlHashtable;
}
}
其它还有几个类似的Service执行的类,跟TopicListService类似。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10926/showart_58282.html |
|