[英]Effective Java By Joshua Bloch: Item1 - Static Factory Method
I am reading the Effective Java
by Joshua Bloch and I have question about Item1 Static Factory Method
. 我正在阅读Joshua Bloch撰写的
Effective Java
,我对Item1 Static Factory Method
有疑问。
Quote[Bloch, p.7] 引用[布洛赫,第7页]
Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types.
接口不能使用静态方法,因此按照惯例,名为Type的接口的静态工厂方法放在名为Types的不可实例化的类中。 For example, the Java Collections Framework, provide unmodifiable collections, synchronized collections, and the like.
例如,Java Collections Framework提供不可修改的集合,同步集合等。 Nearly all of these implementations are export via static factory methods in one noninstantiable class (java.util.Collections).
几乎所有这些实现都是通过一个不可实例化的类(java.util.Collections)中的静态工厂方法导出的。 The classes of the returned objects are all non-public.
返回对象的类都是非公共的。
Ok. 好。 When look at the sources code, I see
java.util.Collection
interface and java.util.Collections
class with private constructor (non-instantiable class). 查看源代码时,我看到
java.util.Collection
接口和带有私有构造函数的java.util.Collections
类(不可实例化的类)。 and I see that the non-instantiable class Collections has all static methods, just like what Bloch said. 而且我看到不可实例化的类Collections具有所有静态方法,就像Bloch所说的那样。 But i fail to see the connection between the two classes as Bloch said
但布洛赫说,我没有看到两个班级之间的联系
Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types. 接口不能使用静态方法,因此按照惯例,名为Type的接口的静态工厂方法放在名为Types的不可实例化的类中。
Can anyone point out the obvious to me? 任何人都可以向我指出明显的事吗?
what is it mean when he said 什么是他说的意思
The classes of the returned objects are all non-public 返回对象的类都是非公共的
Here is where I obtain the java sources: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collection.java?av=f 这是我获取java源代码的地方: http : //grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collection.java?av = f
Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types . 接口不能使用静态方法,因此按照惯例,名为Type的接口的静态工厂方法放在名为Types的不可实例化的类中 。
The point is just the plural 's' on "Type[s]" . 关键是“Type [s]”上的复数“s” 。 So if your interface is called
Foo
and you want to create some implementation called MyFoo
then your factory with the methods to instantiate should be called Foos
by convention. 因此,如果您的接口被称为
Foo
并且您想要创建一个名为MyFoo
实现,那么具有实例化方法的工厂应该按照惯例称为Foos
。
The classes of the returned objects are all non-public 返回对象的类都是非公共的
This means that the classes of objects returned from the factory methods have a private or default visibility modifier as in private class MyFoo{}
so that they can not be instantiated by any other means but their factory methods. 这意味着从工厂方法返回的对象类具有私有或默认可见性修饰符,如
private class MyFoo{}
因此除了工厂方法之外,它们不能通过任何其他方式实例化。 Since you can't construct an Object using the new
operator from private inner or package private class out of their scope (reflection aside). 因为你不能使用私有内部或包私有类中的
new
运算符来构造一个Object,而不是它们的范围(反射旁边)。
eg: 例如:
public interface Foo{ //interface without plural 's' (question 1)
public void bar();
}
public abstract class Foos(){ // abstract factory with plural 's' (question 1)
public static Foo createFoo(){
return new MyFoo();
}
private class MyFoo implements Foo{ // a non visible implementation (question 2)
public void bar(){}
}
}
Let's say you have an interface called List
and you want to use the static factory method to create different type of lists. 假设您有一个名为
List
的接口,并且您希望使用静态工厂方法来创建不同类型的列表。 You cannot define the static factory methods in the List
interface because it's an interface. 您无法在
List
接口中定义静态工厂方法,因为它是一个接口。 So what you have to do it have a class that return instances of classes that implement List 所以你要做的就是有一个返回实现List的类实例的类
public class ListFactory{
private ListFactory(){}
public static List makeArrayList(){...}
public static List makeLinkedList(){...}
public static List makeCrazyList(){...}
}
You cannot do this 你不能做这个
public interface List{
public static List makeArrayList();
public static List makeLinkedList();
public static List makeCrazyList();
}
Since List
is interface. 由于
List
是界面。
So take for example Collections.unmodifiableList(...) . 所以以Collections.unmodifiableList(...)为例。 It returns some implementation of List.
它返回List的一些实现。 But the implementation class's name is irrelevant.
但是实现类的名称是无关紧要的。 Furthermore, said class is only constructed through the static factory method.
此外,所述类仅通过静态工厂方法构造。
public interface Foo
static public class Factory
public Foo create(){..}
Foo foo = Foo.Factory.create();
1) I don't understand your question here. 1)我在这里不明白你的问题。
Collection
is the interface and Collections
has some factory methods like emptyList
Collection
是接口, Collections
有一些像emptyList
这样的工厂方法
2) Eg the List instance that is returned by Collection.emptyList
is an instance of a private class that implements the List
interface. 2)例如,
Collection.emptyList
返回的List实例是实现List
接口的私有类的实例。
它只是意味着Collections
(和其他类似的)中的静态工厂方法的返回类型是接口类型(例如List
)而不是特定的实现类(例如java.util.Collections.UnmodifiableList
),这些类对用户不可见因为这会使事情复杂化并增加API的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.