Professional Documents
Culture Documents
Baking With Oo Goodness
Baking With Oo Goodness
stefoxp/head-first-design-patterns Public
master
head-first-design-patterns / baking_with_oo_goodness.md
stefoxp c.4 Baking with OO Goodness complete
1 contributor
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 1/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
} else if (type.equals("pepperoni")) {
pizza = PepperoniPizza();
}
// Then we place that code in an object that is only going to worry about
if (type.equals("cheese")) {
pizza = new CheesePizza();
} else if (type.equals("greek")) {
pizza = new GreekPizza();
} else if (type.equals("pepperoni")) {
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 2/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
pizza = PepperoniPizza();
}
return pizza;
}
}
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// other methods
}
The Simple Factory isn't actually a Design Pattern it's more of a programming idiom.
Simple pizza class diagram
// the client of the factory
class PizzaStore {
orderPizza()
}
// the factory; it should be the only part of our application that refers
class SimplePizzaFactory {
createPizza()
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 3/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
// concrete products
class CheesePizza extends Pizza {}
class PepperoniPizza extends Pizza {}
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// all the responsability for instantiating Pizzas has been moved into
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 4/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
A factory method:
is abstract so the subclasses are counted on to handle object creation;
returns a Product that is typically used within methods defined in the superclass;
isolates the client (the code in the superclass) from knowing what kind of
concrete Product is actually created;
may be parametrized (or not) to select among several variations of a product;
Implementation Code Pizza Store Simple 05
The Factory Method Pattern
The Factory Method Pattern encapsulates object creation by letting subclasses
decide what objects to create.
The Creator classes - class diagram
// The Creator classes ***
abstract createPizza()
orderPizza()
}
createPizza()
}
createPizza()
}
// ***
// ***
Another way to look at this pattern as a framework is the way it encapsulates product
knowledge into each creator. The factory method is the key to encapsulating this
knowledge.
The Factory Method Pattern - class diagram
// the Creator is a class that contains the implementation for all of the
abstract class Creator {
// the abstract factoryMethod is what all Creator subclasses must impl
abstract factoryMethod()
anOperation()
}
Questions:
This pattern is useful if you've only got concrete creator because you are
decoupling the implementation of the product from its use.
The factory method and the Creator class isn't always abstract. You can define a
default factory method to produce some concrete product.
Guidelines help to respect Dependency Inversion Principle
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 6/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
// our other methods remain the same, with the exception of the prepar
void bake() {
System.out.println("Bake for 25 minutes at 350");
}
void cut() {
System.out.println("Cutting the pizza into diagonal slices");
}
void box() {
System.out.println("Place pizza in official PizzaStore box");
}
void setName(String name) {
this.name = name;
}
String getName() {
return name;
}
public String toString() {
// code to print pizza here
}
}
void prepare() {
System.out.println("Preparing " + name);
// here's where the magic happens
// each time it needs an ingredient, it asks the factory to produc
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
}
}
if(item.equals("cheese")) {
// we now pass each pizza the factory that should be used to p
pizza = new CheesePizza(ingredientFactory);
pizza.setName("New York Style Cheese Pizza");
} else if (item.equals("veggie")) {
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("New York Style Veggie Pizza");
} else if (item.equals("clam")) {
pizza = new ClamPizza(ingredientFactory);
pizza.setName("New York Style Clam Pizza");
} else if (item.equals("pepperoni")) {
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("New York Style Pepperoni Pizza");
}
return pizza;
}
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 9/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
// this is the product family. Each concrete factory can produce an entire
interface AbstractProductA {}
interface AbstractProductB {}
// the Client is written against the abstract factory and then composed at
class Client {}
Often the methods of an Abstract Factory are implemented as factory methods. The
job of an Abstract Factory is to define an interface for creating a set of products. Each
method in that interface is responsible for creating a concrete product. So, factory
methods are a natural way to implement your product methods in your abstract
factories.
The Factory Method and Abstract Factory compared
The Factory Method relies on inheritance: object creation is delegated to subclasses
which implement the factory method to create objects.
The Abstract Factory relies on object composition: object creation is implemented in
methods exposed in the factory interface.
All factory patterns promote loose coupling by reducing the dependency of your
application on concrete class.
The intent of Factory Method is to allow a class to defer instantiation to its
subclasses.
The intent of Abstract Factory is to create families of related objects without having to
depend on their concrete classes.
Factory Method PizzaStore class diagram
// is implemented as a Factory Method because we want to be able to create
abstract class PizzaStore {
createPizza()
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 10/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 11/12
06/05/2023, 13:39 head-first-design-patterns/baking_with_oo_goodness.md at master · stefoxp/head-first-design-patterns · GitHub
https://github.com/stefoxp/head-first-design-patterns/blob/master/baking_with_oo_goodness.md 12/12