简体   繁体   English

有效的Java Item1-用于创建对象的静态工厂方法

[英]Effective Java Item1 - Static factory method for object creation

I was going through Effective java item 1, where "Static factory method vs constructors" for object creation is discussed. 我正在研究有效的Java项目1,其中讨论了用于对象创建的“静态工厂方法与构造函数”。 One of the disadvantages mentioned is the following: 提到的缺点之一如下:

"The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed." “仅提供静态工厂方法的主要缺点是没有公共或受保护的构造函数的类不能被子类化。”

It is also mentioned that this is good since it will promote object composition to inheritance. 还提到这是很好的,因为它将促进对象组成的继承。 However, is it not a serious limitation, when you indeed want inheritance? 但是,当您确实想要继承时,这不是一个严重的限制吗? Why should I prefer static factory methods for object creation, when I do not know from before if the class is going to be extended or not? 当我从以前不知道是否要扩展该类时,为什么我应该选择静态工厂方法来创建对象?

Why should I prefer static factory methods for object creation, when I do not know from before if the class is going to be extended or not? 当我从以前不知道是否要扩展该类时,为什么我应该选择静态工厂方法来创建对象?

The answer to this question is in Effective Java Item 17: Design and Document for Inheritance or else Prohibit It . 这个问题的答案在有效的Java项目17: 继承的设计和文档或禁止继承中 Designing a class for inheritance requires significantly more work, including the following. 设计用于继承的类需要大量的工作,包括以下内容。

  1. Documenting precisely the effects of overriding any method. 精确记录覆盖任何方法的效果。
  2. Providing hook methods. 提供挂钩方法。
  3. Testing subclasses (by implementing those classes yourself). 测试子类(通过自己实现这些类)。
  4. Restricting constructors to avoid all overridable methods. 限制构造函数以避免所有可重写的方法。
  5. Considering the Cloneable and Serializable interfaces, and their effects on inheritance. 考虑CloneableSerializable接口及其对继承的影响。

If you have done all of this work, then you will not provide only static factory methods. 如果您完成了所有这些工作,那么您将不会提供静态工厂方法。 You will also provide at least one public or protected constructor. 您还将提供至少一个公共或受保护的构造函数。

Effective Java goes into detail on each of these points, but the final advice is, 有效的Java在上述各点上都有详细说明,但最终建议是,

The best solution to this problem is to prohibit subclassing in classes that are not designed and documented to be safely subclassed. 解决此问题的最佳方法是禁止在未设计和记录为可安全子类化的类中进行子类化。

The quote is: 引用是:

The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed. 仅提供静态工厂方法的主要缺点是没有公共或受保护的构造函数的类不能被子类化。

Not of static factories, but of providing only static factory methods , feel the difference. 感觉不是与静态工厂有关,而是仅提供静态工厂方法

You have to design for extension, not for "well, maybe I am not quite sure now, but I'll leave it extensible just in case". 您必须为扩展而设计,而不是为“嗯,也许我现在不太确定,但为了以防万一,我会保留它的可扩展性”。

If your class is extensible then it'll need at least a public constructor. 如果您的类是可扩展的,则至少需要一个公共构造函数。 In this case you can provide only static factory methods. 在这种情况下,你只能提供静态工厂方法。 But I won't call it a serious limitation. 但我不会称其为严重限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM