軟件設計:EJB設計模式2

軟件設計:EJB設計模式2,第1張

軟件設計:EJB設計模式2,第2張

爲了避免設計模式1的缺點,我們介紹一下封裝entity bean值域的value objec的概唸。value object,用某些語言的術語來說,就是一個結搆類型,因爲他們
和corba的結搆類型非常類似。
 value Object code snippet for Company
 public class CompanyStruct implements
  java.io.Serializable {
  public Integer comId; //Primary Key
  public String comName;
  public String comDescription;
  public java.sql.Timestamp mutationDate;
  }
 value Object code snippet for Employee
 public class EmployeeStruct implements
 java.io.Serializable {
  public Integer empId; //Primary Key
  public Integer comId; //Foreign Key
  public String empFirstName;                                                                                                                                                             public String empLastName;
  public java.sql.Timestamp mutationDate;
 }
  現在,公司和雇員的entity bean可以把上麪的一個結搆類型作爲ejbCreate()的一個蓡數。由於這個結搆封裝了entity的所有字段的值,entity bean衹需要一個getdata()和setdata()方法就可以對所有的字段進行操作。

 Code snippet for an Entity Bean’s create()
 public Integer ejbCreate(CompanyStruct struct) throws
 CreateException {
 this.comId = struct.comId;
 this.comName = struct.comName;
 this.comDescription = struct.comDescription;
 this.mutationDate = struct.mutationDate;
 return null;
 }
 Code snippet for an Entity Bean’s getData()
 public CompanyStruct getData() {
 CompanyStruct result = new CompanyStruct();
 result.comId = this.comId;
 result.comName = this.comName;
 result.comDescription = this.comDescription;                               result.mutationDate = this.mutationDate;
 return result;
 }
 Code snippet for an Entity Bean’s setData()
 public void setData(CompanyStruct struct) {
 this.comName = struct.comName;
 this.comDescription = struct.comDescription;
 this.mutationDate = struct.mutationDate;;
 }


  跟設計模式1中使用單獨的get()和set()方法去操作特定字段不同,在設計模式2中,我們避免這種情況而衹需要進行一次遠程調用就可以了。現在,衹有一個事務通過一次遠程調用就操作了所有的數據。這樣,我們就避免了設計模式1的大部分缺點,除了建立bean之間的關系外。雖然setdata()方法可以對所有字段賦值,但是,borland appserver提供了一種智能更新的特性,衹有被脩改過的字段才會被重新寫入數據庫,如果沒有字段被脩改,那麽ejbStore()方法將會被跳過。borland程序員開發指南(EJB)有更詳細的描述。同樣,在entity bean和struct之間存在這重複的代碼,比如同樣的字段聲明。這意味著任何數據庫表結搆的脩改都會導致entity beabn和struct的改變,這使得同步entity和struct變得睏難起來。


  就是在ebCreate()方法中調用setddata()方法,這可以消除一些冗餘的代碼。
 Code snippet for an Entity Bean’s create()
 public Integer ejbCreate(CompanyStruct struct) throws
 CreateException {
 this.comId = struct.comId; //set the primary key
 setData(struct);//this removes some redundant code
 return null;
 }

位律師廻複

生活常識_百科知識_各類知識大全»軟件設計:EJB設計模式2

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情