简体   繁体   English

有效的Java作者:Joshua Bloch:第1项 - 静态工厂方法

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

  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 这是我获取java源代码的地方: 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 . 接口不能使用静态方法,因此按照惯例,名为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

  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. 这意味着从工厂方法返回的对象类具有私有或默认可见性修饰符,如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.

相关问题 有效的Java Item1-用于创建对象的静态工厂方法 - Effective Java Item1 - Static factory method for object creation 约书亚布洛赫的有效Java中的等价方法 - Equals method in Joshua Bloch's Effective Java Joshua Bloch #Item 1:考虑静态工厂方法而不是构造函数 - Joshua Bloch #Item 1: Consider static factory methods instead of constructors Enum类型,如Joshua Bloch在Effective Java中所述 - Enum Types as explained in Effective Java by Joshua Bloch 为什么Joshua Bloch在有效的java中减少pop方法中堆栈的“大小”值? - why did Joshua Bloch decrement the “size” value of stack in the pop method in effective java? 什么是 Java 中的不可变值类? (Joshua Bloch 的有效 Java) - What is an immutable value class in Java? (Effective Java by Joshua Bloch) 为什么 Joshua Bloch 在 Effective Java 中使用 2*size + 1 来调整堆栈大小? - Why did Joshua Bloch use 2*size + 1 for resizing the stack in Effective Java? Joshua Bloch在有效的java中建议如何在Java中使用缓存哈希码? - how caching hashcode works in Java as suggested by Joshua Bloch in effective java? 为什么在Joshua Bloch Effective Java示例中,双重检查锁定的速度提高了25% - Why Double checked locking is 25% faster in Joshua Bloch Effective Java Example 什么是用于equals()方法的字段的“规范表示”(Joshua Bloch) - What is a 'canonical representation' of a field meant to be for equals() method (Joshua Bloch)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM