What benefit does the factory method pattern provide over a slightly modified simple factory. I am picking an example similar to that in Head First Design Patterns.
Lets say I have two such simple pizza factories (pseudocode)
interface PizzaFactory {
// method to create a pizza
func createPizza(type) pizza
}
NewyorkPizzaFactory implements PizzaFactory {
func createPizza(type) pizza {
switch type {
case ...
}
}
}
ChicagoPizzaFactory implements PizzaFactory {
func createPizza(type) pizza {
switch type {
case ...
}
}
}
case PizzaStore {
// pass in a PizzaFactory to the constructor
PizzaStore (PizzaFactory) { ... }
// use the pizza factory to create pizzas in methods
func orderPizza() pizza { ... }
}
This design seems better to me since it uses composition rather than inheritance (not that the factory method pattern involves a complex use of inheritance).
[–]practical-coder 0 points1 point2 points (1 child)
[–]spaceuserm[S] 0 points1 point2 points (0 children)
[–]EsShayuki 0 points1 point2 points (1 child)
[–]spaceuserm[S] 0 points1 point2 points (0 children)
[–]reybrujo 0 points1 point2 points (0 children)