Content deleted Content added
→Abstract functions?: new section |
FluffySquid (talk | contribs) →Is this the best way of doing this?: new section |
||
Line 106:
Gang of Four says ''not'' to use abstract functions, but instead use empty functions. Normally on a builder pattern you may not want to implement everything, if you set abstract functions then you'll be forced to implement them, even if it was empty. - [[Special:Contributions/114.76.239.105|114.76.239.105]] ([[User talk:114.76.239.105|talk]]) 15:11, 26 December 2010 (UTC)
== Is this the best way of doing this? ==
public class BuilderExample {
public static void main(String[] args) {
Cook cook = new Cook();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
cook.setPizzaBuilder(hawaiianPizzaBuilder);
cook.constructPizza();
Pizza hawaiian = cook.getPizza();
cook.setPizzaBuilder(spicyPizzaBuilder);
cook.constructPizza();
Pizza spicy = cook.getPizza();
}
}
Wouldn't it be better to do something like the following:
public class BuilderExample {
public static void main(String[] args) {
Cook cook = new Cook();
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
cook.constructPizza(hawaiianPizzaBuilder);
Pizza hawaiian = hawaiianPizzaBuilder.getPizza();
cook.constructPizza(spicyPizzaBuilder);
Pizza spicy = spicyPizzaBuilder.getPizza();
}
}
The Gang of Four have an interaction diagram that shows that you get the resulting product from the builder. I can't see any reason why you would need a setPizzaBuilder setter - you would just pass the pizzaBuilder in as a parameter. This leads to cleaner code, IMO. - [[User:FluffySquid|FluffySquid]] ([[User talk:FluffySquid|talk]]) 14:11, 17 January 2011 (UTC)
|