由 Joshua Bloch 编写的 Effective Java:Item1 - 静态工厂方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6129026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:27:52  来源:igfitidea点击:

Effective Java By Joshua Bloch: Item1 - Static Factory Method

javadesign-patternsfactoryeffective-java

提问by Thang Pham

I am reading the Effective Javaby Joshua Bloch and I have question about Item1 Static Factory Method.

我正在阅读Effective JavaJoshua Bloch 的文章,我对 Item1 有疑问Static Factory Method

Quote[Bloch, p.7]

引用 [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.

接口不能有静态方法,因此按照惯例,名为 Type 的接口的静态工厂方法放在名为 Types 的不可实例化的类中。例如,Java Collections Framework,提供不可修改的集合、同步的集合等。几乎所有这些实现都是通过一个不可实例化的类 (java.util.Collections) 中的静态工厂方法导出的。返回对象的类都是非公开的。

Ok. When look at the sources code, I see java.util.Collectioninterface and java.util.Collectionsclass 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

行。查看源代码时,我看到带有私有构造函数(不可实例化类)的java.util.Collection接口和java.util.Collections类。而且我看到不可实例化的类 Collections 都有所有静态方法,就像 Bloch 所说的那样。但我没有看到 Bloch 所说的两个类之间的联系

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 的不可实例化的类中。

  1. Can anyone point out the obvious to me?

  2. what is it mean when he said

  1. 任何人都可以向我指出显而易见的事情吗?

  2. 他说的时候是什么意思

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

回答by fasseg

  1. 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 Fooand you want to create some implementation called MyFoothen your factory with the methods to instantiate should be called Foosby convention.

  2. 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 newoperator from private inner or package private class out of their scope (reflection aside).

  1. 接口不能有静态方法,因此按照惯例,名为 Type 的接口的静态工厂方法放在名为 Types 的不可实例化的类中

    重点只是"Type[s]" 上复数 's'。因此,如果您的接口被调用Foo并且您想要创建一些被调用的实现,MyFoo那么您的工厂应该按照Foos约定调用具有实例化方法的工厂。

  2. 返回对象的类都是非公开的

    这意味着从工厂方法返回的对象类具有私有或默认可见性修饰符,private class MyFoo{}因此它们不能通过除工厂方法之外的任何其他方式实例化。因为您不能使用new来自私有内部或包私有类的操作符来构造一个 Object超出其范围(反射除外)。

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(){}
    }
 }

回答by Alvin

Let's say you have an interface called Listand you want to use the static factory method to create different type of lists. You cannot define the static factory methods in the Listinterface because it's an interface. So what you have to do it have a class that return instances of classes that implement List

假设您调用了一个接口,List并且您想使用静态工厂方法来创建不同类型的列表。您不能在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 Listis interface.

由于List是接口。

回答by irreputable

public interface Foo
    static public class Factory
        public Foo create(){..}

Foo foo = Foo.Factory.create();

回答by Dilum Ranatunga

So take for example Collections.unmodifiableList(...). It returns some implementation of List. But the implementation class's name is irrelevant. Furthermore, said class is onlyconstructed through the static factory method.

Collections.unmodifiableList(...)为例。它返回 List 的一些实现。但是实现类的名称无关紧要。此外,该类通过静态工厂方法构造。

回答by Kaj

1) I don't understand your question here. Collectionis the interface and Collectionshas some factory methods like emptyList

1)我不明白你的问题。Collection是接口并且Collections有一些工厂方法,比如emptyList

2) E.g the List instance that is returned by Collection.emptyListis an instance of a private class that implements the Listinterface.

2) 例如,返回的 List 实例是Collection.emptyList实现该List接口的私有类的实例。

回答by ColinD

It simply means that the return type of the static factory methods in Collections(and other classes like it) are interface types (e.g. List) rather than specific implementation classes (e.g. java.util.Collections.UnmodifiableList), which are not visible to users since that would just complicate things and increase the size of the API.

它只是意味着Collections(和其他类似的类)中的静态工厂方法的返回类型是接口类型(例如List)而不是特定的实现类(例如java.util.Collections.UnmodifiableList),这对用户是不可见的,因为这只会使事情复杂化并增加API 的大小。