隐藏静态创建方法背后的构造函数? [英] Hiding a constructor behind a static creator method?

查看:178
本文介绍了隐藏静态创建方法背后的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了一种在 Google Guava Project Lombok :隐藏静态创建者方法后面的构造函数。这意味着你不是做新的HashBiMap(),而是做 HashBiMap.create()

I've recently discovered an interesting way to create a new instance of an object in Google Guava and Project Lombok: Hide a constructor behind a static creator method. This means that instead of doing new HashBiMap(), you do HashBiMap.create().

我的问题是为什么?隐藏构造函数有什么好处?对我来说,我认为这样做绝对没有优势,它似乎打破了基本的对象创建原则。由于开始使用 new Object()创建一个对象,而不是某些 Object.createMe()方法。这似乎是为了创建一个方法而创建一个方法。

My question is why? What advantage do you have of hiding the constructor? To me I see absolutely no advantage of doing this, and it seems to break basic object creation principles. Since the beggining you create an object with new Object(), not some Object.createMe() method. This almost seems like creating a method for the sake of creating a method.

这样做会得到什么?

推荐答案

您可能更喜欢静态工厂方法而不是公共构造函数。您可以阅读 Effective Java,Second Edition 中的第1项,以进行更长时间的讨论。

There are a number of reasons why you might prefer a static factory method instead of a public constructor. You can read Item 1 in Effective Java, Second Edition for a longer discussion.


  1. 它允许方法返回的对象类型与包含该方法的类的类型不同。实际上,返回的类型可以取决于参数。例如,如果emum类型的元素非常少,那么 EnumSet.of(E)将返回不同的类型,如果枚举类型包含许多元素(编辑:在这种特殊情况下,改进了enum没有很多元素的常见情况下的性能。)

  2. 它允许缓存。例如, Integer.valueOf(x)默认情况下会返回相同的对象实例,如果多次调用相同的值 x ,如果 x 介于-128和127之间。

  3. 它允许你有命名的构造函数(这可能很有用)如果你的类需要很多构造函数)。例如,请参阅 java.util.concurrent.Executors 中的方法。

  4. 它允许您创建概念上的API简单,但实际上非常强大。例如,集合中的静态方法隐藏了许多类型。他们可以创建许多公共类,而不是拥有许多静态方法的 Collections 类,但对于熟悉或记住该语言的新手来说,这将更难。

  5. 对于泛型类型,它可以限制您需要输入的内容。例如,而不是键入 List< String> strings = new ArrayList< String>()在Guava中你可以做 List< String> strings = Lists.newArrayList() newArrayList 方法是一种泛型方法,推断出泛型类型的类型。)

  1. It allows the type of the object returned by the method to be different than the type of the class that contains the method. In fact, the type returned can depend on the parameters. For example, EnumSet.of(E) will return a different type if the emum type has very few elements vs if the enum type has many elements ( in this particular case, improving performance for the common case where the enum doesn't have many elements)
  2. It allows caching. For instance, Integer.valueOf(x) will, by default, return the same object instance if called multiple times with the same value x, if x is between -128 and 127.
  3. It allows you to have named constructors (which can be useful if your class needs many constructors). See, for example, the methods in java.util.concurrent.Executors.
  4. It allows you to create an API that is conceptually simple but actually very powerful. For instance, the static methods in Collections hides many types. Instead of having a Collections class with many static methods, they could have created many public classes, but that would have been harder for someone new to the language to understand or remember.
  5. For generic types, it can limit how much typing you need to do. For example, instead of typing List<String> strings = new ArrayList<String>() in Guava you can do List<String> strings = Lists.newArrayList() (the newArrayList method is a generic method, and the type of the generic type is inferred).

对于 HashBiMap ,最后一个原因是最有可能的。

For HashBiMap, the last reason is the most likely.

这篇关于隐藏静态创建方法背后的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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