Java流畅的构建器和继承

时间:2016-07-19 10:24:26

标签: java generics inheritance fluent-interface

再多一天,还有一次与仿制药的斗争。

我有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>构造函数调用所杀死。有人能指出我解决这个问题的最佳做法吗?

1 个答案:

答案 0 :(得分:1)

好的,为了正确设置,你应该做一些改变:

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();
    }
}