当前位置: 首页 > 知识库问答 >
问题:

使用生成器模式扩展类

龚钧
2023-03-14

我正试着把a类扩展成AX类。所以,我也扩展了abuilder。但是,虽然我能够使用以下方法创建类a的对象:

aBuilder f = new aBuilder();
f.bi = i;
f.bs = s;
a atry = f.withI(i).withS(s).build();

同样的方法对AX不起作用。当我尝试这样做时:

aXBuilder fb = new aXBuilder();
aX aXtry = fb.withI(i).withS(s).withB(b).build();
class a {

protected String s;
protected int i;

public void getdata() {
    System.out.println(this.s);
    System.out.println(this.i);
}

protected a(aBuilder fb) {
    this.s = fb.bs;
    this.i = fb.bi;
}

public static class aBuilder {
    public aBuilder() {
    }

    protected String bs;
    protected int bi;

    public aBuilder withS(String s) {
        this.bs = s;
        return this;
    }

    public aBuilder withI(Integer i) {
        this.bi = i;
        return this;
    }

    public a build() {
        return new a(this);
    }

}
protected Boolean b;

public void getData()
{
    System.out.println(this.s);
    System.out.println(this.i);
    System.out.println(this.b);
}

protected aX(aXBuilder axb) {
    super(axb);
    this.b = axb.bb;
}

public static class aXBuilder extends aBuilder {
    protected Boolean bb;

    public aXBuilder() {
    }

    public aXBuilder withB(Boolean b) {
        this.bb = b;
        return this;
    };

    public aX build() {
        return new aX(this);
    }
}

共有1个答案

汪坚
2023-03-14

您可以通过泛型来解决问题,尽管它确实需要创建一个抽象的超类。潜伏在这个站点教会了我,从一个具体的类继承被广泛认为是邪恶的。

public abstract class AbstractA {
    protected String s;
    protected int i;
    protected AbstractA() {
    }
    protected abstract static class ABuilder<T extends AbstractA, B extends ABuilder<T,B>> {
        protected T object;
        protected B thisObject;
        protected abstract T getObject(); //Each concrete implementing subclass overrides this so that T becomes an object of the concrete subclass
        protected abstract B thisObject(); //Each concrete implementing subclass builder overrides this for the same reason, but for B for the builder
        protected ABuilder() {
            object = getObject();
            thisObject = thisObject();
        }
        public B withS(String s) {
            object.s = s;
            return thisObject;
        }
        public B withI(int i) {
            object.i = i;
            return thisObject;
        }
        public T build() {
            return object;
        }
    }
}

一旦您拥有了抽象类,您只需根据需要多次扩展它,重写生成器中的抽象方法以返回所需的对象类型。

public final class ConcreteA extends AbstractA {
    private String foo;
    protected ConcreteA() {
    }
    public static final class Builder extends AbstractA.ABuilder<ConcreteA,Builder> {
        @Override protected ConcreteA getObject() {
            return new ConcreteA();
        }
        @Override protected Builder thisObject() {
            return this;
        }
        public Builder() {
        }
        public Builder withFoo(String foo) {
            object.foo = foo;
            return this;
        }
    }
}

然后...concretea baz=new concretea.builder().withfoo(“foo”).withs(“bar”).withi(0).build();

 类似资料:
  • 目录 1. ext_skel 2. PECL_Gen 3. 小结 毫无疑问你已经注意到,每个php扩展都包含一些非常公共的并且非常单调的结构和文件。当开始一个新扩展开发的时候,如果这些公共的结构已经存在, 我们只用考虑填充功 能代码是很有意义的. 为此, 在php中包含了一个简单但是很有用的shell脚本。

  • 所以,我需要在CMS中做一个扩展,称为TYPO3。这里有一些关于这个扩展的信息: “对于新的TYPO3 6.1网站,我们需要一个新闻稿扩展(基于ExtBase/Fluid),该扩展应包含: 标题/说明 目前实际上没有详细视图的设计,所以没有详细视图的链接。所以,现在,请专注于前端列表视图。" 我以前从未使用过这个CMS,所以我读了一下,发现我需要使用扩展生成器来创建这个扩展。所以,我已经下载并安装

  • 是否有可能在这个答案中提供等效的内容,但在TypeScript中? 对 Java 生成器类进行子类化 这是迄今为止我对基类的了解: 和扩展类: 正如另一个线程提到的,由于上下文的变化,我将无法按此顺序构建客户: 我目前正在尝试使用泛型来解决此问题,但是在为我的 setter 方法返回 this 指针时遇到问题(类型 this 不能分配给类型 T)。有什么想法吗?

  • 如何让“教授”延伸到“人”?我应该直接在代码上做吗?还是我错过了什么?

  • 问题 你需要准备一个复杂的、多部分的对象,你希望操作不止一次或有不同的配置。 解决方案 创建一个生成器封装对象的产生过程。 Todo.txt 格式提供了一个先进的但还是纯文本的方法来维护待办事项列表。手工输入每个项目有损耗且容易出错,然而 TodoTxtBuilder 类可以解决我们的麻烦: class TodoTxtBuilder constructor: (defaultParamet

  • 亦称: 建造者模式、Builder 意图 生成器模式是一种创建型设计模式, 使你能够分步骤创建复杂对象。 该模式允许你使用相同的创建代码生成不同类型和形式的对象。 问题 假设有这样一个复杂对象, 在对其进行构造时需要对诸多成员变量和嵌套对象进行繁复的初始化工作。 这些初始化代码通常深藏于一个包含众多参数且让人基本看不懂的构造函数中; 甚至还有更糟糕的情况, 那就是这些代码散落在客户端代码的多个位置