Joshua Bloch #Item 1:考虑静态工厂方法而不是构造函数 [英] Joshua Bloch #Item 1: Consider static factory methods instead of constructors

查看:91
本文介绍了Joshua Bloch #Item 1:考虑静态工厂方法而不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与从约书亚·布洛赫(Joshua Bloch)的《有效Java》一书中创建和销毁对象有关

This is related to creating and destroying objects from the book 'Effective Java' by Joshua Bloch

项目1:考虑静态工厂方法而不是构造方法

此方法将布尔基本值转换为布尔对象引用:

This method translates a boolean primitive value into a Boolean object reference:

public static Boolean valueOf(boolean b) {
    return b ? Boolean.TRUE : Boolean.FALSE;
}




请注意,静态工厂方法不是与设计模式中的工厂
方法模式相同[Gamma95,p。 107]。此项中描述的静态
工厂方法在
设计模式中没有直接等效项。

Note that a static factory method is not the same as the Factory Method pattern from Design Patterns [Gamma95, p. 107]. The static factory method described in this item has no direct equivalent in Design Patterns.

作者似乎谈论静态工厂方法和工厂方法模式之间的区别。究竟有什么区别?

The author seems to be talking about the the difference between Static Factory Method and Factory Method Pattern. What exactly is the difference here?

还有另外一件事, BalusC 在该线程中提及,在工厂方法下的链接, java。 util.Calendar#getInstance()是一种静态工厂方法,因此建议该静态工厂方法是Factory Method Pattern的子集。

Asa further matter, BalusC mentions in this thread, a link under Factory Method, java.util.Calendar#getInstance() which is a static factory method suggesting thereby that the static factory method is a subset of Factory Method Pattern.

推荐答案

工厂方法模式

工厂方法是用于创建对象的接口。此接口的具体实现指定要创建的具体对象。

A factory method is an interface for creating objects. A concrete implementation of this interface designates the concrete object to be created.

工厂方法模式是在客户端必须实例化对象但不知道如何创建对象时使用的。

The factory method pattern is uses when a client must instantiate an object, but it should not know how it is created.

  +------------+       uses        +-------------+
  |   Client   |    ---------->    |   Factory   |
  +------------+                   +-------------+
        | uses                     | create()    |
        V                          +-------------+
  +------------+                          ^
  | SomeObject |                          |
  +------------+                          |
        ^                                 |
        |                                 |
+--------------------+   create  +------------------+
| SomeConcreteObject | <-------- | ConcreateFactory |
+--------------------+           +------------------+

静态工厂方法

静态工厂方法模式是一种编写干净代码的方法。这是给构造函数一个更有意义的名称来表达其功能的方法。例如,

The static factory method pattern is a way to write clean code. It is a way to give a constructor a more meaningful name to express what it does. E.g.

List<String> newList = new ArrayList<String>(otherList);

上面的代码是什么意思?

What does the code above mean?

newList otherList 的副本还是 ArrayList 保留对 otherList 的引用,并仅委托它的调用(如包装程序)?

Is newList a copy of the otherList or does the ArrayList keeps a reference of the otherList and just delegates calls to it (like a wrapper)?

我想每个人知道上面的代码是做什么的,因为我们阅读了Javadoc。但是,如果使用静态工厂方法,则无需阅读javadoc,代码将更加清晰。例如,

I guess everyone knows what the code above does, because we read the javadoc. Nevertheless if static factory methods would have been used, the code whould be more clear without reading the javadoc. E.g.

List<String> copy = ArrayList.copyOf(otherList);
SortedSet<SomeObject> sortedSet = TreeSet.orderedBy(comparator);

使用静态工厂方法,也可以使用相同的参数列表编写多个构造函数,因为您可以给每个人起另一个名字。使用常规构造函数无法实现。例如,

With static factory methods it is also possible to write multiple 'constructors' with the same parameter list, because you can give everyone another name. This would not be possible using 'normal' constructors. E.g.

List<String> copy = ArrayList.copyOf(otherList);
List<String> delegateList = ArrayList.delegateOf(otherList);

这篇关于Joshua Bloch #Item 1:考虑静态工厂方法而不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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