问题标签 [builder-pattern]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1263 浏览

java - Java 构建器模式 - 抽象构建器

首先,我对 Java 比较陌生,所以我所问的可能是微不足道的,但我在这里或其他地方找不到答案。

为简单起见,假设我有以下类层次结构:

我想为每个形状使用构建器模式。因此,我为它们中的每一个都添加了 Builders:

编辑: 这里使用泛型是为了允许以任何顺序使用 Builder 方法。例如,为了允许以下调用:

我的问题在于这个铸造:

我正在努力寻找一种方法来避免这种铸造。到目前为止,我发现的唯一方法是向 BaseBuilder 方法添加另一个抽象方法:

所以每个派生的构建器都必须实现它:

在我看来这有点矫枉过正,但我​​不想收到编译警告。

问题:有没有更优雅的方法来避免铸造?

0 投票
2 回答
80 浏览

c# - 数据构建器模式问题

我正在玩数据构建器模式,但我无法理解某些行为。我在下面写了我想要实现的简化版本

上面代码的使用

现在它将为我创建孩子,智能感知也显示我可以这样做 new DataBuilderChild().WithId(1).WithDescription(""); 但是,当我尝试构建它时,它失败了。我不明白为什么我无法访问此方法?它是公开的,并且可以通过智能感知看到。任何人都可以解释如何使它工作吗?谢谢

编辑

错误消息是:错误 1 ​​无法将类型 'TestProgram.Program.DataBuilderParent' 隐式转换为 'TestProgram.Program.MyChild' C:\Apps\TestProgram\Program.cs 44 29 TestProgram

0 投票
2 回答
4390 浏览

android - Android - 使用构建器模式创建片段?

我总是使用 newInstance() 模式创建片段并将参数传递给片段。这适用于一两个参数。我现在正在创建一个包含大约 10 个参数的片段,并且它们都是可选的。

我正在考虑使用 Builder 模式,类似于 AlertDialog 的工作方式。但是,我不确定实现这一点的最佳方法是什么,或者它是否是一个好主意。

这是我在想的一个例子,但有更多的变量。

这样做的问题是片段必须有一个默认构造函数,所以这不起作用。

有没有“正确”的方法来做到这一点?

0 投票
2 回答
1189 浏览

java - Java Builder Design Pattern Redundant Field Declaration in Class and its Builder

The classic Builder Pattern requires fields to be declared in the class-to-be-built and the exact same fields to be declared in the builder class. This can lead to problems when there are many fields and, during refactoring, the field types are not kept in sync. Here's an example of what I mean (I borrowed this code sample from an article by Joshua Block):

Now let's assume that servingSize needs to be changed from int to long and that this change is done in NutritonFacts but, by accident, not also done in the static Builder.

Admittedly less problematic is the number of fields. NutritionFacts has 6 fields and, therefore, so does Builder. What if there were 20 or 100 fields? Duplicating them all in NutritionFacts and Builder would be real pain. Is there a better way so that all the duplication and potential for type-syncing errors can be avoided?

0 投票
1 回答
1575 浏览

java - 如何使用构建器模式构建抽象类?

这是我的抽象类

然后我想使用这个抽象类,如下所示:

由于我无法实例化抽象类,因此构建器模式将不起作用。我也不知道任何替代模式可以做到这一点。

注意:我也不知道从这个基类扩展的类。

0 投票
0 回答
118 浏览

java - 默认构造函数在使用 Jersey Java 构建器模式构造的类中是否有任何优势

我正在尝试使用 Jersey Java 创建一个不可变对象。但我不知道默认构造函数在大型应用程序中是否有用。有人可以指导我吗?

这是我的课:-

默认构造函数也是如此:-

帮助序列化/反序列化或根本没有用。

我不知道它在大型应用程序中会有什么影响,或者我不应该全部使用它。

0 投票
2 回答
2302 浏览

java - 从 Yaml 文件加载 Java Builder 对象

我使用 Builder Pattern 创建了一个 Bean 类,并且在从 yaml 文件创建对象时遇到了问题。

这是一个示例类(实际类很大,如果您想用示例回答,这只是一个摘录):

我正在使用snakeyaml 加载文件,但任何yaml api 都可以。由于这displayName是一个强制性参数,我想在创建实例时传递该值。其他参数可以在创建对象时传递,但我希望选择通过 yaml 文件加载它们。

如果我使用 java bean,我可以加载 yaml 文件。有没有办法实例化构建器对象?

我试过:

但我收到以下错误:

0 投票
1 回答
88 浏览

c# - Writing a test data builder with syntax

Is it possible to write a TestDataBuilder which follows a given syntax?

So for example:

Carbuilder

I know how to write a basic builder for a car that's not the problem.

But how can I achieve, that I can only add new windows to a door?

So this is allowed:

But this is not allowed:

Is there a possibility to enforce this syntax-rule?

Can this be achieved by using an additional door builder which inherits car builder?

Should the car builder implement different interfaces like IDoorBuilderWithSyntax which defines the methods ICarBuilderWithSyntax WithWindow() and ICarBuilderWithSyntax HavingSide(); ICarBuilderWithSyntax HavingColor()?

0 投票
0 回答
70 浏览

json - 如何在 json 中使用嵌套构建器模式?

我有一个 JSON 对象:

我正在使用构建器模式来构建Address这个数组的对象。我怎样才能在这个结构中再添加一条街道(给定这个数组的不同对象中是唯一的。Addressname

注意:由于我使用的是构建器模式,因此我无法使用 getter 访问此对象的字段。

​</p>

0 投票
1 回答
911 浏览

java - 使用 Mockito 对流畅的界面进行单元测试

我想模拟构建器模式中使用的 DAO 接口,如下所示。但是,当我在下面运行测试时,它通过表明我的模拟对象永远不会被调用。我究竟做错了什么?