vendredi 11 janvier 2019

GoF design patterns: different kinds of factory

I'm using GoF design patterns since a while and I found that I misunderstood some of them.

I'm focused on Factory (not Abstract Factory). Until now, I've thought Factory was a class with several creational methods like BorderFactory in Java API:

  • Border b1 = BorderFactory.createLineBorder(Color.RED)
  • Border b2 = BorderFactory.createTitledBorder("Hello")

All methods returning the same kind of abstraction (e.g. interface).

However, I've found that "Factory" in GoF (real name "Factory Method") is not really this kind of class, but a class with a unique method returning a concrete type depending on the input parameter (e.g. a string). I'm using this kind of factory for parsing purposes (modeling language).

GoF creational patterns are Factory Method, Abstract Factory, Builder, Singleton and Prototype. There is no "Factory", unless Factory is considered as a refined Factory Method (too many input parameters -> need to define several createXXX methods) ?

What do you think about this ?

Thanks.

Aucun commentaire:

Enregistrer un commentaire