在整個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的次序,我也說不清這是否站得住腳。
- public abstract class DomainObject implements Comparable{
- private Long id;
- private String code;
- private String name;
- private String description;
- private TimePeriod lifecycle;
- private int version;
- private int serialNumber;
- /**
- * 從持久層中重新構造DomainObject
- */
- protected DomainObject(); {
- }
- /**
- * 業務上創建DomainObject
- */
- protected DomainObject(String name, String code, int serialNumber,
- String description); {
- this.id = IdGenerator.getCurrent();.nextId(this);;
- this.version = 0;
- this.lifecycle = new TimePeriod();;
- this.name = name;
- this.code = code == null ? id.toString(); : code;
- this.serialNumber = serialNumber;
- this.description = description;
- }
- public void destroy(); {
- }
- @Override
- public int hashCode(); {
- assert id != null : this.getClass();.getName(); + " id為null";
- return id.hashCode();;
- }
- @Override
- public boolean equals(Object obj); {
- if (!(obj instanceof DomainObject););
- return false;
- DomainObject domainObj = (DomainObject); obj;
- return this.getId();.longValue(); == domainObj.getId();.longValue();;
- }
- public int compareTo(Object obj); {
- assert this.getClass(); == obj.getClass(); : "無在不同的DomainObject間比較";
- DomainObject o = (DomainObject); obj;
- return this.serialNumber.compareTo(o.serialNumber);;
- }
- /**
- * 判斷DomainObject是否已過期
- */
- public boolean isExpired(); {
- Calendar now = Calendar.getInstance();;
- if (now.compareTo(lifecycle.getEnd();); > 0);
- return true;
- else
- return false;
- }
- public TimePeriod getLifecycle(); {
- return (TimePeriod); lifecycle.clone();;
- }
- public void setEnd(Calendar end); {
- lifecycle.setEnd((Calendar); end.clone(););;
- }
- public void checkVersion(int version); {
- if (this.version.compareTo(version); != 0);
- throw new DataChangedByOthersException();;
- }
- }
安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢】