本文介绍了为什么Java Pattern类使用工厂方法而不是构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在中对此进行了很好的讨论。

There's a good discussion of this in the general case.

但是,我特别想知道为什么类使用静态方法来创建一个对象,而不是构造函数?

However, I was wondering specifically why the Pattern class uses the compile static method to create an object, rather than the constructor?

我觉得使用构造函数更直观。

Seems to me to be more intuitive to use a constructor.

推荐答案

Pattern类比JDK中的许多东西都要新。因此,我相信他们采用了更现代的工厂方法,而不是公共构造函数的旧方法。

The Pattern class is newer than a lot of the stuff in the JDK. As such I believe they adopted the more modern approach of using factory methods rather than the older approach of public constructors. You can't really retrofit factory methods to existing classes.

通常来说,没有太多理由在工厂方法上使用构造函数,所以我认为这就是全部对它。工厂方法允许您抽象对象的创建,这可能非常有用。

Generally speaking, there's not a lot of reason to use a constructor over a factory method so I think that's all there was to it. Factory methods allow you to abstract object creation, which can be pretty useful.

这篇关于为什么Java Pattern类使用工厂方法而不是构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:41