簡體   English   中英

將這兩個類提取到一個公共類的最佳實踐是什么?

[英]What is the best practice for extracting these 2 classes to a common class?

我有兩個非常簡單的課程。 兩者都有兩個都是UUID的變量,我正在考慮將其提取到一個通用類中的最佳方法。

以下是我定義的類:

@AllArgsConstructor
final class ObjectKey
{
    @Getter
    private UUID sourceId;

    @Getter
    private UUID targetId;

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        ObjectKey key = (ObjectKey) o;
        return sourceId.equals(key.getSourceId()) && targetId.equals(key.getTargetId());
    }

    @Override
    public int hashCode()
    {
        return (sourceId.hashCode() + targetId.hashCode()) * 31;
    }
}


final class AnimalKey
{
    @Getter
    private UUID catId;

    @Getter
    private UUID dogId;

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        AnimalKey key = (AnimalKey) o;
        return dogId.equals(key.getDogId()) && catId.equals(key.getCatId());
    }

    @Override
    public int hashCode()
    {
        return (catId.hashCode() + dogId.hashCode()) * 31;
    }
}

所以我想知道什么是最佳實踐,因為兩個類基本上具有相同類型的相同數量的變量……只是變量名不同。

當然,一個選擇是只有一個具有通用名稱(如firstIDsecondID 但是,使用這些類的getter / setter方法編寫的代碼不是很容易閱讀和理解。 有什么我缺少的明顯方法嗎?

我認為沒有您想像的那么優雅,或者沒有太多的必要/好處。 但是,想到的兩個選項是擁有一個通用的基類和一些顯式的getter,它們映射到更友好的名稱和/或TupleUUID的對象。

@AllArgsConstructor
final class TupleUUID
{
    @Getter
    private UUID primaryId;

    @Getter
    private UUID secondaryId;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TupleUUID that = (TupleUUID) o;
        return Objects.equals(primaryId, that.primaryId) 
           && Objects.equals(secondaryId, that.secondaryId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(primaryId, secondaryId);
    }
}
@AllArgsConstructor
class TupleKey {

    @Getter(AccessLevel.PROTECTED)
    private TupleUUID uuid;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass())  {
            return false;
        }

        TupleKey that = (TupleKey ) o;
        return Objects.equals(key, that.key);
    }

    @Override
    public int hashCode() {
        return uuid.hashCode();
    }
}
@AllArgsConstructor
final class ObjectKey extends TupleKey {
    public UUID getSourceId() {
        return getUuid().getPrimaryId();
    }

    public UUID getTargetId() {
        return getUuid().getSecondaryId();
    }
}
@AllArgsConstructor
final class AnimalKey extends TupleKey {
    public UUID getCatId() {
        return getUuid().getPrimaryId();
    }

    public UUID getDogId() {
        return getUuid().getSecondaryId();
    }
}

當然,您可以對此進行混合和匹配並擺脫TupleKey基類,或者將TupleUUID滾動到TupleKey中。

除非您將擁有許多帶有兩個唯一標識符的對象,否則這樣做的好處令人懷疑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM