当多个 classes 继承单个抽象 class 时使用构建器模式

Using a Builder Pattern when a multiple classes inherits a single abstract class

我正在使用两个实体构建 Web 应用程序:

  1. 漫画 - id、标题、发布日期、coverId、流派、作者、卷等
  2. 漫画数量 - id、标题、发布日期、封面 ID、isbn、页数等

这两本class继承自摘要class书。 Book class 将有两个 classes 的公共变量:id、title、releaseDate、coverID;

我正在使用 Pattern Builder 创建漫画和卷 objects。但是当我尝试设置描述时出现此错误:

        Manga manga = (Manga) new Manga.Builder().setID(id)
                                     .setTitle(title)
                                     .setCoverID(id)
                                     .setDescription() //Cannot resolve method 'setDescription' in 'Builder'
                                     .build();

图书class:

public 摘要 class Book {

public final Long id;
public final String title;

protected Book(final Builder<T> builder) {
    this.id = builder.id;
    this.title = builder.title;
}

public abstract static class Builder<T> {
    private Long id;
    private String title;

    public Builder<T> setID(Long id) {
        this.id = id;
        return this;
    }

    public Builder<T> setTitle(String title) {
        this.title = title;
        return this;
    }

    public abstract T build();
}

我的漫画class:

public class Manga extends Book<Manga> {

private final String description;

protected Manga(Builder builder) {
    super(builder);
    this.description = builder.description;
}

public static class Builder extends Book.Builder<Manga> {
    
    private String description;

    public Builder setDescription(String description) {
        this.description = description;
        return this;
    }

    @Override
    public Manga build() {
        return new Manga(this);
    }
}

或者有更好的解决方案吗?

谢谢。

这里应该有用:

package de.dwosch;

public abstract class Book {

    public final Long id;
    public final String title;

    protected Book(final Builder<?> builder) {
        this.id = builder.id;
        this.title = builder.title;
    }

    public abstract static class Builder<T extends Builder<T>> {
        private Long id;
        private String title;

        public T id(Long id) {
            this.id = id;
            return me();
        }

        public T title(String title) {
            this.title = title;
            return me();
        }

        public abstract T me();
        public abstract Book build();
    }
}

然后

package de.dwosch;

public class Manga extends Book {

    private final String description;

    protected Manga(Builder builder) {
        super(builder);
        this.description = builder.description;
    }

    public static class Builder extends Book.Builder<Builder> {

        private String description;

        public Builder description(String description) {
            this.description = description;
            return this;
        }

        @Override
        public Builder me() {
            return this;
        }

        @Override
        public Manga build() {
            return new Manga(this);
        }
    }
}

用法

    Manga manga = new Manga.Builder().description("test")
            .title("title")
            .build();

这个模式我已经实现过好几次了。约书亚·布洛赫 (Joshua Bloch) 在他的书《有效》Java(顺便说一句,很棒的书)中写到了这一点。

如果您需要这方面的更多信息,请告诉我。

顺便说一句:个人偏好 - setter 构建器流程中没有 set 的方法。看起来 'cleaner' :)