Reputation: 4779
GoF book states that there are two ways to implement Factory Method:
Consider the following issues when applying the Factory Method pattern:
- Two major varieties. The two main variations of the Factory Method pattern are the case when the Creator class is an abstract class and does not provide an implementation for the factory method it declares, and the case when the Creator is a concrete class and provides a default implementation for the factory method. It’s also possible to have an abstract class that defines a default implementation, but this is less common. The first case requires subclasses to define an implementation, because there’s no reasonable default. It gets around the dilemma of having to instantiate unforeseeable classes. In the second case, the concrete Creator uses the factory method primarily for flexibility. It’s following a rule that says, “Create objects in a separate operation so that subclasses can override the way they’re created.” This rule ensures that designers of subclasses can change the class of objects their parent class instantiates if necessary.
- Parameterized factory methods. Another variation on the pattern lets the factory method create multiple kinds of products. The factory method takes a parameter that identifies the kind of object to create. All objects the factory method creates will share the Product interface. In the Document example, Application might support different kinds of Documents. You pass CreateDocument an extra parameter to specify the kind of document to create.
Design Patterns (Design Patterns: Elements of Reusable Object-Oriented Software)
In what cases should I use one approach instead of another. What the benefits and drawbacks when I prefer one approach instead of another?
Thanks in advance.
Upvotes: 1
Views: 304
Reputation: 17066
Kudos for reading the book. Most people attempt #2 believing that is the Factory Method pattern, when in fact #1 claims to describe the two major varieties.
So we're actually dealing with three slightly different versions of the pattern in the quoted text, though only two of them are numbered. The differences between these versions are based on how much information the Creator
has about which Product
implementation it wants.
Creator
with an abstract Factory Method knows nothing about the Product
implementation and leaves everything up to the ConcreteCreator
.Creator
with a default Factory Method knows what Product
implementation it wants most of the time, but not always; so it allows a ConcreteCreator
to override the default.Creator
with a parameterized Factory Method has a menu of Product
implementations to choose from and decides which one to ask the ConcreteCreator
for.So in each consecutive version, the Creator
has progressively more information about the Product
implementations and more logic concerning how the implementation is chosen.
In the Factory Method pattern, a Creator
delegates responsibility for creating objects to its child classes because it, "can't anticipate the class of objects it must create." (page 108) Based on the different varieties, we can see how the pattern changes slightly when a Creator
can anticipate some information about the class of objects to create.
The version you choose depends on how much you know about the Product
implementations at compile time.
Upvotes: 2