JiveJdon Community Forums
在线119人 J道首页 | 论坛首页 | 培训咨询 | 开源框架 | 精华 | 查搜 | 注册 | 登陆 |
首页 » 论坛 » 项目工程开发经验谈
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表
???en_US.forumThreadNext.name??? 下一主题
这个主题共有 22 回复 / 2 页 [ 1 2 下一页 ]  发表新帖子  回复该主题贴
nova

发表文章: 10
注册时间: 2003年11月18日 08:12
一个小的WEB项目中的实现方法讨论 发表: 2004年10月14日 10:51 回复
最近对一个别人的WEB项目进行维护,看到这样的实现方法:
1.只有一个Controller的servlet 类
2.一个Service接口
3.一些实现Service接口的类

Controller类负责进行控制,动态产生业务逻辑的类的实例(所有的类需要实现Service接口),然后通过
httpservletrequest.setAttribute("USERLIST", userList);向WEB端赋值,

具体的可以参考部分代码:
Controller 类(extends HttpServlet )

protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//service name,example for packagename.ServiceName
String serviceName = request.getParameter(Constant.SERVICE);
if (serviceName == null)
throw new ServletException(
"There isn't service parameter![?service=]");
String targetName = request.getParameter(Constant.TARGET);
//the targeted file name,example for /fileName.jsp
if (targetName == null)
throw new ServletException(
"There isn't target parameter![?target=]");
ServletContext servletcontext = getServletContext();
try {
//TODO:hashmap to reduce the generated instance?
Class serviceClass = Class.forName(serviceName);
Service service = (Service) serviceClass.newInstance();
service.execute(request, response, servletcontext);
} catch (ClassNotFoundException classnotfoundexception) {
throw new ServletException(classnotfoundexception.getMessage());
} catch (IllegalAccessException illegalaccessexception) {
throw new ServletException(illegalaccessexception.getMessage());
} catch (Exception exception) {
throw new ServletException(exception.getMessage());
}
forward(request, response, targetName);
}


Service 接口


public interface Service {
public abstract void execute(
HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse,
ServletContext servletcontext)
throws Exception;
}


一个实现service的类(相当于业务类)


public class StartService implements Service {

public void execute(
HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse,
ServletContext servletcontext)
throws Exception {
//test data
List userList = new ArrayList();
httpservletrequest.setAttribute(
"USERNAME", "TestUser");
httpservletrequest.setAttribute(
"USERLIST", userList);
}

}



JSP 页面文件


<%
String userName=(String)request.getAttribute(
"USERNAME");
List userList=(List)request.getAttribute(
"USERLIST");
%>



访问的时候:
/service.Controller?service=StartService&target=/StartPage.jsp


我现在想知道的
1.这种实现方案怎么样?为什么这么做,有什么好处
2.产生的service 类对象有没有必要用hashmap保存,以避免产生更多的对象

//TODO:hashmap to reduce the generated instance?
Class serviceClass = Class.forName(serviceName);
Service service = (Service) serviceClass.newInstance();
service.execute(request, response, servletcontext);

3.大家有没有好的类似的方案(只是针对小型的WEB项目,利用Framework的就不要说了)

谢谢!
banq

发表文章: 8929
注册时间: 2002年08月03日 17:08
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年10月19日 09:51 回复
这其实是一个Command模式实现,Service是Command的接口,这个小系统具备了MVC模式基础,我认为是不错的代码。

关于你的两个问题:
>1.这种实现方案怎么样?为什么这么做,有什么好处
这样做主要是使用Command模式的好处,一般在服务器这边接受外界处理命令都是使用Command模式,这是一个习惯了,主要优点有:
1. 条理清楚,扩展容易,增加一个新Service只要继承Service就可以。
2. 性能好,由于服务器端直接将请求提交给具体Service处理,没有其他中间延迟过程。


>2.产生的service 类对象有没有必要用hashmap保存,以避免产生更多的对象

这主要也是提升性能,因为newInstance生成实例比new要慢,所以第一次生成后,保存起来,第二次就没有创建的开销了,当然推荐这里使用对象池Pool来取代hashmap。

对象池使用有三种选择:
1. 使用Apache的Pool组件直接使用。
2. 透过SpringAOP拦截器间接使用Apache的Pool.
3. 将Service变成EJB的无态Bean。



xuesenlin

发表文章: 30
注册时间: 2003年09月15日 16:53
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年11月08日 14:44 回复
不错的帖子,,关注中.....
anonymous

发表文章: 0
注册时间:
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年11月13日 19:24 回复
不错,是不错,还差一点,对元数据,美好好利用,这样都是从前段纵向控制,没有利用元数据横向控制。你这个action,和我的很类似,不过没有达到AOP,不过也快了。差一点了。呵呵。
anonymous

发表文章: 0
注册时间:
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年11月13日 19:26 回复
另外,若是改用Struts等结合jbuiderX等,就可以更"工业化些了"。
wzw9258

发表文章: 19
注册时间: 2004年11月04日 14:11
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月01日 13:02 回复
本人不才,是否是得产生很多service实现类
yulchina

发表文章: 2
注册时间: 2004年11月26日 14:05
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月02日 15:30 回复
这里好象没有收藏的功能哦~
这篇文章真的不错!
brokendoor

发表文章: 6
注册时间: 2002年09月20日 11:31
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月09日 14:37 回复
> 最近对一个别人的WEB项目进行维护,看到这样的实现方法:
> 1.只有一个Controller的servlet 类
> 2.一个Service接口
> 3.一些实现Service接口的类

大部分的MVC框架都是这样实现的,我个人认为即便是小型项目,利用一下成熟框架应该也是首选。
个人推荐: Turbine 或者 WebWork。
我自己做了一个jWorks的扩展,用于兼容Turbine和WebWork框架,有兴趣的朋友可以看看,提点宝贵意见。

http://www.softme.org/jwiki/v_1.2.1/jwiki.jsp?topic=jWorks
边缘人

发表文章: 2
注册时间: 2004年12月11日 17:30
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月11日 17:33 回复
找到了
边缘人

发表文章: 2
注册时间: 2004年12月11日 17:30
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月11日 17:33 回复
找到了
yangkun24

发表文章: 20
注册时间: 2003年08月24日 21:56
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月13日 00:53 回复
我门公司用asp做项目时候,也是使用类似的方式。

首先,一个Framework.IBusiness接口中有一个IBusiness_Run方法,

然后,每个叶面对应一个“业务类”,所有的“业务类”都是继承Framework.IBusiness接口,并仅仅实现IBusiness_Run方法,当IBusiness_Run方法运行完成后,叶面的所有信息就呈现出来了。
在这些“业务类”中,request/response等交互对象都是从IBusiness_Run方法的参数中创建的。。。

不知道大家有何感想?
YuLimin

发表文章: 25
注册时间: 2007年01月21日 12:47
Re: 一个小的WEB项目中的实现方法讨论 发表: 2004年12月29日 13:47 回复
YAWL的工作流引擎也有点儿类似这种的做法。
siberian

发表文章: 24
注册时间: 2003年07月14日 16:24
Re: 一个小的WEB项目中的实现方法讨论 发表: 2005年01月11日 09:32 回复
个人认为既然是自己的项目,就不用着什么命令模式了。按照自己的流程来做一个贴切的,要省力的多。其它程序开发也容易得多。
从最简单的做起。
siberian

发表文章: 24
注册时间: 2003年07月14日 16:24
Re: 一个小的WEB项目中的实现方法讨论 发表: 2005年01月11日 09:35 回复
赋值程序做值对象来完成,页面只是得到。这样向c/s上转化比较容易。如果没有这方面的要求。这个还是原来的更直接简洁。
沧海・旭日

发表文章: 3
注册时间: 2005年02月15日 17:43
Re: 一个小的WEB项目中的实现方法讨论 发表: 2005年02月15日 17:46 回复
大家都说的好哈
这个主题有 22 回复 / 2 页 [ 1 2 下一页 ]
???en_US.forumThreadPrev.name??? 上一主题
Go back to the topic listing   返回主题列表    返回页首  返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Spring Struts
查询本论坛内 回复超过的热门帖子
快速发表回复
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-07 jdon.com

anti spam