在整個DomainModel框架中,最基礎(chǔ)的對象莫過于DomainObject。DomainObject既然是所有領(lǐng)域
對象的父類,就該體現(xiàn)最基礎(chǔ)的特征。并且為其他層或某些方面提供一致的入口。
“有名萬物之母”,這也是DomainObject中需要體現(xiàn)的。
DomainObject的名有很多,很多的原因是有很多關(guān)心它的參與者。
計算機(jī)使用的名--ID:必須,通常為64位的Integer。如果數(shù)據(jù)庫是64位的CPU,據(jù)說這樣定ID效率是最高的。
第三方和用戶輸入的名--Code:可選,String類型。盡管計算機(jī)知道了DomainObject,但用戶卻不知道,(用戶登錄系統(tǒng)就需要通過Code)第三方的開發(fā)人員也不知道。
用戶期望看到的名--Name: 可選,String類型。對應(yīng)于自然語言中的名。
用戶期望看到的關(guān)于該對象的描述之名--Description:可選,String類型。這也需要最不重要的名了,對于該對象的備注,簡要解釋都可以放到這里。
有了名,我們就可以思考DomainObject了,基于“萬物皆過程”的思考,表現(xiàn)過程的屬性是需要加上的。于是
DomainObject有了{(lán)TimePeriod lifecycle}字段。lifecycle.start應(yīng)該可以在某個構(gòu)造函數(shù)中填入,當(dāng)對象在業(yè)務(wù)上無效時可以填入lifecycle.end。DomainObject就“存活”于lifecycle之中。
當(dāng)然你可以殘酷一些,讓DomainObject回歸虛無,直接調(diào)用destroy方法,徹底刪除它。
實際上持久化的DomainObject不過是反映了對象在lifecycle期間的當(dāng)前快照,也就是說DomainObject存在很多快照,
我們可以使用{int version}來標(biāo)識當(dāng)前快照。
不知道什么時間,有人在DomainObject中放入了{(lán)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;
- /**
- * 從持久層中重新構(gòu)造DomainObject
- */
- protected DomainObject(); {
- }
- /**
- * 業(yè)務(wù)上創(chuàng)建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();;
- }
- }
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】