考虑添加静态接口方法以为对象创建提供单一入口点
Consider Adding Static Interface Methods to Provide a Single Entry Point for Object Creation
"implementation" below是指对象的实例吗?也许他们指的是实现代码。
Consider adding static interface methods, to allow the client code to
create (potentially specialized) objects that implement the interface.
For example, if we have an interface Point with two methods int x()
and int y(), then we can expose a static method Point.of(int x, int y)
that produces a (hidden) implementation of the interface.
So, if x and y are both zero, we can return a special implementation
class PointOrigoImpl (with no x or y fields), or else we return
another class PointImpl that holds the given x and y values. Ensure
that the implementation classes are in another package that are
clearly not a part of the API (e.g. put the Point interface in
com.company. product.shape and the implementations in
com.company.product.internal.shape).
Do This:
Point point = Point.of(1,2);
Don't Do This:
Point point = new PointImpl(1,2);
这样做的好处是强制封装?但是,如果实现是包私有的而不是 API 的一部分,那不会产生访问问题吗?如果是客户端,"world" 如下:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
无法访问 Point
的实现,那么它怎么可能像上面那样为 "do this" 实例化一个新的 Point
?
好吧 PointImpl
也会暴露 API 来创建自己,可能是以 PointImpl.create(int x, int y)
和 PointOrigoImpl.create()
的形式。由于它们都在内部包中,因此可以 public 和 Point.of
可以自由使用它们,而从技术上讲 "hidden" 来自 API 的用户,因为它们残留在 com.company.product.internal.shape
。
当然,这是出于善意,程序员不会滥用 PointImpl.create
而不是 Point.of
,但这是正常的。如果程序员想要破坏封装(或需要如此),无论如何都无法阻止他(应该如此)。
If the client, "world" doesn't have access to the implementation of Point then how can it possibly instantiate a new Point as above for "do this"?
客户端不需要实例化,因为实例化发生在 Point
本身。作为实现 implements
接口 Point
,Point
的所有可公开访问的方法都应该可供客户端访问,并且客户端始终可以自由使用这些方法。
For example, if we have an interface Point with two methods int x()
and int y(), then we can expose a static method Point.of(int x, int y)
that produces a (hidden) implementation of the interface.
1) 请注意,自 Java 8 以来,您可以在接口中添加静态方法,但在此之前您不能。
2) 我很难理解这个建议:
So, if x and y are both zero, we can return a special implementation
class PointOrigoImpl (with no x or y fields), or else we return
another class PointImpl that holds the given x and y values. Ensure
that the implementation classes are in another package that are
clearly not a part of the API (e.g. put the Point interface in
com.company. product.shape and the implementations in
com.company.product.internal.shape).
这是一个与您引用的文本匹配的示例 Point
class:
package com.company.product.shape;
import com.company.product.internal.shape.PointOrigoImpl;
import com.company.product.internal.shape.PointImpl;
public interface Point{
int x();
int y();
static Point of(int x, int y) {
if (x == 0 && y == 0){
return new PointOrigoImpl();
}
return new PointImpl(x, y);
}
}
如果 Point
接口提供给客户端并且它声明了工厂方法,为了编译正常,Point
接口必须对 PointOrigoImpl
和 PointImpl
classes 以便实例化它们。
由于 Point
接口和子 classes 在两个不同的包中(com.company.product.shape
和 com.company.product.internal.shape
),
这意味着 PointImpl
和 PointOrigoImpl
应该有一个 public 构造函数,否则 Point
无法实例化它们。
最后,操作 Points 的客户端也可以实例化 PointImpl
和 PointOrigoImpl
subclasses 的构造函数。
它违背了不想公开实现的工厂选择自己实现的目的return。
接口是 public 并且设计为完全可供客户端访问。
因此,作为一般规则,它不应包含我们不想向客户公开的实现。
3) 这里:
The advantage being that this enforces encapsulation? However, if the
implementation is package private and not part of the API, would that
not create access problems? If the client, "world" as here:
doesn't have access to the implementation of Point then how can it
possibly instantiate a new Point as above for "do this"?
我想你想知道为什么文字说:
确保实现 classes 位于另一个显然不属于 API.
的包中
在您提到的文本中,作者想让 Point
客户端知道该接口,但他不想让 Point
的子class 知道因为他要根据工厂Point中使用的参数来决定Point subclass到return
点客户端将只知道这个接口。
在 Java 中,为了不提供对实现 classes 的访问,您可以使用 public class、接口和内部私有 class :
- public class 暴露给客户调用工厂方法return实现
- 接口 class 众所周知,与实现没有耦合。
- 内部私有 class 构成 public class 的一部分。客户看不到它,但工厂看到它 return 合适的实现。
以你所举的例子,就是直截了当的。
点(接口):
public interface Point{
int x();
int y();
}
点工厂(public class 包含私有 classes):
public final class PointFactory {
// private classes
private class PointImpl implements Point {
private int x;
private int y;
private PointImpl(int x, int y) {
this.x = x;
this.y = y;
}
public int x() {
return x;
}
public int y() {
return y;
}
}
private class PointOrigoImpl implements Point {
public int x() {
return 0;
}
public int y() {
return 0;
}
}
// end private classes
public Point of(int x, int y) {
if (x == 0 && y == 0) {
return new PointOrigoImpl();
}
return new PointImpl(x, y);
}
}
"implementation" below是指对象的实例吗?也许他们指的是实现代码。
Consider adding static interface methods, to allow the client code to create (potentially specialized) objects that implement the interface. For example, if we have an interface Point with two methods int x() and int y(), then we can expose a static method Point.of(int x, int y) that produces a (hidden) implementation of the interface.
So, if x and y are both zero, we can return a special implementation class PointOrigoImpl (with no x or y fields), or else we return another class PointImpl that holds the given x and y values. Ensure that the implementation classes are in another package that are clearly not a part of the API (e.g. put the Point interface in com.company. product.shape and the implementations in com.company.product.internal.shape).
Do This:
Point point = Point.of(1,2);
Don't Do This:
Point point = new PointImpl(1,2);
这样做的好处是强制封装?但是,如果实现是包私有的而不是 API 的一部分,那不会产生访问问题吗?如果是客户端,"world" 如下:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
无法访问 Point
的实现,那么它怎么可能像上面那样为 "do this" 实例化一个新的 Point
?
好吧 PointImpl
也会暴露 API 来创建自己,可能是以 PointImpl.create(int x, int y)
和 PointOrigoImpl.create()
的形式。由于它们都在内部包中,因此可以 public 和 Point.of
可以自由使用它们,而从技术上讲 "hidden" 来自 API 的用户,因为它们残留在 com.company.product.internal.shape
。
当然,这是出于善意,程序员不会滥用 PointImpl.create
而不是 Point.of
,但这是正常的。如果程序员想要破坏封装(或需要如此),无论如何都无法阻止他(应该如此)。
If the client, "world" doesn't have access to the implementation of Point then how can it possibly instantiate a new Point as above for "do this"?
客户端不需要实例化,因为实例化发生在 Point
本身。作为实现 implements
接口 Point
,Point
的所有可公开访问的方法都应该可供客户端访问,并且客户端始终可以自由使用这些方法。
For example, if we have an interface Point with two methods int x() and int y(), then we can expose a static method Point.of(int x, int y) that produces a (hidden) implementation of the interface.
1) 请注意,自 Java 8 以来,您可以在接口中添加静态方法,但在此之前您不能。
2) 我很难理解这个建议:
So, if x and y are both zero, we can return a special implementation class PointOrigoImpl (with no x or y fields), or else we return another class PointImpl that holds the given x and y values. Ensure that the implementation classes are in another package that are clearly not a part of the API (e.g. put the Point interface in com.company. product.shape and the implementations in com.company.product.internal.shape).
这是一个与您引用的文本匹配的示例 Point
class:
package com.company.product.shape;
import com.company.product.internal.shape.PointOrigoImpl;
import com.company.product.internal.shape.PointImpl;
public interface Point{
int x();
int y();
static Point of(int x, int y) {
if (x == 0 && y == 0){
return new PointOrigoImpl();
}
return new PointImpl(x, y);
}
}
如果 Point
接口提供给客户端并且它声明了工厂方法,为了编译正常,Point
接口必须对 PointOrigoImpl
和 PointImpl
classes 以便实例化它们。
由于 Point
接口和子 classes 在两个不同的包中(com.company.product.shape
和 com.company.product.internal.shape
),
这意味着 PointImpl
和 PointOrigoImpl
应该有一个 public 构造函数,否则 Point
无法实例化它们。
最后,操作 Points 的客户端也可以实例化 PointImpl
和 PointOrigoImpl
subclasses 的构造函数。
它违背了不想公开实现的工厂选择自己实现的目的return。
接口是 public 并且设计为完全可供客户端访问。
因此,作为一般规则,它不应包含我们不想向客户公开的实现。
3) 这里:
The advantage being that this enforces encapsulation? However, if the implementation is package private and not part of the API, would that not create access problems? If the client, "world" as here: doesn't have access to the implementation of Point then how can it possibly instantiate a new Point as above for "do this"?
我想你想知道为什么文字说: 确保实现 classes 位于另一个显然不属于 API.
的包中在您提到的文本中,作者想让 Point
客户端知道该接口,但他不想让 Point
的子class 知道因为他要根据工厂Point中使用的参数来决定Point subclass到return
点客户端将只知道这个接口。
在 Java 中,为了不提供对实现 classes 的访问,您可以使用 public class、接口和内部私有 class :
- public class 暴露给客户调用工厂方法return实现
- 接口 class 众所周知,与实现没有耦合。
- 内部私有 class 构成 public class 的一部分。客户看不到它,但工厂看到它 return 合适的实现。
以你所举的例子,就是直截了当的。
点(接口):
public interface Point{
int x();
int y();
}
点工厂(public class 包含私有 classes):
public final class PointFactory {
// private classes
private class PointImpl implements Point {
private int x;
private int y;
private PointImpl(int x, int y) {
this.x = x;
this.y = y;
}
public int x() {
return x;
}
public int y() {
return y;
}
}
private class PointOrigoImpl implements Point {
public int x() {
return 0;
}
public int y() {
return 0;
}
}
// end private classes
public Point of(int x, int y) {
if (x == 0 && y == 0) {
return new PointOrigoImpl();
}
return new PointImpl(x, y);
}
}