java 如何使用“静态工厂方法”而不是构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4029622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use "Static factory methods" instead of constructors?
提问by kal
Effective java says "Consider providing static factory methods instead of constructors"
有效的java说“考虑提供静态工厂方法而不是构造函数”
If you have a
如果你有一个
class A {
public static A getInstance() {
return new A();
}
}
Does it make sense to provide this method for class A , rather than call new A() in the code.
为类 A 提供此方法而不是在代码中调用 new A() 是否有意义。
回答by Marcelo Cantos
See herefor a nice exposition of the main reasons you might want to do this. In summary:
有关您可能想要这样做的主要原因的详细说明,请参见此处。总之:
- Named "constructors".
- Can return null, if appropriate.
- Can return an instance of a derived class, if appropriate.
- Reduce verbosity when instantiating variables of generic types.
- 命名为“构造函数”。
- 如果合适,可以返回 null。
- 如果合适,可以返回派生类的实例。
- 在实例化泛型类型的变量时减少冗长。
Another reason comes to mind that the article doesn't mention: Can implement interesting logic to avoid creating new objects all the time (caching based on parameters, recycling, etc.).
想到文章没有提到的另一个原因:可以实现有趣的逻辑,以避免一直创建新对象(基于参数的缓存,回收等)。
回答by Prine
For your example above it doesn't make much sense to provide a static constructor. And normally when you provide a static constructor you should make the normal constructor private. Otherwise it is still possible to create an instance with the normal constructor.
对于上面的示例,提供静态构造函数没有多大意义。通常,当您提供静态构造函数时,您应该将普通构造函数设为私有。否则,仍然可以使用普通构造函数创建实例。
I try to explain here with another example, when you could use static factory methods. One of the advantages is, that you can give the factory methods understandable names.
我试着用另一个例子来解释一下,当你可以使用静态工厂方法时。优点之一是,您可以为工厂方法提供易于理解的名称。
class Complex {
public static Complex fromCartesian(double real, double imag) {
return new Complex(real, imag);
}
public static Complex fromPolar(double modulus, double angle) {
return new Complex(modulus * cos(angle), modulus * sin(angle));
}
private Complex(double a, double b) {
//...
}
}
Complex c = Complex.fromPolar(1, pi)
Or another example would be the Singleton Pattern. There you wan't to provide only one instance. So this is the reason why you make the constructor private and create an own getInstance method where you make sure, that there is always just one instance available.
或者另一个例子是Singleton Pattern。你不想只提供一个实例。所以这就是为什么您将构造函数设为私有并创建自己的 getInstance 方法的原因,您可以确保始终只有一个实例可用。
public class Singleton {
private volatile static Singleton singleton;
private Singleton() {
}
// synchronized keyword has been removed from here
public static Singleton getSingleton() {
if(singleton==null) {
synchronized(Singleton.class){
if(singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
回答by bala singareddy
The Singleton pattern with static method calls are a better design if ur looking for a single instance.. because of the reentrant locking nature of static.. providing thread safety and also avoiding out-of-order writes..
如果您正在寻找单个实例,那么带有静态方法调用的单例模式是更好的设计......因为静态的可重入锁定特性......提供线程安全并避免无序写入......