vendredi 26 février 2021

Factory Method: "Patterns in Java" by Mark Grand vs GoF interpretation

I'm learning Java design patterns by "Patterns in Java", volume 1 by Mark Grand (Factory Method specifically). My point is to highlight difference between closest patterns for myself. There are good answers that clarify the difference between Factory Method and Abstract Factory (Design Patterns: Factory vs Factory method vs Abstract Factory, What is the basic difference between the Factory and Abstract Factory Design Patterns?). But I noticed that most of authors mean some other interpretation of Factory Method compared to one I have read in "Patterns in Java". The interpretation from the answers is closer to Factory Method from GoF book.

GoF interpretation:

GoF Factory Method interpretation

Grand's interpretation: Mark Grand's Factory Method interpretation

To be specific I will describe what in my opinion is a key difference between Grand's and GoF interpretations. The source of polymorphism in GoF interpretation is inheritance: different implementations of Creator create different types of Products. The source of polymorphism in Mark Grand interpretations apparently is "data-driven class determination" (example from book):

Image createImage(String ext){
    if (ext.equals("gif"))
        return new GIFImage();
    if (ext.equals("jpeg"))
        return new JPEGImage();
    ...
}

CreationRequester delegates object creation to other object that encapsulates "data-driven class determination" logic to pick a right type of ProductIF by discriminator argument.

Can any one please explain me:

  1. Is the Grand's Factory Method interpretation equivalent to Factory Method by GoF? Is it actually Factory Method at all? May be it is some kind of generalization or special case of GoF Factory Method? I can't see direct mapping between these interpretations taking into account UML class diagrams.
  2. Let's consider GoF interpretation now. Can one define a factory method in a separate interface (not in abstract class), like Mark Grand describes? Will it still be Factory Method? Can one consider Factory Method in GoF interpretation as a special case of Template Method that returns some product?
  3. If we consider Factory Method as Grand describes, then it becomes obvious that Factory Method is simply a special case of Abstract Factory that produces a single product. Am I right?
  4. If one just encapsulates a "data-driven class determination" logic into a separate class, can this structure be called "Factory Method"?

Aucun commentaire:

Enregistrer un commentaire