与非专利药又一天又一天的挣扎。

我用以下继承树设置了Control对象:

BaseControl
|_SimpleControl
  |_MultipleControl
    |_AutocompleteControl
    |_SelectControl


对于此树中的每个非抽象对象,我想提供构建器,以便可以轻松创建这些对象。这是我到目前为止的内容:

BaseControlBuilder:

public abstract class BaseControlBuilder<C extends BaseControl, B extends BaseControlBuilder<C, B>> {
    protected C control;
    private B builder;

    BaseControlBuilder() {
        control = createObj();
        builder = getThis();
    }
    public C build() { return control; }

    protected abstract C createObj();
    protected abstract B getThis();
}


SimpleControlBuilder:

public class SimpleControlBuilder<C extends SimpleControl, B extends SimpleControlBuilder<C, B>>
        extends BaseControlBuilder<SimpleControl, SimpleControlBuilder<C, B>> {

    public SimpleControlBuilder(final String id, final String caption,
            final InputType controlType) {
        super();
        control.setId(id);
        control.setCaption(caption);
        control.setType(controlType);
    }

    public SimpleControlBuilder(final InputType controlType) {
        this("", "", controlType);
    }

    public SimpleControlBuilder(final Enum<?> en, final InputType controlType) {
        this(en.name(), en.toString(), controlType);
    }

    public SimpleControlBuilder<C, B> disabled() {
        control.setDisabled(true);
        return this;
    }

    @Override
    protected SimpleControl createObj() {
        return new SimpleControl();
    }

    @Override
    protected SimpleControlBuilder<C, B> getThis() {
        return this;
    }
}


MultipleControlBuilder:

abstract class MultipleControlBuilder<C extends MultipleControl, B extends MultipleControlBuilder<C, B>>
        extends SimpleControlBuilder<MultipleControl, MultipleControlBuilder<C, B>> {

    MultipleControlBuilder(final InputType type) {
        super(type);
    }

    MultipleControlBuilder(final String id, final String caption,
            final InputType type) {
        super(id, caption, type);
    }

    MultipleControlBuilder(final Enum<?> en, final InputType type) {
        super(en, type);
    }

    public MultipleControlBuilder<C, B> multiple() {
        ((MultipleControl) control).setMultiple(true);
        return this;
    }
}


AutocompleteControlBuilder:

public class AutocompleteControlBuilder<C extends AutocompleteControl, B extends AutocompleteControlBuilder<C, B>>
    extends MultipleControlBuilder<AutocompleteControl, AutocompleteControlBuilder<C, B>> {

    public AutocompleteControlBuilder(final String url,
            final AutocompleteType autocompleteType) {
        this("", "", url, autocompleteType);
    }

    public AutocompleteControlBuilder(final String id,
            final String caption, final String url,
            final AutocompleteType autocompleteType) {
        super(id, caption, InputType.AUTOCOMPLETE);
        ((AutocompleteControl) control).setAutocompleteUrl(url);
        ((AutocompleteControl) control).setAutocompleteType(autocompleteType);
    }

    public AutocompleteControlBuilder(final Enum<?> en, final String url,
            final AutocompleteType autocompleteType) {
        this(en.name(), en.toString(), url, autocompleteType);
    }

    @Override
    protected AutocompleteControl createObj() {
        return new AutocompleteControl();
    }

    @Override
    protected AutocompleteControlBuilder<C, B> getThis() {
        return this;
    }
}


但是令人惊讶的是,我得到了一些意想不到的结果。
例如,在下面的代码中,我必须将control转换为MultipleControl来调用setter,尽管事实上C extends MultipleControl ...

此外,以下build()方法调用:new AutocompleteControlBuilder<AutocompleteControl, AutocompleteControlBuilder>("url", AutocompleteType.STANDARD).build());返回SimpleControl而不是AutocompleteControl,这没有意义,因为我显式提供了类型参数。

最后一根稻草是,我试图实现的简洁明了的代码被难看的new AutocompleteControlBuilder<AutocompleteControl, AutocompleteControlBuilder>构造函数调用杀死了。有人可以指出我解决该问题的最佳做法吗?

最佳答案

好的,为了正确设置它,您应该进行一些更改:

public class SimpleControlBuilder<C extends SimpleControl, B extends SimpleControlBuilder<C, B>>
        extends BaseControlBuilder<SimpleControl, SimpleControlBuilder<C, B>> { // this should extend with the extension classes

    public SimpleControlBuilder(final String id, final String caption,
            final InputType controlType) {
        super();
        control.setId(id);
        control.setCaption(caption);
        control.setType(controlType);
    }

    public SimpleControlBuilder(final InputType controlType) {
        this("", "", controlType);
    }

    public SimpleControlBuilder(final Enum<?> en, final InputType controlType) {
        this(en.name(), en.toString(), controlType);
    }

    public SimpleControlBuilder<C, B> disabled() { // this should return B
        control.setDisabled(true);
        return this;
    }

    @Override
    protected SimpleControl createObj() { // this should return C
        return new SimpleControl();
    }

    @Override
    protected SimpleControlBuilder<C, B> getThis() { // this should return B
        return this;
    }
}


所以这意味着

public abstract class SimpleControlBuilder<C extends SimpleControl, B extends SimpleControlBuilder<C, B>>
        extends BaseControlBuilder<C, B> {

    public SimpleControlBuilder(final String id, final String caption,
            final InputType controlType) {
        super();
        control.setId(id);
        control.setCaption(caption);
        control.setType(controlType);
    }

    public SimpleControlBuilder(final InputType controlType) {
        this("", "", controlType);
    }

    public SimpleControlBuilder(final Enum<?> en, final InputType controlType) {
        this(en.name(), en.toString(), controlType);
    }

    public B disabled() {
        control.setDisabled(true);
        return getThis();
    }
}




abstract class MultipleControlBuilder<C extends MultipleControl, B extends MultipleControlBuilder<C, B>>
        extends SimpleControlBuilder<C, B> {

    MultipleControlBuilder(final InputType type) {
        super(type);
    }

    MultipleControlBuilder(final String id, final String caption,
            final InputType type) {
        super(id, caption, type);
    }

    MultipleControlBuilder(final Enum<?> en, final InputType type) {
        super(en, type);
    }

    public B multiple() {
        control.setMultiple(true);
        return getThis();
    }
}




public abstract class AutocompleteControlBuilder<C extends AutocompleteControl, B extends AutocompleteControlBuilder<C, B>>
    extends MultipleControlBuilder<C, B>> {

    public AutocompleteControlBuilder(final String url,
            final AutocompleteType autocompleteType) {
        this("", "", url, autocompleteType);
    }

    public AutocompleteControlBuilder(final String id,
            final String caption, final String url,
            final AutocompleteType autocompleteType) {
        super(id, caption, InputType.AUTOCOMPLETE);
        control.setAutocompleteUrl(url);
        control.setAutocompleteType(autocompleteType);
    }

    public AutocompleteControlBuilder(final Enum<?> en, final String url,
            final AutocompleteType autocompleteType) {
        this(en.name(), en.toString(), url, autocompleteType);
    }
}


如果MultipleControl extends SimpleControlAutocompleteControl extends MultipleControl有效,并且您具有SimpleControl的具体扩展名,这些扩展名可以使用带有具体化参数的getThis()返回。

public class SomeControlBuilder extends MultipleControlBuilder<SomeControl, SomeControlBuilder> {
    public SomeControlBuilder(final InputType type) {
        super(type);
    }

    public SomeControlBuilder(final String id, final String caption,
            final InputType type) {
        super(id, caption, type);
    }

    public SomeControlBuilder(final Enum<?> en, final InputType type) {
        super(en, type);
    }

    @Override
    protected SomeControlBuilder getThis() {
        return this;
    }

    @Override
    protected SomeControl createObj() {
        return new SomeControl();
    }
}

10-08 16:41