聊聊dddsample-core的model
序
本文主要研究一下dddsample-core的model
Entity
public interface Entity<T> {
/**
* Entities compare by identity, not by attributes.
*
* @param other The other entity.
* @return true if the identities are the same, regardless of other attributes.
*/
boolean sameIdentityAs(T other);
}
Entity接口定义了sameIdentityAs方法
ValueObject
public interface ValueObject<T> extends Serializable {
/**
* Value objects compare by the values of their attributes, they don't have an identity.
*
* @param other The other value object.
* @return <code>true</code> if the given value object's and this value object's attributes are the same.
*/
boolean sameValueAs(T other);
}
ValueObject接口定义了sameValueAs方法
TrackingId
public final class TrackingId implements ValueObject<TrackingId> {
private String id;
/**
* Constructor.
*
* @param id Id string.
*/
public TrackingId(final String id) {
Validate.notNull(id);
this.id = id;
}
/**
* @return String representation of this tracking id.
*/
public String idString() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TrackingId other = (TrackingId) o;
return sameValueAs(other);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean sameValueAs(TrackingId other) {
return other != null && this.id.equals(other.id);
}
@Override
public String toString() {
return id;
}
TrackingId() {
// Needed by Hibernate
}
}
TrackingId实现了ValueObject接口,sameValueAs方法通过equals方法判断
Type
public enum Type implements ValueObject<Type> {
LOAD(true),
UNLOAD(true),
RECEIVE(false),
CLAIM(false),
CUSTOMS(false);
private final boolean voyageRequired;
/**
* Private enum constructor.
*
* @param voyageRequired whether or not a voyage is associated with this event type
*/
private Type(final boolean voyageRequired) {
this.voyageRequired = voyageRequired;
}
/**
* @return True if a voyage association is required for this event type.
*/
public boolean requiresVoyage() {
return voyageRequired;
}
/**
* @return True if a voyage association is prohibited for this event type.
*/
public boolean prohibitsVoyage() {
return !requiresVoyage();
}
@Override
public boolean sameValueAs(Type other) {
return other != null && this.equals(other);
}
}
Type枚举实现了ValueObject接口,其sameValueAs方法通过equals判断
DomainEvent
public interface DomainEvent<T> {
/**
* @param other The other domain event.
* @return <code>true</code> if the given domain event and this event are regarded as being the same event.
*/
boolean sameEventAs(T other);
}
DomainEvent接口定义了sameEventAs方法
小结
dddsample-core定义了Entity、ValueObject、DomainEvent接口,它们分别定义了sameIdentityAs、sameValueAs、sameEventAs方法。
doc
code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun
推荐阅读
2022年终总结
最近两年开始陷入颓废中,博客也写的越来越少了。究其原因,主要还是陷入了职业倦怠期,最近一次跳槽感觉颇为失败,但是碍于给的薪资高,为了五斗米折腰,又加上最近行情不好,想要往外跳也跳不了,就这样子一直...
codecraft阅读 711
Golang 中 []byte 与 string 转换
string 类型和 []byte 类型是我们编程时最常使用到的数据结构。本文将探讨两者之间的转换方式,通过分析它们之间的内在联系来拨开迷雾。
机器铃砍菜刀赞 22阅读 55.1k评论 1
年度最佳【golang】map详解
这篇文章主要讲 map 的赋值、删除、查询、扩容的具体执行过程,仍然是从底层的角度展开。结合源码,看完本文一定会彻底明白 map 底层原理。
去去1002赞 14阅读 11k评论 2
年度最佳【golang】GMP调度详解
Golang最大的特色可以说是协程(goroutine)了, 协程让本来很复杂的异步编程变得简单, 让程序员不再需要面对回调地狱, 虽然现在引入了协程的语言越来越多, 但go中的协程仍然是实现的是最彻底的. 这篇文章将通过分析...
去去1002赞 13阅读 11.2k评论 4
【已结束】SegmentFault 思否技术征文丨浅谈 Go 语言框架
亲爱的开发者们:我们的 11 月技术征文如期而来,这次主题围绕 「 Go 」 语言,欢迎大家来参与分享~征文时间11 月 4 日 - 11 月 27 日 23:5911 月 28 日 18:00 前发布中奖名单参与条件新老思否作者均可参加征文...
SegmentFault思否赞 11阅读 4.7k评论 11
【Go微服务】开发gRPC总共分三步
之前我也有写过RPC相关的文章:《 Go RPC入门指南:RPC的使用边界在哪里?如何实现跨语言调用?》,详细介绍了RPC是什么,使用边界在哪里?并且用Go和php举例,实现了跨语言调用。不了解RPC的同学建议先读这篇文...
王中阳Go赞 8阅读 3.7k评论 6
【golang】sync.WaitGroup详解
上一期中,我们介绍了 sync.Once 如何保障 exactly once 语义,本期文章我们介绍 package sync 下的另一个工具类:sync.WaitGroup。
去去1002赞 13阅读 30.2k评论 2
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。