Are factory methods used in Smalltalk, and if so, how should one go about writing one, as opposed to how they would in Java, for example? Thanks.
In a factory pattern we instantiate some subclass without naming it. Consider a pizza factory and hierarchy:
Pizza
PepperoniPizza
CheesePizza
...
We'd like instantiate a pizza subclass without knowing it's class name. For example:
pizza := Pizza flavor: 'cheese' size: 12 inches
answers the right subclass of pizza, with it's size filled in.
Now in Java or C++ one would probably do a large 'switch' statement to compare on different string names. Each time we added a new subclass of Pizza, we'd need to remember to add to the master switch statement. See Wikipedia article for typical examples.
Not so in Smalltalk, where classes are first class objects, so we can iterate down the class hierarchy, asking each subclasses to match. For example:
Pizza class>>flavor: aString size: anInteger
matchingClass := self subclasses detect: [:first | first matching: aString].
^matchingClass new size: anInteger.
And whenever we implement a new subclass of pizza, we implement one method to do the factory matching:
CheesePizza class>>matching: aString
^aString = 'cheese'
PepperoniPizza class>>matching: aString
^aString = 'pepperoni'
No central switch statement to maintain. Just objects!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With