简体   繁体   English

具有双向关联的类的 equals() 方法

[英]equals() method for classes with bidirectional association

I am trying to implement equals method for Java classes Book and Chapter in my application.我正在尝试在我的应用程序中为 Java 类BookChapter实现equals方法。 Book has a set of Chapter s, while a Chapter has an associated Book . Book有一组Chapter ,而Chapter有一个关联的Book The bidirectional association is shown as below:双向关联如下图所示:

class Book{
    private String isbn;
    private String name;
    private Date publishDate;
    private Set<Chapter> chapters;
    ...

    public boolean equals(Object o){
        if(o == this){
            return true;
        }
        if (!(o instanceof Book)){
            return false;
        }
        Book book = (Book)o;
        if( (this.isbn.equals(book.getIsbn()) ) && (this.name.equals(book.getName())) &&(this.publishDate.equals(book.getPublishDate())) &&(this.chapters.equals(book.getChapters())) ){
            return true;
        }else{
            return false;
        }

    }
}

Now I tried to implement equals for Chapter :现在我尝试为Chapter实现equals

public class Chapter {
    private String title;
    private Integer noOfPages;
    private Book book;
    ...

    public boolean equals(Object o){
         if(o == this){
            return true;
        }
        if (!(o instanceof Chapter)){
            return false;
        }
        Chapter ch = (Chapter)o;
        if((this.title.equals(book.getTitle())) && (this.noOfPages.intValue()== book.getNoOfPages().intValue())  ){
            return true;
        }else{
            return false;
        }
    }

}

Here, I am wondering if I need to compare the book field as well.在这里,我想知道是否还需要比较书籍领域。 Wouldn't that start an infinite loop?这不会开始无限循环吗? What is the correct way of implementing the equals method for such bidirectional associations?为这种双向关联实现equals方法的正确方法是什么?

A book should be equal to another book only if their ISBNs are equal.只有当它们的 ISBN 相同时,一本书才应该等于另一本书。 So implement the book equals only based on that field.因此,仅基于该字段实施 book equals。

For the chapter - compare the chapter number and the owning Book对于章节 - 比较章节编号和拥有的Book

(I'm assuming this is Java) In the Chapter class equals method, you could just compare the book references (that is, using ==, not equals). (我假设这是 Java)在 class 等于方法一章中,您可以只比较书籍参考(即使用 ==,而不是等于)。 This only compare references, so it would avoid an infinite loop.这仅比较引用,因此可以避免无限循环。 However, if you Clone books sometimes, this approach would fail.但是,如果您有时克隆书籍,这种方法会失败。

An even better way to solve this specific case would be to compare not the books, but their ISBN, since that is an unique identifier for a Book.解决此特定情况的更好方法不是比较书籍,而是比较它们的 ISBN,因为这是书籍的唯一标识符。

In general, it is better to avoid bidirectional dependencies like this.一般来说,最好避免像这样的双向依赖。 One way is to have one of the two classes implement an interface, so as not to use it directly.一种方法是让两个类之一实现一个接口,以免直接使用它。

From a modeling perspective, the chapter is part of the book.从建模的角度来看,这一章是本书的一部分。 So although you have references in both directions, the book is "stronger" than the chapter.因此,尽管您在两个方向都有参考,但本书比章节“更强大”。

When you have part-of relationships like with Book and Chapter, the part (Chapter) sometimes takes the whole (Book) into account when defining equals().当您与 Book 和 Chapter 有部分关系时,在定义 equals() 时,部分(Chapter)有时会考虑整个(Book)。 But not the other way round.但不是反过来。

So clearly, the book would not use its chapters to define equals().很明显,这本书不会使用它的章节来定义 equals()。 The chapter might use the book.本章可能会使用本书。 That depends on the model.这取决于 model。

You have to choose a Book field as a ID (like ISBN).您必须选择Book字段作为 ID(如 ISBN)。 Then, in Chapter equals, you can do a thing like然后,在Chapter equals 中,你可以做类似的事情

book.getISBN().equals(other.book.getISBN())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM