Thoughts, rants and commentary from a husband, father of five and professional web geek

Chapter 4: The factory pattern

Posted on October 7th, 2007 in Geek Stuff, PHP, PHP Design Patterns, Web Development

Again, a little later than I would have liked, but here is the translation of the first run factory pattern from the Head First Design Patterns book:

<?php
/*
class SimplePizzaFactory {
	public function createPizza($type) {
		$pizza = null;
 
		if ($type == 'cheese') {
			$pizza = new CheesePizza();
		} elseif ($type == 'pepperoni') {
			$pizza = new PepperoniPizza();
		} elseif ($type == 'clam') {
			$pizza = new ClamPizza();
		} elseif ($type == 'veggie') {
			$pizza = new VeggiePizza();
		}
 
		return $pizza;
	}
}
*/
abstract class PizzaStore {
	//public $factory;
 
	//public function __construct(SimplePizzaFactory $factory) {
	public function __construct() {
		//$this->factory = $factory;
	}
 
	public function orderPizza($type) {
		$pizza = $this->createPizza($type);
		$pizza->prepare();
		$pizza->bake();
		$pizza->cut();
		$pizza->box();
		return $pizza;
	}
 
	abstract protected function createPizza($type);
}
 
class NYPizzaStore extends PizzaStore {
	public function createPizza($item) {
		if ($item == 'cheese') {
			return new NYStyleCheesePizza();
		} elseif ($item == 'pepperoni') {
			return new NYStylePepperoniPizza();
		} elseif ($item == 'clam') {
			return new NYStyleClamPizza();
		} elseif ($item == 'veggie') {
			return new NYStyleVeggiePizza();
		} else {
			return null;
		}
	}
}
 
class CAPizzaStore extends PizzaStore {
	public function createPizza($item) {
		if ($item == 'cheese') {
			return new CAStyleCheesePizza();
		} elseif ($item == 'pepperoni') {
			return new CAStylePepperoniPizza();
		} elseif ($item == 'clam') {
			return new CAStyleClamPizza();
		} elseif ($item == 'veggie') {
			return new CAStyleVeggiePizza();
		} else {
			return null;
		}
	}
}
 
class ChicagoPizzaStore extends PizzaStore {
	public function createPizza($item) {
		if ($item == 'cheese') {
			return new ChicagoStyleCheesePizza();
		} elseif ($item == 'pepperoni') {
			return new ChicagoStylePepperoniPizza();
		} elseif ($item == 'clam') {
			return new ChicagoStyleClamPizza();
		} elseif ($item == 'veggie') {
			return new ChicagoStyleVeggiePizza();
		} else {
			return null;
		}
	}
}
 
abstract class Pizza {
	public $name;
	public $dough;
	public $sauce;
	public $toppings = array();
 
	public function prepare() {
		echo '<p>Preparing ' . $this->name . '<br />';
		echo '</p><p>Tossing dough ...</p>';
		echo '<p>Adding sauce ...</p>';
		echo '<p>Adding toppings: ';
		for ($i = 0; $i < count ($this->toppings); $i++) {
			echo ' ' . $this->toppings[$i];
		}
		echo '</ count></p>';
	}
 
	public function bake() {
		echo '<p>Bake for 25 minutes at 350</p>';
	}
 
	public function cut() {
		echo '<p>Cutting the pizza into diagonal slices</p>';
	}
 
	public function box() {
		echo '<p>Place pizza in official PizzaStore box</p>';
	}
 
	public function getName() {
		return $this->name;
	}
}
 
class NYStyleCheesePizza extends Pizza {
	public function __construct() {
		$this->name = 'NY Style Sauce and Cheese Pizza';
		$this->dough = 'Thin Crust Dough';
		$this->sauce = 'Marinara Sauce';
		$this->toppings[] = 'Grated Reggiano Cheese';
	}
}
 
class ChicagoStyleCheesePizza extends Pizza {
	public function __construct() {
		$this->name = 'Chicago Style Deep Dish Cheese Pizza';
		$this->dough = 'Extra Thick Crust Dough';
		$this->sauce = 'Plum Tomato Sauce';
		$this->toppings[] = 'Shredded Mozarella Cheese';
	}
 
	public function cut() {
		echo '<p>Cutting the pizza into square slices</p>';
	}
}
 
class PizzaTestDrive {
	public $NYStore;
	Public $ChicagoStore;
 
	public function __construct() {
		$this->NYStore = new NYPizzaStore();
		$this->ChicagoStore = new ChicagoPizzaStore();
 
		$pizza = $this->NYStore->orderPizza('cheese');
		echo '<p>Ethan ordered a ' . $pizza->getName() . '</p>';
 
		$pizza = $this->ChicagoStore->orderPizza('cheese');
		echo '<p>Joel ordered a ' . $pizza->getName() . '</p>';
	}
}
 
$ptd = new PizzaTestDrive();
?>

Output:
Preparing NY Style Sauce and Cheese Pizza
Tossing dough ...
Adding sauce ...
Adding toppings: Grated Reggiano Cheese
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a NY Style Sauce and Cheese Pizza

Preparing Chicago Style Deep Dish Cheese Pizza
Tossing dough ...
Adding sauce ...
Adding toppings: Shredded Mozarella Cheese
Bake for 25 minutes at 350
Cutting the pizza into square slices
Place pizza in official PizzaStore box
Joel ordered a Chicago Style Deep Dish Cheese Pizza

No Responses to “Chapter 4: The factory pattern”

There are currently no comments on this post. But you can change that...

Leave a Reply

Back to top