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:
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 Product
s. 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:
- 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.
- 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?
- 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?
- 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