乔舒亚-布洛赫的《有效的Java》:项目1-静态工厂方法[英] Effective Java By Joshua Bloch: Item1 - Static Factory Method

本文是小编为大家收集整理的关于乔舒亚-布洛赫的《有效的Java》:项目1-静态工厂方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在阅读Joshua Bloch的Effective Java,我对Item1 Static Factory Method有疑问.

Quote [Bloch,p.7]

接口不能具有静态方法, 因此,按照惯例,静态工厂 名为类型的接口的方法 被放在不可分割的班级中 命名类型.例如,爪哇 收藏框架,提供 不可变化的集合,同步 收藏等.近乎全部 这些实现是导出的 通过静态工厂方法 非可靠性类 (java.util.collections).课程 返回的对象都是 非公共.

好的.查看源代码时,我会看到java.util.Collection接口和java.util.Collections类带有私有构造函数(不可信性类).而且我看到,不可实现的类集合具有所有静态方法,就像布洛克所说的那样.但是我看不到两类之间的连接,正如布洛克所说的

接口不能具有静态方法,因此,按照惯例,将名为类型的接口的静态出厂方法放入了名为类型的非稳定类中.

  1. 有人可以指出我的明显吗?

  2. 他说

  3. 是什么意思

返回对象的类都是非公共

这是我获得Java消息来源的地方:推荐答案

  1. 接口不能具有静态方法,因此,按照惯例,将名为类型的接口的静态出厂方法放入了名为type types 的不可信性类中.

    重点只是" type [s]" 上的复数.因此,如果您的界面称为Foo,并且要创建一些称为MyFoo的实现,那么您的工厂具有实例化的方法,应由惯例称为Foos.

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

    这意味着从出厂方法返回的对象类具有私有或默认的可见性修饰符,如private class MyFoo{}中,因此无法通过任何其他方式实例化,而是其工厂方法.由于您无法使用new运算符构造一个对象,从私有内部或软件包私有类中构建对象(反射).

例如:

 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(...).它返回列表的某些实现.但是实施类的名称无关紧要.此外,所说的类是仅通过静态工厂方法构建的.

本文地址:https://www.itbaoku.cn/post/627743.html

问题描述

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.

  1. Can anyone point out the obvious to me?

  2. 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

推荐答案

  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 Foo and you want to create some implementation called MyFoo then your factory with the methods to instantiate should be called Foos by 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 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.