A Look Inside J2EE Patterns(3) (轉)
A thick smog had descended upon downtown :namespace prefix = st1 ns = "urn:schemas--com::smarttags" />
The coffee was out in full force this morning as some people were up late watching post season baseball and had to catch an early morning train to be at class. Yet people were still alert, asking a myriad of technical questions in reference to the slides and labs.
As a lead into the next section, Owen reiterated yesterday's lesson on ain and Custom DTby mentioning their drawbacks. Domain and Custom DTOs tightly couple the client to the system in that the client is burdened with the need to identify and proy construct DTO types, especially when there are a multitude of domain s. The number of DTOs can get quite large especially when using Custom DTOs. The solution to this problem is to use Generic DTOs, of which there are two types: Hashmap and Rowset.
Generic DTOs: Hashmap and Rowset
One way to reduce the number of DTO types is to implement a Hashmap DTO which enables the storage of any Object type identified by an attribute key. The client creates a Hashmap and stuffs it with name value pairs and passes it as a parameter to an . The Typed Hashmap is an extension of this pattern; it includes and wraps the HashMap with a type-checking class that will enforce that only the originally inserted types and keys are allowed. By 'hardening' the object types and keys, you ensure the Typed Hashmap ensures that they remain constant.
The Rowset DTO pattern is appropriate whenever data is accessed through JC and large ResultSets are returned. You can use a java..RowSet object to encapsulate the rows retrieved from your table. In a nut, the RowSet object is simply a ResultSet object that functions as a JavaBeans component.
A gentleman from the back of the room, with a slight English accent asked Owen a question, but wasn't sure if he was correctly pronouncing his name.
"Oh don't worry about it," Owen assured him, "I had a Russian teacher once who used to call me Oven." The class had a good laugh at this.
The question he asked, with reference to the RowSet DTO Pattern, was how one can ensure that when two clients are looking at the same row, that that data doesn't become stale.
"We're just getting to that," responded Owen. "Your question is a great segway into our next topic: the DTO Version Number Pattern."
The DTO Version Number Pattern
The DTO Version Number pattern solves the problem of data becoming stale when two clients are reading and updating the same data. The solution is to use Version Numbers to implement staleness checks on Entity Beans. The Version Numbers confithat the data in memory is the same as the version in the database. Version numbers are added to the DTOs, the Entity Beans and the Database tables.
In the DTO version number lab, we were presented with a scenario where whenever two clients tried to update a field from the same entity bean, only one client's changes were committed. The DTOs sent to the clients were inside of separate transactions so that when the first client updated the data, the second client had no knowledge of these updates and was now working with a stale DTO. The lab required solving the classic 'readers-writers' problem. Some validation logic also had to be added to the DTOs as these were part of the lab requirements, since many of the clients were entering invalid data.
At the end of the lab, Owen asked the class why we used a Custom DTO in the lab instead of Domain DTOs. Custom DTOs reduce latency. But Domain DTOs might be more useful if you have many usecases. It all depends on the immediate and future needs of the system.
The Session Facade
Before lunch, Owen started lecturing on the pattern that he described as the "grand daddy of them all": The Session Facade Pattern.
"You've already been using the Session Facade in every lab we've touched, except the JDO one. A 'facade' is when you simplify a complex system," Owen explained. So in effect, the class had already been using 'facades' by creating layers of abstraction that hide underlying complexity.
The more general Facade Pattern serves to cleanly separate the Logic and Data Layers. The principle behind the Session Facade, a specialization of the Facade Pattern, is to achieve the maximum reuse of Business Logic and Data. The Session Facade decouples persistence code from business logic.
By implementing the Session Facade Pattern, reusability is improved because data components no longer encapsulate application specific logic and are now useful accross applications. This reduces 'code bloat' in entity beans, which really should only represent business data. Conversely, your business logic can now also be applied to various data components.
"Entity beans are designed to be shared between different processes. We discourage the use of any business methods in your entity bean layer," Owen responded to a question on whether entity beans should or shouldn't contain business methods.
Reducingwork overhead is another benefit of this pattern. The EJB client can execute a use case's business logic in one transaction and one bulk call, instead of having to deal directly with a multitude of entity beans, possibly accross a network. The client really should only have a single point of entry into the system. Owen showed us an antipattern sequence diagram of an EJB client talking directly with the entity layer which helped to reinforce the idea of using the Session Facade to reduce network overhead.
The gentleman from the back of the room asked why we couldn't simply use a plain Java class to front our entity beans. Owen reminded him that the Session bean embodies all the services the container offers such as transactions. Transactions should be handled by the container and not programmatically.
Just before we were dismissed to sate our appetites at one of the many fine
After lunch, we finished off the Session Pattern and looked at an interesting variation of it: The Message Facade, which is an asynchronous form the Session Facade. The benefits of it are similar: reduced network overhead, increased scalability, with the addition of decreased response times for the client, and client independent transaction semantics. As an example we looked at a -based airline reservation system, which, given the nature of the airline industry, cannot make clients wait as it sometimes takes days for a response. Somebody asked which commercial systems are using and one gentleman, who works for a pharmaceutical firm, said that his company uses it. Clients, such as researchers and doctors, submit large protein strings which are parsed and sent out to different J2EE applications for asynchronous processing. Owen mentioned that many projects he's done for financial institutions have implemented JMS technology.
The Command Pattern
Towards mid-afternoon, we began our study of the Command Pattern. So far, we had learned how to encapsulate data in a Data Traner Object. The Command Pattern uses a similar strategy, except it encapsulates a request object into a Command Bean, which the client sets the attributes on. The client then simply calls the executeCommand() method which transparently uses some routing logic to get to the destination EJB. The routing logic then sends the Command Bean to a CommandServer (a CommandServerBean), which invokes the execute() method of the Command Bean. All business logic is written in the execute() method of the Command Bean, which is implemented from an abstract Command interface.
The Command Pattern decouples the client from EJBs. For instance, s that need to invoke business logic no longer need to know about the methods required to interact with the Session Bean layer. The CommandServer and routing logic code are reusable across applications. The Command Bean is also reusable in non-EJB environments. The essential benefit of this pattern is the decoupling of the presentation layer from server side components.
A further variation of this pattern was the Data Access Command bean, which helps to decouple business logic from the persistance mechanism. The Data Access command bean encapsulates persistance logic (getting connections, using prepared statements, all code, etc.).
Service Locator (EJBHomeFactory)
We finished off the third day by looking at the Service Locator, or EJBHomeFactory Pattern. JNDI lookup calls from the client are expensive operations with signifigant performance overheads. The Service Locator solves this problem by abstracting EJB Home lookup code into a reusable Java Class. It obtains home stubs from the JNDI on initial client requests, and caches them for all subsequent requests, which results in significant performance gains.
Moving Into the Web Tier
On the fourth day of J2EE Patterns, our journey continues through the Business tier and onwards into the Web tier. Day 4 will be an interesting class, with the morning spent discussing The Business Delegate Pattern and Marchitecture. We then move on to J2EE Web Patterns and the .
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-990711/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- J2EE模式-控制反轉模式
- React PatternsReact
- Digital PatternsGit
- [Patterns] ABT
- 回顧2022年,我做了這些事?,來look、look?
- Concurrency Patterns in GoGo
- Design Patterns - Component
- Full postback triggered by LinkButton inside GridView inside UpdatePanelIDEView
- Look what I found out.
- Advanced-react-patterns(2)React
- Glob Patterns匹配模式使用模式
- GCD Inside: GCD 宏GCIDE
- Design Patterns in Android:裝飾模式Android模式
- Java 21 新特性:Record PatternsJava
- Design Patterns 之工廠模式模式
- Linux基礎命令---顯示文字lookLinux
- Linux基礎命令—文字顯示lookLinux
- typedef and struct inside class definition?StructIDE
- J2EE 核心模式模式
- Design Patterns in Android:責任鏈模式Android模式
- [翻譯]通訊模式(Communication Patterns)模式
- YOLO(You Only LOOK Once)論文詳讀YOLO
- J2EE開發筆記(一)—— J2EE開發環境配置筆記開發環境
- J2EE - IncompatibleClassChangeError: Implementing classError
- 《Inside UE4》開篇IDE
- Mach-O Inside: BSS SectionMacIDE
- [Inside HotSpot] Java分代堆IDEHotSpotJava
- luoguP2947 [USACO09MAR] Look Up S
- LOOK 直播活動地圖生成器方案地圖
- J2EE模式-依賴注入模式依賴注入
- GCD Inside: GCD 資料結構GCIDE資料結構
- UIKit Inside: frame bounds position anchorPoint centerUIIDE
- Inside Java Newscast #1 深度解讀IDEJavaAST
- WebKit Inside: DOM樹的構建WebKitIDE
- Components 與 Patterns 究竟有什麼區別
- PostgreSQL DBA(124) - Develop(Putting multiple LIKE patterns into an array)SQLdev
- J2EE模式-傳輸物件模式模式物件
- [Inside HotSpot] Serial垃圾回收器Full GCIDEHotSpotGC
- [Inside HotSpot] UseParallelGC和UseParallelOldGC的區別IDEHotSpotParallelGC