java 为什么工厂模式中的 getInstance() 方法应该是静态的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7393700/
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
Why should the getInstance() method in Factory pattern be static?
提问by rkg
In most of the factory pattern implementations, the getInstance
method is usuallydeclared as static. The main advantage of factory pattern is to hide the implementation details, but why does getInstance()
method needs to be static? Is instantiating a new Factory Object a bad practice?
在大多数工厂模式实现中,该getInstance
方法通常被声明为静态的。工厂模式的主要优点是隐藏了实现细节,但为什么getInstance()
方法需要是静态的?实例化一个新的工厂对象是一种不好的做法吗?
XYZFactory factory = new XYZFactory();
XYZObj obj = factory.getInstance(TYPE);
Vs
对比
XYZObj obj = XYZFactory.getInstance(TYPE);
采纳答案by cobaltduck
Daves answeris absolutely correct if the factory method is in the class itself. For a factory method in some other class, I think it is a matter of style. I would go back to the basic question of when is something static: does this method provide behavior to the class as a whole, or to specific instances of the class? I argue factory methods typically offer class-level behavior.
如果工厂方法在类本身中,Daves 的回答是绝对正确的。对于其他类中的工厂方法,我认为这是风格问题。我会回到什么时候是静态的基本问题:这个方法是为整个类提供行为,还是为类的特定实例提供行为?我认为工厂方法通常提供类级别的行为。
回答by DaveFar
A lot of factory methodsare used to offer an instance of the class itself, without the class exporting any constructors (see e.g. Josh Bloch item 1). If the factory method were an instance method, you wouldn't have an object of the class to start with.
许多工厂方法用于提供类本身的实例,而类无需导出任何构造函数(参见例如Josh Bloch 第 1 项)。如果工厂方法是一个实例方法,那么您将没有该类的对象作为开始。
Furthermore, getInstance()
is usually independent of any existing instance, so it should be declared static. If it depends on one, a prototype(i.e. clone()
) is often preferred.
此外,getInstance()
通常独立于任何现有实例,因此应将其声明为静态。如果它取决于一个,原型(即clone()
)通常是首选。
Finally, you should distinguish between factory method public static getInstance()
and an abstract factory, which is a class that hides implementation details often for several interfaces. You must, of course, be able to instantiate subclasses of the abstract factory. You can find a great introduction to creational patterns (Abstract Factory, Factory Method, Prototype, amongst others) in the classic Design Patterns book from the Gang of Four. It also gives an example of a non-static factory method intermixed with a prototype. So you see, many variants are possible...
最后,您应该区分工厂方法public static getInstance()
和抽象工厂,抽象工厂是一个经常隐藏多个接口的实现细节的类。当然,您必须能够实例化抽象工厂的子类。您可以在四人帮的经典设计模式一书中找到对创建模式(抽象工厂、工厂方法、原型等)的精彩介绍。它还给出了与原型混合的非静态工厂方法的示例。所以你看,很多变种都是可能的......
回答by cobaltduck
Because you use a factory to create objects. What you need is object instances not factory instances. So you usually look for the simplest, cleanest way of doing it.
因为您使用工厂来创建对象。您需要的是对象实例而不是工厂实例。因此,您通常会寻找最简单、最干净的方法。
It isn't useful to have more instances of the factory laying around. The factory instances will be useless after creating the object(s), so why create useless factory objects when just one long lived instance will do?
放置更多工厂实例是没有用的。创建对象后,工厂实例将无用,那么为什么只有一个长期存在的实例才能创建无用的工厂对象呢?
回答by Y.A.P.
Factory should give you instances of products, not instances of itself. It's rather like Singleton.
工厂应该给你产品的实例,而不是它自己的实例。这有点像辛格尔顿。