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