转载 2008年04月25日 23:32:00
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>









    最近,该网站推出A Look Inside J2EE Patterns,系列文章,个人认为蛮不错的,而且很成体系,所以萌生了翻译、整理成中文的念头。




On the final day of J2EE Patterns, we tied everything together and applied all that we learned in the past wEEk. Some of the topics we covered were the WebWork open source framework and Antipatterns. To wrap things up, we engaged in a 'Pattern Hatching' exercise and later split up into groups to apply the patterns we learned in the final lab.


Owen started off the morning by asking the class, "What is a framework?"


A response came from the front of the room, "It's something that helps you accomplish what you want." There was an unsually long silence as we tried to grapple with this answer. "Could I have bEEn more vague?" came the humorous follow up which prompted Owen to rephrase his question.


"What comprises a GOOD framework?" Owen asked the class again.


"A good framework makes use of patterns," came the more enlightened answer.


Frameworks allow you to write code that is more focused on the problem you are trying to solve, without having to take care of all the low level plumbing. Frameworks use design patterns, encouraging you to code to known architectures such as the Model View Controller (MVC) design paradigm.




WebWork, an open source framework, provides a methodology and a framework that enables you to rapidly develop Java Web applications. It utilizes the Service to Worker ('pull' hierarchical MVC), the Front Controller, and the View Helper Web patterns, which we studied on the fourth day of class.


The Controller in WebWork is embodied by a ServletDispatcher, which bundles and routes Java requests to specific Action classes and JSPs as mandated by the configuration file. It filters the request and response behaviour according to the mapping present and the results of related operations. WebWork action classes, used as Dispatchers and ViewHelpers, delegate business logic to isolated classes such as POJOs (plain old Java objects) or SessionBeans.


Java Beans and WebWork-specific Action classes (implementing webwork.action.Action) comprise the Model. WebWork encourages the idea of code reuse, which we emphasized in our study of the Struts framework, by enabling all Action classes to be empty of any servlet API calls; this enables them to be used in a Swing application if the nEEd should ever arise.


In WebWork, Actions are created by ActionFactories, which gather their information from factory-generated ActionContext instances that are associated with their Thread of execution. Requests are wrapped in a webwork.action.ActionContext and are not web-specific, always available by calling the ActionContext.getContext() method. Return types from the ActionContext methods are generic Objects such as Map types. When a method is executed within a servlet container, the map includes servlet-specific information such as HttpServletRequest attributes. When the same request is made outside of the servlet container, the map contains generic application-specific information.


The View in WebWork is embodied by JSP pages and Servlets, XML and HTML pages, and non-Web based technologies such as Swing Java classes. A rich set of taglibs is associated with the WebWork framework and can aid in building complex views and solving logical problems. We then looked two additional features provided by WebWork: A Value Stack, and a rich expression language, which collaboratively help to limit and simplify the code in your view. This was followed by an interesting comparison of the WebWork and JSTL expression languages.




"Knowing about antipatterns helps us learn from the experience of those who have gone before," Owen said with a gleam of wisdom in his eyes.


An antipattern is a literary form consisting of words and diagrams that is used to communicate a commonly occurring problem. It is past experience 'encapsulated' in the form of good advice. An antipattern addresses a system of errors with the same root cause.


Design patterns provide a blueprint for common solutions. They establish good ways to solve a problem and provide an abstract communication vehicle; however, Design patterns do not identify problems in existing code, warn against problematic implementations, or analyze the root causes of a problem.


It is important to study antipatterns as they train us to better identify and solve existing problems as we refactor our code. Antipatterns and refactoring are intimately related. Refactoring is the work required to get from an antipattern to a design pattern. Antipatterns help you decide what to refactor, identify 'broken areas' in your code, and elminate traps while you refactor.


One antipattern we talked about was the Y2K. We discussed the motivations for why Y2K came about, one of the reasons being that memory and storage were more precious in the past and it was thought that the software that was being created back then wouldn't be around for another 30 years.


Owen said that the moment you press Control-S to save your code, it becomes legacy code, emphasizing the point that it is always important to think about the future consequences of the way you design your applications today.


We looked at several categories of antipatterns and studied an example of each. Some of the categories were coding and idiom antipatterns (such as Java memory leaks), architectural antipatterns (such as round tripping), and process antipatterns (such as performance thrashing). One of the development tier antipatterns we looked at was 'The Magic Servlet', which places the model, view and controller logic into a single servlet.


Pattern Hatching Exercise and a Group Lab


It was Day 5, the home stretch, and the class was visibly exhausted from an intense wEEk of eating, slEEping, and breathing J2EE Patterns. Owen did a great job in the afternoon of kEEping things lively. Apart from the usual energy he showed through his emphatic gestures, flailing hands, and lightning wit, Owen tactfully used varying accents to explain certain slides in the voices of a J2EE-savy mobster from Brooklyn and a Texan cowboy. As a treat, he sang us his famous 'EJB' song, sung to the tune of '99 bottles of bEEr on the wall'.


The Pattern Hatching Exercise proved to be just the thing we nEEded to start communicating our knowledge of J2EE patterns. Learning about patterns is different from using them to solve problems and to communicate. Patterns are things that take time and practice to utilize effectively. We would spend the next couple of hours applying patterns, and thinking about how to use them in a real system.


As an example application, we looked at the 'Hungry Hippo Hamburger Hotel', which was a J2EE app that enables wireless access protocol ordering of custom hamburgers. The application remembers customers and their previous orders, and uses that information to possibly display customized messages or menus to returning customers. It also notifies the delivery system when a hamburger is ready.


The class actively brainstormed ideas on how this application would be designed. We asked ourselves questions like 'What reference architectures can we use?', 'What J2EE frameworks can we use?', etc. As we spewed out ideas, Owen wrote them on the board. It was decided that we should use WebWork as our framework. One student said that we would nEEd a WAP interface, and for bEEper clients, JMS queues for asynchronous communication. Laughhter erupted as somebody retorted that "if we queue up our orders, our hamburgers could get cold." It was said that our wireless client would first talk to a gateway, which would communicate with a Web server. The WAP client would basically pass in lightweight Strings comprising order information. We'd nEEd our typical entity EJBs such as an OrderBean and a CustomerBean.


A brief debate betwEEn Owen and one of the students followed on whether we should use stateful session beans to hold our state or HTTPSessions. The class was generally opposed to using stateful session beans, so we decided to store state in the session. To decouple our layers, we wanted to use the Business Delegate and the Data Access Objects (DAOs). To increase the performance of our system, we decided to use the Read Mostly Pattern or JDBC for Reading (for the bulk loading of data).


For the final lab, the class was separated into 4 groups and provided with a series of pattern-related topics in which we would combine patterns we had learned about or even 'hatch' new patterns to solve a particular problem.


One group looked at the Visitor pattern and discussed how it could be used to refactor a sample gambling application we had worked with in a previous lab. Another group drew up a preliminary design for enabling our completed gambling application to fully integrate with another game system. They discussed particular patterns and groups of patterns that would be used to accomplish this, backing up their decisions by addressing the guiding principles and motivations behind them. A third group's task was to search the Web for a pattern not covered in the course; they selected the Observer Pattern, which defines a one-to many dependency betwEEn objects so that when one object changes state, all its dependents are notified and updated automatically. They covered it extensively, using PML and Sequence diagrams.


Patterns: A Language and a State of Mind


The use of patterns is prevalent in many fields such as Art and Architecture, the Sciences, Manufacturing, and Software development. Patterns are a convenient and effective way of transmitting expert knowledge. In the J2EE community, we strive to formalize the presentation of patterns and ensure the frEE distribution and promotion of them. During the course of the wEEk, we learned more than just the names of and motivations behind different patterns. We learned how and when to use them, and started thinking about design problems in an entirely new way.


Patterns usage offers the benefit of better communication in projects, quicker decision making, and the ability to analyze existing and future solutions with improved mental tools. Armed with a knowledge of patterns, many felt that they could now quickly begin to tackle problems and save themselves a great deal of confusion. Students who were already using existing patterns felt more passionate about them and were very interested in implementing related patterns in their systems.


There were many different motivations behind the attendEEs' decisions to attend J2EE Patterns; however, the common benefits they reaped from the course were starting to become clear. They were heading back to their projects with a renewed sense of confidence. No longer would they have to work hard to solve a problem for which there was already a documented solution. Empowered with the techniques and mindset to look at a problem from a higher-level and with the knowledge of 'pattern-speak' (the necessary vocabulary to communicate and understand patterns), many were ready to go back and apply the patterns they had learned, to imbue their new or existing projects with greater robustness and integrity.


<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

以功能开发者角度理解J2EE核心模式(Core J2EE Patterns)

一. 目标 模块化、松耦合、紧内聚、提高可维护性和重用性。这些我们听得最多了,看起来很空,但模式确实做到了。实现主要用到:封装、分层、AOP 二. 理解 我粗劣看完《J2EE核心模式》第二版,有一些理...
  • stephenluu
  • stephenluu
  • 2014年05月07日 23:02
  • 1836

J2EE 中的几种常用模式?及对设计模式的一些看 法

Session Facade Pattern:使用 SessionBean 访问 EntityBean; Message Facade Pattern:实现异步调用; EJB Command Pa...
  • uniquewonderq
  • uniquewonderq
  • 2015年06月09日 20:53
  • 1507


框架一瞥(基于J2EE) ---------------------------------------------------------------------------------------...
  • yuqun
  • yuqun
  • 2005年02月02日 11:02
  • 1500


---------------------------------------------------------------------------------------------- 本文属原创...
  • zxhong
  • zxhong
  • 2005年01月13日 08:33
  • 3495


 1.Model1模式  2.Model2模式(Model2x模式) 3.MVC模式 Model1的基础是JSP文件,它由一些相互独立的JSP文件和其它一些Java CLass(不是必须的)组...
  • hll19950830
  • hll19950830
  • 2015年08月26日 11:36
  • 666


在新建web工程时 如果选的是javaEE5自带的jstl,仅能够使用tomcat6.x才能正常使用el 如果选的是j2ee1.4或者j2ee1.3时,需要手动添加jstl, 默认与tomcat...
  • ruizefirst
  • ruizefirst
  • 2014年06月05日 15:19
  • 638


总共23种,分为三大类:创建型,结构型,行为型 我只记得其中常用的6、7种,分别是: 创建型(工厂、工厂方法、抽象工厂、单例) 结构型(包装、适配器,组合,代理) 行为(观察者,模版,策略) 然后再针...
  • u012927417
  • u012927417
  • 2014年08月06日 13:36
  • 3744


介绍4种常用的设计模式: 1.业务委托模式:表示层组件将与业务层服务交互。这样,业务层改变会使表示层也需要改变。业务委托模式可以解决这个问题。 业务委托模式的目的就是减少表示层和业务层的耦合,隐藏...
  • guoxuepeng123
  • guoxuepeng123
  • 2012年08月10日 00:12
  • 1652

比较Java EE 5平台和J2EE 1.4平台

    本文对 JavaTM Platform Enterprise Edition 5 ("Java EE 5") 与 Java 2 Platform Enterprise Edition 1.4 ...
  • hdy007
  • hdy007
  • 2007年03月26日 10:43
  • 803


  • cd_cd
  • cd_cd
  • 2017年12月21日 16:40
  • 200