2011年10月11日 星期二

SCEA ch04 - Patterns


  • Patterns used with J2EE Framework


[caption id="attachment_482" align="alignnone" width="700" caption="J2EE used pattern"]J2EE used pattern[/caption]

  • Proxy Pattern


代理者模式徹底地將介面與實作切開來了,如果當實作品需要變更調整時,代理者仍然還是優雅的在那存在著,在大型架構的設計面上,最常用的用途便是考量到了物件初始化的昂貴資源需求,為了效能考量,可用一個健全的方法( it is a sound approach),那就是把物件實體化的動作延遲到當你真正需要此物件時,才進行初始化。




[caption id="attachment_484" align="alignnone" width="322" caption="proxy pattern"]proxy pattern[/caption]

  • Decorator Pattern


裝飾者模式提供相同的物件準則合約(合同),但是提供了具有延展性的功能,裝飾者模式一般使用場合是放在需要動態的添增物件功能時使用,這個解法包含了封裝既有物件的介面在裏頭,而裝飾者與原生物件繼承個相同的abstract 介面。


常見的介紹系統如同點餐、點飲料、點牛排等系統,店家除了提供單純的原生商品以外,也提供依照顧客食用喜好,來選擇添加的調味做出合適的商品,這個pattern的重點就是"裝飾者"本身是被裝飾者的子類別,然後由子類別封裝新的功能來服務。EJBObject就是一個裝飾者為Bean class提供了裝飾服務。




[caption id="attachment_485" align="alignnone" width="416" caption="Decorator pattern"]Decorator pattern[/caption]

  • Factory Method Pattern


The Factory Method pattern provides the ability to define an interface for creating an object but defers instantiation to subclasses.


在J2EE中使用factory method 的是EJBHome,透過ejbhome 建立了 new EJBObject。




[caption id="attachment_486" align="alignnone" width="433" caption="Factory Method Pattern"]Factory Method Pattern[/caption]

  • Abstract Factory Pattern


provides an interface for creating families of related or dependent objects without specifying concrete classes. J2EE technology use this pattern for the EHBHome interface, which creates new EJBObjects.




  • Presentation tier Patterns


[caption id="attachment_487" align="alignnone" width="689" caption="presentation tier pattern"]presentation tier pattern[/caption]

  • Business Tier Pattern


[caption id="attachment_488" align="alignnone" width="688" caption="Business tier pattern"]Business tier pattern[/caption]

  • Integration Tier Pattern


[caption id="attachment_489" align="alignnone" width="687" caption="integration tier pattern"]integration tier pattern[/caption]

  • DAO Pattern


DAO pattern 區隔了系統資源以及存取資源的實作程式碼,在設計觀點上,我們會選擇使用一個abastract DAO ,用來進行將資料存取的工作都封裝起來,之後可藉由不同的RDBMS 的使用需求,來提供不同的DAOImpl,達成鬆散耦合的需要。




  • Value Object


You should use a value object when the business entity being modeled has :


1. Only methods that get values from the object's internal state( that is, immutable state)


2. A life cycle that is completely controlled by another object.


3. A relatively small size.




  • Session Bean Facade


老生常談,透過一個統一封裝的介面,用來處理各式各樣的需求來源,並將訊息傳送到後方的object進行處理,這方法也同時等於隱藏了資料結構的實作內容。




[caption id="attachment_490" align="alignnone" width="598" caption="session facade"]session facade[/caption]

  • JEE Connector


[caption id="attachment_492" align="alignnone" width="700" caption="JEE Connector"]JEE Connector[/caption]

  • Service Locator


Service Locator 是用在需要存取通用的J2EE 服務時(像是 JMS Factories , data sources , EJBHome object , and so on)。幾乎沒有異議的是使用JNDI lookup取得。


在系統設計時,有些主要的設計問題會變得很明顯,當client與 service是不在同一個location時(也可假定是不同的JVM):


1. Entire seconds can go by each time these operations are carried out.


2. Each interested client must be complicated by the inclusion of JNDI lookup.


3. Unnecessary network traffic is added to the system.


為了避免這些設計上的蠢問題,我們常使用caching service object 來解決這問題,而考量到效能議題時,可以做以下幾種措施:


1. 如果Object 只需要在local端使用,那就完全不要使用EJB,指使用POJOs即可。


2. You can use local interfaces if EJB calls another EJB ( or a servlet calling an EJB) from the same container and the same virtual machine.


3. Wrap entity beans into session bean, reduce the remote calls communication, also called session bean facade.


4. Control serialization by modifying unnecessary data variables with a transient keyword to avoid unnecessary data transfer over network.


5. Cache EJBHome references to avoid JNDI lookup overhead ( aka Service locator)


6. 對於不需要存在於交易機制中的method,請宣告為NotSupported or Never。


7. 交易運作時間及盡可能的縮小他的lock範圍,依照lock isolation範圍程度可分為 dirty read, repeatable read , phantom reads.


Repeatable reads 指的是 the data read is locked against changes. If the data is locked, it can't be changed by any other transaction until this transaction ends.


Dirty read occurs when the first transaction reads  uncommited changes made by a second transaction.  If the second transaction is rolled back, the data read by the first transaction becomes invalid because the rollback undoes the changes. the first transaction won't be aware that the data it has read has become invalid.


Phantom reads occur when new row added to the database are detectable by transactions that started prior to the insert. Queries will include rows inserted by other transactions after their transaction has started.




  • Security


A principal is an entity to which privileges can be assigned.


A role is a collection of privileges.


Authentication is a mechanism by which callers and service providers prove that they are acting on behalf of specific users or systems.


Web-tier authentication consists of HTTP basic authentication , form-based authentication, HTTPS mutual authentication.


在J2EE架構裡,所謂的protection domain指的是一組entities的集合裡,彼此是互相信任的,只要是在同一個PD裡面的元件交互運作溝通時,那基本上是完全沒有限制的。

沒有留言: