有效的Java作者Joshua Bloch:Item1 - 静态工厂方法 [英] Effective Java By Joshua Bloch: Item1 - Static Factory Method

查看:110
本文介绍了有效的Java作者Joshua Bloch:Item1 - 静态工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Joshua Bloch的有效Java ,并且我对Item1 Static Factory Method 有疑问。

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

引用[Bloch,p.7]


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

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.

好的。当看到源代码时,我看到 java.util.Collection interface和 java.util.Collections class with private构造函数(不可实例化类)。我看到不可实例化的类Collections具有所有的静态方法,就像Bloch所说的那样。但是我没有看到两个类之间的连接,因为Bloch说

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

接口不能有静态方法,所以按惯例,名为Type的接口的静态工厂方法被放在不可实例化的类型中。


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

  1. Can anyone point out the obvious to me?

他说的是什么意思

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

这里是我获取java源的位置: http://grepcode.com/file/ repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collection.java?av=f

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. 接口不具有静态方法,因此按照惯例,名为Type的接口的静态工厂方法放在不可实例化的类中,名为Types < STRONG>。

  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.

这一点只是类型[s]上的复数'''。因此,如果您的界面被称为 Foo ,并且您想要创建一些名为 MyFoo 的实现,那么您的工厂将使用实例化方法应该按照惯例称为 Foos

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.

返回的对象的类都是非-public

这意味着从工厂方法返回的对象类具有私有或默认可见性修饰符,如 private class MyFoo {} ,以便它们不能以任何其他方式实例化,但不能以工厂方式实例化。因为您不能使用私有内部或包私有类的运算符构造对象(反射在旁边)。

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

例如:

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

这篇关于有效的Java作者Joshua Bloch:Item1 - 静态工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆