1

Refactoring Guru provides a good example for factory method.

In that example there are Common product interface, Concrete products, Base creator and Concrete creator. But in Calendar, I find only Common product interface (Calendar class) and Concrete products ( GregorianCalendar, JapaneseImperialCalendar etc. which extends Calendar).

We call Calendar.getInstance() and we get instance of Calendar. My expectation was that we call a concrete creator and get an instance of concrete product.

How is Calendar an example for factory method?

J.F.
  • 13,927
  • 9
  • 27
  • 65
user344563
  • 63
  • 6
  • 1
    `Calendar.getInstance()` is a factory method – Mark Rotteveel Nov 09 '20 at 15:33
  • 2
    First, I wouldn't use `Calendar` as an example of anything. Second, there is a difference between "Factory method" and "Factory design pattern". – RealSkeptic Nov 09 '20 at 15:34
  • @MarkRotteveel thank you for your comment. I understand that `Calendar.getInstance()` is a factory method. My understanding about factory method design pattern is that factory method is within a Base Creator class and concrete creators can override this method. And they will return concrete products. But here `Calendar.getInstance()` returns Calendar object itself. That is what confusing me. – user344563 Nov 09 '20 at 15:59
  • @RealSkeptic thank you for your comment. could you please elaborate? I have read in multiple places that Calendar.getInstance() is an example for factory method design pattern. – user344563 Nov 09 '20 at 16:02
  • 1
    `Calendar.getInstance()` will return an instance of `GregorianCalendar` for most locales, and for some locales `BuddhistCalendar` or `JapaneseImperialCalendar` (or possibly a specialty type defined through a calendar provider). That is what makes it a factory method. You don't call a constructor of a concrete type, but instead let the base type figure out which type is the right type to construct. – Mark Rotteveel Nov 09 '20 at 16:02
  • @MarkRotteveel thank you once again. I understand that. But only 2 classes are involved there. An abstract class and a concrete product. Usually all the explanations about abstract factor method design pattern mentions about 4 classes - Common product interface, Concrete products, Base creator and Concrete creator. Its the same here too [Refactoring Guru](https://refactoring.guru/design-patterns/factory-method/java/example). That is confusing me. – user344563 Nov 09 '20 at 16:14
  • 2
    Ah now I see what you mean. They are two different patterns. What `Calendar` does is a static factory method, which is similar, but not identical to the factory method pattern. – Mark Rotteveel Nov 09 '20 at 16:29
  • @MarkRotteveel Thank you again. So, does that means `Calendar` is not an example for factory method design pattern and it is an example for static factory method design pattern? – user344563 Nov 09 '20 at 16:36
  • I think that definition doesn't work for Java. In Java, you can't override a static method - only an instance method. But for an instance method, you need an instance. So how do you create an instance? Using `new`? That defeats the purpose. So the idea of overriding a factory method is incorrect for Java. – RealSkeptic Nov 09 '20 at 16:46
  • @RealSkeptic In the example [Refactoring Guru](https://refactoring.guru/design-patterns/factory-method/java/example), createButton() method in Dialog.java(Base creator) is factory method and abstract and it is overridden by WindowsDialog.java(Concrete creator) which instantiates WindowsButton (Concrete product). Button is the Common product interface. Having different Concrete creators, we can have different concrete products with same Common product interface. – user344563 Nov 09 '20 at 17:00

1 Answers1

2

To sum up the comments, Calendar.getInstance() is an example of the Static Factory pattern. It is not an example of the GoF Factory Method Pattern.

The GoF Factory Method Pattern always utilizes inheritance. That is part of its definition. It is contrasted with the other GoF factory pattern here.

Refactoring Guru is a good resource on design patterns in general; but all design pattern tutorials make mistakes, especially regarding factories, because there are so many different versions, and the ones outside the GoF may be more common than the ones in it.

jaco0646
  • 15,303
  • 7
  • 59
  • 83