问题描述
我正在阅读Joshua Bloch的Effective Java,我对Item1 Static Factory Method有疑问.
Quote [Bloch,p.7]
接口不能具有静态方法, 因此,按照惯例,静态工厂 名为类型的接口的方法 被放在不可分割的班级中 命名类型.例如,爪哇 收藏框架,提供 不可变化的集合,同步 收藏等.近乎全部 这些实现是导出的 通过静态工厂方法 非可靠性类 (java.util.collections).课程 返回的对象都是 非公共.
好的.查看源代码时,我会看到java.util.Collection接口和java.util.Collections类带有私有构造函数(不可信性类).而且我看到,不可实现的类集合具有所有静态方法,就像布洛克所说的那样.但是我看不到两类之间的连接,正如布洛克所说的
接口不能具有静态方法,因此,按照惯例,将名为类型的接口的静态出厂方法放入了名为类型的非稳定类中.
-
有人可以指出我的明显吗?
-
他说
是什么意思
返回对象的类都是非公共
例如:
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(){} } }
其他推荐答案
假设您有一个称为List的接口,您想使用静态工厂方法来创建不同类型的列表.您无法在List接口中定义静态出厂方法,因为它是接口.因此,您必须做的是一个类,该类返回实现列表的类的类实例
public class ListFactory{ private ListFactory(){} public static List makeArrayList(){...} public static List makeLinkedList(){...} public static List makeCrazyList(){...} }
你不能做这个
public interface List{ public static List makeArrayList(); public static List makeLinkedList(); public static List makeCrazyList(); }
因为List是接口.
其他推荐答案
所以请例如 collections.unmodifiablestist(...).它返回列表的某些实现.但是实施类的名称无关紧要.此外,所说的类是仅通过静态工厂方法构建的.
问题描述
I am reading the Effective Java by Joshua Bloch and I have question about Item1 Static Factory Method.
Quote[Bloch, p.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. For example, the Java Collections Framework, provide unmodifiable collections, synchronized collections, and the like. Nearly all of these implementations are export via static factory methods in one noninstantiable class (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). and I see that the non-instantiable class Collections has all static methods, just like what Bloch said. 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.
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
推荐答案
Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types.
The point is just the plural 's' on "Type[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.
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. Since you can't construct an Object using the new operator from private inner or package private class out of their scope (reflection aside).
e.g.:
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. You cannot define the static factory methods in the List interface because it's an interface. So what you have to do it have a class that return instances of classes that implement 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.
其他推荐答案
So take for example Collections.unmodifiableList(...). It returns some implementation of List. But the implementation class's name is irrelevant. Furthermore, said class is only constructed through the static factory method.