什么构成了建造者模式?
What makes up a Builder Pattern?
你好,我有几个关于构建模式的问题?
- 为什么主class中的实例变量是私有的?
- 为什么内部 class 声明为静态的?
- 为什么内部class中的实例变量是private和重复的?
- 我们return方法中的构建器对象这个对象到底包含什么?
- 为什么主构造函数是私有的?
public class Plank {
//1. Why are these instance variables private?
private double widthInches;
private double heightInches;
private double thicknessInches;
//2. Why is this class static?
public static class Builder {
//Why are these instance variables private and repeated?
private double widthInches;
private double heightInches;
private double thicknessInches;
public Builder widthInches(double inches) {
widthInches = inches;
//3. What is returned here what object is this referencing?
return this;
}
public Builder heightInches(double inches) {
heightInches = inches;
return this;
}
public Builder thicknessInches(double inches) {
thicknessInches = inches;
return this;
}
public Plank build() {
return new Plank(this);
}
}
//4. Why is this constructor private?
private Plank(Builder build) {
widthInches = build.widthInches;
heightInches = build.heightInches;
thicknessInches = build.thicknessInches;
}
}
Why are the instance variables in the main class private?
这是 any class 实现的一个很好的默认值 - 不透露实现的内部细节。这不是构建器模式特有的。
Why is the inner class declared static??
因为您需要在外部 class 的实例创建之前创建构建器。
Why are the instance variables in the inner class private and
repeated?
参见关于 "private" 的第一个答案。至于 "repeated" - 不确定你的意思。
We return the Builder Object within the methods what exactly is
contained within this object?
我们 return 生成器对象,以便用户能够链接调用,即
builder.widthInches(3.0)
.heightInches(2.0)
...
Why is the main constructor private?
为了阻止从外部访问它 - 并强制用户使用生成器。
您可以read here了解有关构建器模式的更多信息。
首先,了解 builder pattern。
Why are the instance variables in the main class private?
因为实例变量应该是private
(或protected
)以防止不受信任的代码直接操作。
Why is the inner class declared static??
因为构建器需要在之前构建class,即外部class.
Why are the instance variables in the inner class private and repeated?
因为(见第一个答案)和(见第二个答案)。
We return the Builder Object within the methods what exactly is contained within this object?
从 setter 方法返回构建器对象允许 method chaining.
Why is the main constructor private?
因此 class 只能由构建器实例化。
- 为什么主class中的实例变量是私有的?
在 Java 中,最佳做法是始终将实例变量设为私有。这是一种封装形式。 class 负责它自己的字段,class 之外的任何代码都应该通过 public 方法(例如 Setters 和 Getters)间接访问这些字段。
- 为什么内部 class 声明为静态的?
有两种类型的嵌套classes:静态嵌套classes和内部classes。在这种情况下,您正在使用静态嵌套 class,因为您正在以静态方式访问它(即您还没有包含 class 的实例)。
- 为什么内部class中的实例变量是private和重复的?
出于与主要 class 的实例变量相同的原因,它们是私有的。
- 我们return方法中的构建器对象这个对象中到底包含什么?
关键字 this
引用当前对象实例,在本例中为 Builder
对象。返回当前实例允许调用者像
这样的链式调用
Builder builder = new Builder().widthInches(2.0).heightInches(3.0).thicknessInches(1.5);
- 为什么主构造函数是私有的?
强制调用代码使用Builder
是私有的。即使它是私有的,嵌套的 classes(例如 Builder#build
)也可以调用它。
Why are the instance variables in the main class private?
这遵循适当的封装。在这种情况下,Plank
的宽度、高度和厚度在创建 Plank
后永远不会改变。如果这些字段是 public,有人可能会无意中更改现有 Plank
.
的大小
一般来说,许多程序员宁愿通过 getter 和 setter 方法将事物设为私有并公开数据。这样,作为程序员,您可以保留对 classes 中变量的控制,并且可以清理数据并适当控制 read/write 访问。
Why is the inner class declared static?
Builder
实例不绑定到 Plank
(或任何其他对象)的特定实例。不仅如此,它 不能 绑定到特定实例,因为 Builder
正在尝试创建实例。
使其成为非静态的会产生一个有趣的情况,在这种情况下,您需要一个 Plank
来创建一个 Plank
。
Why are the instance variables in the inner class private and repeated?
如前一个答案所述,Builder
与 Plank
分开存在,并且在 Plank
实例之前创建。因此它需要拥有自己的字段副本,以便稍后创建木板。
同样,将它们保密允许 Builder
保留对这些变量的完全控制,确保 Builder
的用户必须与这些方法交互才能正确创建 Plank
.
We return the Builder Object within the methods what exactly is contained within this object?
返回的Builder
表示Builder
的当前状态。以下是一些示例:
// Calling plankBuilder1.build() would create a Plank with no width, height, or thickness.
Plank.Builder plankBuilder1 = new Plank.Builder();
// Calling plankBuilder2.build() would create a Plank with a height, but no width or thickness
Plank.Builder plankBuilder1 = new Plank.Builder().heightInches(1.0);
Why is the main constructor private?
Builder
模式旨在替换构造函数。程序员可能会觉得这是可取的原因有很多,但最终结果是您希望此 class 的使用者使用 Builder
来创建对象的实例。
如果构造函数是 public,那么消费者可能会尝试使用它而不是 Builder
。将其设为私有会迫使他们使用 Builder
.
我会按照列举的顺序尽量回答你的问题。
1。为什么主class中的实例变量是私有的?
主要 class 中的实例变量是私有的,以便 访问控制 对象的属性。在 Java 中,对象的大多数 字段 按照惯例是私有的,public 访问器(即 Type getField()
和 void setField(Type field)
)公开在必要时。
这是因为根据面向对象的原则,除了对象本身之外,所有行为的实现都应该从视图中隐藏起来。实际上,这会导致覆盖底层实现的能力(例如,如果您希望封装对象提供存储,或者如果您希望实现每次都需要数据库提取和更新而不是存储数据的本地副本。
2。为什么内部 class 声明为静态的?
Builder
class 是 Plank
class 的静态成员,因为它提供了创建 Plank
对象的唯一外部可访问方式.它紧密耦合了两个 classes,因为它们相互依赖。
重要提示:您的代码中有一个非常重要的拼写错误:Builder
class 应该是public
。
这样做的好处意味着您可以构造一个 Builder
并使用它一点一点地流畅地构造一个 Plank
,而不是将大量属性传递给构造函数。 Builder
是静态的意味着你可以这样做来接收带有默认参数的 Plank
:
Plank p = (new Plank.Builder()).build();
3。为什么里面的实例变量class是private的,重复的?
这些实例变量作为 模板 用于 Plank
对象。通过一个一个地配置这些,你可以在你的 Builder
中一点一点地构建一个 Plank
,然后扣动扳机并弹出一个新的 Plank
。类比是设置伐木机的参数,然后使用这些参数构建一个或多个相同的木板,并提供合理、可用的默认值。
这意味着您可以执行以下操作:
Plank.Builder builder = new Plank.Builder();
builder.widthInches(13);
builder.heightInches(2);
// Don't set the thickness; use the default defined in the Builder's constructor.
Plank p1 = builder.build();
Plank p2 = builder.build();
// Now set the thickness to a new value
builder.thicknessInches(14);
Plank p3 = builder.build();
4。我们 return 方法中的 Builder 对象 该对象中到底包含什么?
哦,这是我的最爱之一。这让我们可以执行所谓的 方法链接 ,这类似于字符串连接和数学运算在语言中的工作方式,但使用变量和函数调用而不是运算符和操作数。这让我们可以做这样的技巧:
Plank p = (new Plank.Builder())
.heightInches(13)
.thicknessInches(14)
.widthInches(12)
.build();
或者这个:
Plank.Builder builder = (new Plank.Builder())
.heightInches(13)
.thicknessInches(14)
.widthInches(12);
Plank p1 = builder.build();
Plank p2 = builder.build();
当与更好的方法名称结合使用时,这可以用来在大声朗读时听起来像口头命令,这是一种称为 "fluent interface" 的模式。 C# 的 LINQ 和 Enumerables 使用其中之一。
5。为什么主构造函数是私有的?
嗯,因为使用 Builder
更好!我们希望避免使用这些 classes 的开发人员使用丑陋的语法,因此我们将丑陋的部分隐藏起来并且无法访问,让手指远离使这种模式起作用的齿轮。
我们不想要的是在客户端代码中这样的东西:
Plank.Builder builder = new Plank.Builder().heightInches(13);
Plank p1 = new Plank(builder);
这比:
丑多了
Plank p1 = (new Plank.Builder()).heightInches(13).build();
增加
如果您想进一步清理它,可以将 Plank.Builder
的构造函数设为私有,并在 Plank
中提供以下方法:
public static Plank.Builder getBuilder() {
return new Plank.Builder();
}
那么,客户端代码将是:
Plank p1 = Plank.getBuilder()
.heightInches(13)
.build();
你好,我有几个关于构建模式的问题?
- 为什么主class中的实例变量是私有的?
- 为什么内部 class 声明为静态的?
- 为什么内部class中的实例变量是private和重复的?
- 我们return方法中的构建器对象这个对象到底包含什么?
- 为什么主构造函数是私有的?
public class Plank {
//1. Why are these instance variables private?
private double widthInches;
private double heightInches;
private double thicknessInches;
//2. Why is this class static?
public static class Builder {
//Why are these instance variables private and repeated?
private double widthInches;
private double heightInches;
private double thicknessInches;
public Builder widthInches(double inches) {
widthInches = inches;
//3. What is returned here what object is this referencing?
return this;
}
public Builder heightInches(double inches) {
heightInches = inches;
return this;
}
public Builder thicknessInches(double inches) {
thicknessInches = inches;
return this;
}
public Plank build() {
return new Plank(this);
}
}
//4. Why is this constructor private?
private Plank(Builder build) {
widthInches = build.widthInches;
heightInches = build.heightInches;
thicknessInches = build.thicknessInches;
}
}
Why are the instance variables in the main class private?
这是 any class 实现的一个很好的默认值 - 不透露实现的内部细节。这不是构建器模式特有的。
Why is the inner class declared static??
因为您需要在外部 class 的实例创建之前创建构建器。
Why are the instance variables in the inner class private and repeated?
参见关于 "private" 的第一个答案。至于 "repeated" - 不确定你的意思。
We return the Builder Object within the methods what exactly is contained within this object?
我们 return 生成器对象,以便用户能够链接调用,即
builder.widthInches(3.0)
.heightInches(2.0)
...
Why is the main constructor private?
为了阻止从外部访问它 - 并强制用户使用生成器。
您可以read here了解有关构建器模式的更多信息。
首先,了解 builder pattern。
Why are the instance variables in the main class private?
因为实例变量应该是private
(或protected
)以防止不受信任的代码直接操作。
Why is the inner class declared static??
因为构建器需要在之前构建class,即外部class.
Why are the instance variables in the inner class private and repeated?
因为(见第一个答案)和(见第二个答案)。
We return the Builder Object within the methods what exactly is contained within this object?
从 setter 方法返回构建器对象允许 method chaining.
Why is the main constructor private?
因此 class 只能由构建器实例化。
- 为什么主class中的实例变量是私有的?
在 Java 中,最佳做法是始终将实例变量设为私有。这是一种封装形式。 class 负责它自己的字段,class 之外的任何代码都应该通过 public 方法(例如 Setters 和 Getters)间接访问这些字段。
- 为什么内部 class 声明为静态的?
有两种类型的嵌套classes:静态嵌套classes和内部classes。在这种情况下,您正在使用静态嵌套 class,因为您正在以静态方式访问它(即您还没有包含 class 的实例)。
- 为什么内部class中的实例变量是private和重复的?
出于与主要 class 的实例变量相同的原因,它们是私有的。
- 我们return方法中的构建器对象这个对象中到底包含什么?
关键字 this
引用当前对象实例,在本例中为 Builder
对象。返回当前实例允许调用者像
Builder builder = new Builder().widthInches(2.0).heightInches(3.0).thicknessInches(1.5);
- 为什么主构造函数是私有的?
强制调用代码使用Builder
是私有的。即使它是私有的,嵌套的 classes(例如 Builder#build
)也可以调用它。
Why are the instance variables in the main class private?
这遵循适当的封装。在这种情况下,Plank
的宽度、高度和厚度在创建 Plank
后永远不会改变。如果这些字段是 public,有人可能会无意中更改现有 Plank
.
一般来说,许多程序员宁愿通过 getter 和 setter 方法将事物设为私有并公开数据。这样,作为程序员,您可以保留对 classes 中变量的控制,并且可以清理数据并适当控制 read/write 访问。
Why is the inner class declared static?
Builder
实例不绑定到 Plank
(或任何其他对象)的特定实例。不仅如此,它 不能 绑定到特定实例,因为 Builder
正在尝试创建实例。
使其成为非静态的会产生一个有趣的情况,在这种情况下,您需要一个 Plank
来创建一个 Plank
。
Why are the instance variables in the inner class private and repeated?
如前一个答案所述,Builder
与 Plank
分开存在,并且在 Plank
实例之前创建。因此它需要拥有自己的字段副本,以便稍后创建木板。
同样,将它们保密允许 Builder
保留对这些变量的完全控制,确保 Builder
的用户必须与这些方法交互才能正确创建 Plank
.
We return the Builder Object within the methods what exactly is contained within this object?
返回的Builder
表示Builder
的当前状态。以下是一些示例:
// Calling plankBuilder1.build() would create a Plank with no width, height, or thickness.
Plank.Builder plankBuilder1 = new Plank.Builder();
// Calling plankBuilder2.build() would create a Plank with a height, but no width or thickness
Plank.Builder plankBuilder1 = new Plank.Builder().heightInches(1.0);
Why is the main constructor private?
Builder
模式旨在替换构造函数。程序员可能会觉得这是可取的原因有很多,但最终结果是您希望此 class 的使用者使用 Builder
来创建对象的实例。
如果构造函数是 public,那么消费者可能会尝试使用它而不是 Builder
。将其设为私有会迫使他们使用 Builder
.
我会按照列举的顺序尽量回答你的问题。
1。为什么主class中的实例变量是私有的?
主要 class 中的实例变量是私有的,以便 访问控制 对象的属性。在 Java 中,对象的大多数 字段 按照惯例是私有的,public 访问器(即 Type getField()
和 void setField(Type field)
)公开在必要时。
这是因为根据面向对象的原则,除了对象本身之外,所有行为的实现都应该从视图中隐藏起来。实际上,这会导致覆盖底层实现的能力(例如,如果您希望封装对象提供存储,或者如果您希望实现每次都需要数据库提取和更新而不是存储数据的本地副本。
2。为什么内部 class 声明为静态的?
Builder
class 是 Plank
class 的静态成员,因为它提供了创建 Plank
对象的唯一外部可访问方式.它紧密耦合了两个 classes,因为它们相互依赖。
重要提示:您的代码中有一个非常重要的拼写错误:Builder
class 应该是public
。
这样做的好处意味着您可以构造一个 Builder
并使用它一点一点地流畅地构造一个 Plank
,而不是将大量属性传递给构造函数。 Builder
是静态的意味着你可以这样做来接收带有默认参数的 Plank
:
Plank p = (new Plank.Builder()).build();
3。为什么里面的实例变量class是private的,重复的?
这些实例变量作为 模板 用于 Plank
对象。通过一个一个地配置这些,你可以在你的 Builder
中一点一点地构建一个 Plank
,然后扣动扳机并弹出一个新的 Plank
。类比是设置伐木机的参数,然后使用这些参数构建一个或多个相同的木板,并提供合理、可用的默认值。
这意味着您可以执行以下操作:
Plank.Builder builder = new Plank.Builder();
builder.widthInches(13);
builder.heightInches(2);
// Don't set the thickness; use the default defined in the Builder's constructor.
Plank p1 = builder.build();
Plank p2 = builder.build();
// Now set the thickness to a new value
builder.thicknessInches(14);
Plank p3 = builder.build();
4。我们 return 方法中的 Builder 对象 该对象中到底包含什么?
哦,这是我的最爱之一。这让我们可以执行所谓的 方法链接 ,这类似于字符串连接和数学运算在语言中的工作方式,但使用变量和函数调用而不是运算符和操作数。这让我们可以做这样的技巧:
Plank p = (new Plank.Builder())
.heightInches(13)
.thicknessInches(14)
.widthInches(12)
.build();
或者这个:
Plank.Builder builder = (new Plank.Builder())
.heightInches(13)
.thicknessInches(14)
.widthInches(12);
Plank p1 = builder.build();
Plank p2 = builder.build();
当与更好的方法名称结合使用时,这可以用来在大声朗读时听起来像口头命令,这是一种称为 "fluent interface" 的模式。 C# 的 LINQ 和 Enumerables 使用其中之一。
5。为什么主构造函数是私有的?
嗯,因为使用 Builder
更好!我们希望避免使用这些 classes 的开发人员使用丑陋的语法,因此我们将丑陋的部分隐藏起来并且无法访问,让手指远离使这种模式起作用的齿轮。
我们不想要的是在客户端代码中这样的东西:
Plank.Builder builder = new Plank.Builder().heightInches(13);
Plank p1 = new Plank(builder);
这比:
丑多了Plank p1 = (new Plank.Builder()).heightInches(13).build();
增加
如果您想进一步清理它,可以将 Plank.Builder
的构造函数设为私有,并在 Plank
中提供以下方法:
public static Plank.Builder getBuilder() {
return new Plank.Builder();
}
那么,客户端代码将是:
Plank p1 = Plank.getBuilder()
.heightInches(13)
.build();