Potala(4)——Command Pattern

  好久没上cnblogs了。昨天上来看下,发现还落下好些文章没有写完。虽然不太高兴写,但做事还是有头有尾的比较好,就写完吧:)

 

  关于业务逻辑封装,在项目中我们用了Command Pattern来做。关于理由 ,《Java Persistence with Hibernate》一书说得挺好的,我也不另外阐述了,就直接copy过来好了:

   a third tier application may move the presentation layer to a separate virtual machine, the current persistence context can’t be held open anymore until the view has been rendered. This is typically the case in three-tiered EJB application, or in an architecture with a rich client in a separate process.
   If the presentation layer runs in a different process, you need to minimize the requests between this process and the tier that runs the business and persistence layers of the application. This means that you can’t use the previous lazy approach, where the view is allowed to pull data from the domain model objects as needed. Instead, the business tier must accept responsibility for fetching all data that is needed  ubsequently for rendering the view.

   Although certain patterns that can minimize remote communication, such as the session facade and data transfer object (DTO) patterns, have been widely used in the Java developer community, we want to discuss a slightly different approach. The Command pattern (often also called EJB Command) is a sophisticated solution that combines the advantages of other strategies.

   具体的接口和实现大家可以去参考下一些设计模式的书,或者也可以对《Java Persistence with Hibernate》上写的代码进行一些丰富和修改,比如给Command加入更多的方法,CommandHandle可以用泛型来实现等等,不再累述。

  

   另一个问题是:One operation, One Session.这个在两层的系统中可以用Open Session In View (OSIV)来解决,三层的系统就有比较多方案了,我们使用了组合不同Command到一个Command中来解决这个问题,最基本的思想也是出自《Java Persistence with Hibernate》一书,具体也不多介绍了。


posted @ 2009-03-07 11:09  Marco Zeng  阅读(306)  评论(0编辑  收藏  举报