Is it forbidden to use static methods in factory pattern?

♀尐吖头ヾ 提交于 2021-02-02 09:55:38

问题


I got told that using static methods when implementing the factory-method-pattern is wrong and should be avoided. Because I wasn't really familiar with the pattern I accepted that answer.

After reading articles and getting deeper into it, I couldn't find any source which supports this statement.

Can someone help me out with this situation. Should I avoid the static-keyword in factory-methods and if so, when are they useful?


回答1:


The first thing to be 100% clear about is that there is no single "Factory Pattern". The term Factory describes a category of patterns with wildly differing implementations. When someone says Factory Pattern they could be talking about any creational pattern; but most commonly the phrase Factory Pattern indicates ignorance of design patterns.

Two very specific Factory Patterns are published in the famous Gang of Four book: Abstract Factory and Factory Method.

Static Factory was popularized by Joshua Bloch in Effective Java. There is no relationship between Bloch's Static Factory Method and the GoF's Factory Method, other than having similar names.

I will adjust the original question then, to provide a clear answer.

Is it forbidden to use static methods in the GoF Factory Method pattern?

Yes. The GoF pattern requires inheritance, which is not supported by static methods. This does not make the Static Factory pattern less useful! Static Factory is a perfectly legitimate design pattern that happens to be implemented in a very different way from the GoF Factory Method pattern. You may use both patterns when appropriate; and now you can label them appropriately as well.




回答2:


An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass.

source: https://sourcemaking.com/design_patterns/factory_method

As far as I know, Its ok to use static on factory method. But @Serge Ballesta* has a good point in the comment.




回答3:


Sometimes the implementation class doesn't matter, because the interface specifies all of the relevant behavior. In these cases, callers don't care how it's implemented, and a static factory method is OK. Collections.emptyList() or singletonSet(item), for example, are fine.

Sometimes, though, it is not necessarily true that the same implementation will suffice for all callers. This is the usual case, even if special implementations are only needed for tests or staging environments. In these cases the proper functioning of the callers depends on the implementation choice, and a static factory method is a violation of the dependency inversion principle.

If the same implementation isn't necessarily acceptable for all uses, then you should inject a non-static factory so that the callers aren't coupled to the particular implementation choice.



来源:https://stackoverflow.com/questions/60276277/is-it-forbidden-to-use-static-methods-in-factory-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!