国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > DomainModel之DomainObject

DomainModel之DomainObject
2010-01-14 22:32:24  作者:  來源:
相對于UI的開發受限于既有框架的結構,DomainModel有更大的靈活性,因為框架本身由自己開發的。
  在整個DomainModel框架中,最基礎的對象莫過于DomainObject。DomainObject既然是所有領域
對象的父類,就該體現最基礎的特征。并且為其他層或某些方面提供一致的入口。
  “有名萬物之母”,這也是DomainObject中需要體現的。
DomainObject的名有很多,很多的原因是有很多關心它的參與者。

計算機使用的名--ID:必須,通常為64位的Integer。如果數據庫是64位的CPU,據說這樣定ID效率是最高的。

第三方和用戶輸入的名--Code:可選,String類型。盡管計算機知道了DomainObject,但用戶卻不知道,(用戶登錄系統就需要通過Code)第三方的開發人員也不知道。

用戶期望看到的名--Name: 可選,String類型。對應于自然語言中的名。

用戶期望看到的關于該對象的描述之名--Description:可選,String類型。這也需要最不重要的名了,對于該對象的備注,簡要解釋都可以放到這里。

有了名,我們就可以思考DomainObject了,基于“萬物皆過程”的思考,表現過程的屬性是需要加上的。于是
DomainObject有了{TimePeriod lifecycle}字段。lifecycle.start應該可以在某個構造函數中填入,當對象在業務上無效時可以填入lifecycle.end。DomainObject就“存活”于lifecycle之中。
當然你可以殘酷一些,讓DomainObject回歸虛無,直接調用destroy方法,徹底刪除它。

實際上持久化的DomainObject不過是反映了對象在lifecycle期間的當前快照,也就是說DomainObject存在很多快照,
我們可以使用{int version}來標識當前快照。

不知道什么時間,有人在DomainObject中放入了{int serialNumber},說是為了比較同類DomainObject的次序,我也說不清這是否站得住腳。

Java代碼 復制代碼
  1. public abstract class DomainObject implements Comparable{   
  2.        
  3.     private Long id;   
  4.        
  5.     private String code;   
  6.        
  7.     private String name;   
  8.   
  9.     private String description;   
  10.   
  11.           private TimePeriod lifecycle;   
  12.   
  13.           private int version;   
  14.   
  15.           private int serialNumber;   
  16.   
  17.     /**  
  18.     * 從持久層中重新構造DomainObject  
  19.     */  
  20.     protected DomainObject(); {   
  21.   
  22.     }   
  23.   
  24.     /**  
  25.     * 業務上創建DomainObject  
  26.     */  
  27.     protected DomainObject(String name, String code, int serialNumber,   
  28.         String description); {   
  29.         this.id = IdGenerator.getCurrent();.nextId(this);;   
  30.         this.version = 0;   
  31.         this.lifecycle = new TimePeriod();;   
  32.         this.name = name;   
  33.         this.code = code == null ? id.toString(); : code;   
  34.         this.serialNumber = serialNumber;   
  35.         this.description = description;   
  36.     }   
  37.        
  38.     public void destroy(); {   
  39.        
  40.     }   
  41.        
  42.     @Override  
  43.     public int hashCode(); {   
  44.         assert id != null : this.getClass();.getName(); + " id為null";   
  45.         return id.hashCode();;   
  46.     }   
  47.   
  48.     @Override  
  49.     public boolean equals(Object obj); {   
  50.         if (!(obj instanceof DomainObject););   
  51.             return false;   
  52.         DomainObject domainObj = (DomainObject); obj;   
  53.         return this.getId();.longValue(); == domainObj.getId();.longValue();;   
  54.     }   
  55.   
  56.     public int compareTo(Object obj); {   
  57.         assert this.getClass(); == obj.getClass(); : "無在不同的DomainObject間比較";   
  58.         DomainObject o = (DomainObject); obj;   
  59.         return this.serialNumber.compareTo(o.serialNumber);;   
  60.     }   
  61.        
  62.     /**  
  63.     * 判斷DomainObject是否已過期  
  64.     */  
  65.     public boolean isExpired(); {   
  66.         Calendar now = Calendar.getInstance();;   
  67.         if (now.compareTo(lifecycle.getEnd();); > 0);   
  68.             return true;   
  69.         else  
  70.             return false;   
  71.     }   
  72.        
  73.     public TimePeriod getLifecycle(); {   
  74.         return (TimePeriod); lifecycle.clone();;   
  75.     }   
  76.        
  77.     public void setEnd(Calendar end); {   
  78.         lifecycle.setEnd((Calendar); end.clone(););;   
  79.     }   
  80.        
  81.     public void checkVersion(int version); {   
  82.         if (this.version.compareTo(version); != 0);   
  83.             throw new DataChangedByOthersException();;   
  84.     }   
  85.        
  86. }  

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢