0

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?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Michael Holley
  • 119
  • 1
  • 12
  • 1
    Unsure whether this is really on-topic here, but it looks like an interesting question. Static method are harder to test and specially to mock, and it is often better (on a conception point of view) to setup an auxilliary class and put factory methods there. That way if will be possible to mock the factory in test classes. That being said there plenty of examples of static factory method, and plenty of use cases where that kind of design is totally acceptable. – Serge Ballesta Feb 18 '20 at 08:12

3 Answers3

1

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.

Sepehr GH
  • 1,297
  • 1
  • 17
  • 39
1

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.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
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.

jaco0646
  • 15,303
  • 7
  • 59
  • 83