1

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"?
peremeykin
  • 519
  • 5
  • 11
  • Sorry, StackOverflow is not a discussion site. One question per post. Please review the [help] and read [Ask] to learn how to use this site effectively. – Jim Garrison Feb 26 '21 at 20:47

2 Answers2

1

Question #1.

  1. No.
  2. No.
  3. Sometimes UML obfuscates a thing more than it clarifies a thing.

Question #2.

  1. I only see one factory interface in the Grand picture. I don't see a separate interface.
  2. ?
  3. Yes. And no.

Question #3. No.

Question #4. No.


I'm not as up on my GoF patterns as I used to be, but I'll take a shot at this. I think you are correct about the difference. The GoF pattern uses inheritance for polymorphism and Grand's example uses conditional logic for polymorphism.

The operational difference is that to add a new type in Grand's example, I would modify the method createImage:

if (ext.equals("png"))
       return new PngImage()

In the GoF version no existing code is modified. I create a new subclass calledPngCreator that implements the Creator interface. This is a good example of the open/closed principle-- the GoF factory is closed to modification, but the pattern open to extension (by adding a new implementation of the factory interface).

Factory vs. Template

Factory Method is always implemented using the Template Method pattern (Design Patterns Smalltalk Companion by Alpert, Brown, and Woolf)

Yes, factory method pattern is a kind of template method pattern. However, a (GoF) pattern is more than its structure or diagram. Every pattern in the GoF book includes intent, motivation, applicability, participants, and so on. Intent and motivation are part of what makes Factory and Template different. For starters, one is a creation pattern and the other is a behavioral pattern.

The intent of the Factory Method pattern is to let subclasses of the Factory decide what class to instantiate. In Grand's version, I see only one concrete factory, and it uses conditional logic. IMHO, that is different than using multiple concrete subclasses. Then again, Grand's version does decouple creation requesters from class names. If you modify the factory to return an instance of Jpeg2000 instead of Jpeg, the compiler won't complain. CreationRequester depends only on the interface Image. This is important if I am using someone else's library and I can't modify it. If avoiding re-compilation of the creation requester is the motivation, then Grand's version works fine.

Is Grand's Factory Method pattern a special case of Abstract Factory pattern?

I don't think it is. I'll borrow the example from my (ancient) copy of Design Patterns Smalltalk Companion. Let's say my application uses vehicles. I might have a VehicleFactory interface with methods like 'createTruck', 'createPassengerCar', 'createMotorcyle`.

What the vehicle manufacturer was important to my applicaiton? Enter the Abstract Factory pattern! VehicleFactory defines the methods createTruck, createPassengerCar, createMotorcyle. I create classes named ToyotaFactory, FordFactory and HondaFactory that all implement the interface.

Here is what each concrete factory return:

ToyotaFactory.createPassengerCar -> Camry

HondaFactory.createPassengerCar -> Accord

FordFactory.createPassengerCar -> Taurus


ToyotaFactory.createMotorcycle -> FM6

HondaFactory.createMotorcycle -> GoldWing

FordFactory.createPassengerCar -> return null or throw exception, or ...

On the next day, I add a FiatFactory. Because the client classes depend on the VehicleFactory interface, I don't have to change any old code or recompile anything.

ahoffer
  • 6,347
  • 4
  • 39
  • 68
1
  1. The GoF Factory Method Pattern defines three varieties: What's the difference between Factory Method implementations? Grand's discriminator is indicative of what the GoF refer to as a parameterized factory method.
  2. No: Design Patterns: Abstract Factory vs Factory Method
  3. No: without inheritance and without a template method, it would be what Head First Design Patterns calls a Simple Factory.
jaco0646
  • 15,303
  • 7
  • 59
  • 83