关于golang:聊聊dddsamplecore的model

6次阅读

共计 2320 个字符,预计需要花费 6 分钟才能阅读完成。

本文次要钻研一下 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

  • dddsample-core
正文完
 0