2

I see many people using something like this and call it Factory pattern.

class Factory {
    public IProduct Create (ProductEnum type) {
        switch (type) {
            case ProductAType:
                return new ProductA();
            case ProductBType:
                return new ProductB();
        }
    }
}

But isn't the real Factory pattern the one where you have one factory for each product type? So when you create new implementation of IProduct you have to create a new factory class and redefine the Create method. Something like in this picture:

enter image description here

Is the first example also Factory pattern or it is something else, and should that be used or not?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Aleksa
  • 2,976
  • 4
  • 30
  • 49

2 Answers2

1

There are actually two factory patterns. The first one is called factory method and the second one displayed in the picture is abstract factory. People talking about a factory pattern usually don't know one of them or don't understand the difference. It would be waste of time to describe the difference here so I'll point you to this SO question to get started. There's obviously much more on the internet.

What you have there in the code snippet is factory method.

Community
  • 1
  • 1
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • 1
    Please, if you downvote my answer, provide me with feedback. Otherwise I won't be able to improve the answer. – Ondrej Janacek Nov 05 '15 at 01:16
  • The code snippet is not a factory method. A factory method relies on inheritance, per the GoF. In addition to the two GoF factory patterns, there are other, degenerate versions (which in my experience are more common). This snippet is one of those other versions. – jaco0646 Nov 05 '15 at 01:35
  • @jaco0646 Patterns are only suggestions, they don't tell you how your code should look like, but how it should behave. There's nothing like degenerate versions of patterns. That's the sole definition of the word pattern. – Ondrej Janacek Nov 05 '15 at 01:53
  • 1
    Factory method is always a polymorphic call from the client. My favorite example is Java's `Collection.iterator()` method. – Fuhrmanator Nov 05 '15 at 14:34
  • Regarding _degenerate patterns_, I count seven references in the GoF book, including _degenerate factory_ and two references each towards _degenerate iterator_, _degenerate bridge_, and _degenerate composite_. Clearly, there's something like degenerate versions of patterns. – jaco0646 Jul 30 '16 at 01:02
1

But isn't the real Factory pattern the one where you have one factory for each product type?

If by, "real Factory pattern" you mean the GoF Factory Method pattern, then yes: the GoF pattern states that, "subclasses decide which class to instantiate."

Is the first example also Factory pattern or it is something else,

The first example is not a GoF pattern, but it is commonly used and commonly referred to as a Factory. You may also see it called a Simple Factory, or if the method is static, a Static Factory, to differentiate it from the GoF. Confusion is rampant when the distinction is not made between GoF patterns and related (but unofficial) coding idioms.

and should that be used or not?

The reason you would not see an idiom like this in the GoF patterns is that it violates the open/closed principle. The same factory class must be modified every time a new product is added, rather than adding products via inheritance & polymorphism per the GoF. You must judge for yourself whether the simplicity of violating the open/closed principle outweighs potential maintainability.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • Regarding OCP: a Factory method also violates open/closed principle using the same reasoning you have here. A new factory class must be *added* every time a new product is added. Also, the Singleton pattern has little or nothing to do with OCP, yet it was in the GoF book. Furthermore, wouldn't a [simple factory that uses reflection](http://stackoverflow.com/q/30376353/1168342) also respect the OCP? I'm using the part of the definition that states it allows a program's "behaviour to be extended without modifying its source code." – Fuhrmanator Nov 05 '15 at 14:30
  • 1
    Thank you for the three comments. **1.** Adding a new factory class (via subclassing) would be the quintessential example of obeying OCP: the existing factory remains untouched, i.e. Open for extension, Closed for modification. **2.** To quote from the book, _"Use the Singleton pattern when the sole instance should be extensible by subclassing,"_ which is saying the singleton should be Open for extension. **3.** Yes, reflection could certainly be used to satisfy OCP. The GoF focuses on composition and polymorphism. I don't recall any of their patterns mentioning reflection. – jaco0646 Jul 30 '16 at 01:55