HeadFirst design mode is a good book to explain the design mode. it is not as boring as F4. it should be easy to understand. The Head First design model is a good book that explains the design model. it is not as boring as F4. it should be easy to understand.
Examples in the book are easy to understand, but because they are written by foreign experts, the habits of the examples are not very relevant to Chinese characteristics, and may occasionally look awkward, and the language habits are not Chinese style. Of course, after reading this book, you will be able to deeply understand what problems the design pattern can solve for you and what problems it cannot solve for you (for example, it cannot replace your encoding ).
I changed some of the code in the book to PHP. it is easier to understand the concept of code combination.
Rule mode
The code is as follows:
> \ N ";}} abstract class Duck {protected $ quack_obj; protected $ fly_obj; public abstract function display (); public function implements mquack () {$ this-> quack_obj-> quack ();} public function extends mfly () {$ this-> fly_obj-> fly ();} public function swim () {echo "All ducks float, even decoys! \ N ";} public function setFlyBehavior (FlyBehavior $ fb) {$ this-> fly_obj = $ fb;} public function setQuackBehavior (QuackBehavior $ qb) {$ this-> quack_obj = $ qb;} class ModelDuck extends Duck {public function _ construct () {$ this-> fly_obj = new FlyNoWay (); $ this-> quack_obj = new MuteQuack ();} public function display () {echo "I'm a model duck! \ N ";}}$ model = new ModelDuck (); $ model-> elastic mfly (); $ model-> elastic mquack (); // provide new capabilities $ model-> setFlyBehavior (new FlyRocketPowered (); $ model-> setQuackBehavior (new Squeak (); $ model-> parse mfly (); $ model-> queue mquack ();?>
Single-piece mode
The code is as follows:
Factory method mode
The code is as follows:
createPizza($type); $pizza->prepare(); $pizza->bake(); $pizza->cut(); $pizza->box(); return $pizza; } public abstract function createPizza($type); } class NYPizzaStore extends PizzaStore { public function createPizza($type) { if ($type == "cheese") { return new NYStyleCheesePizza(); } elseif ($type == "veggie") { return new NYStyleVeggiePizza(); } elseif ($type == "clam") { return new NYStyleClamPizza(); } elseif ($type == "papperoni") { return new NYStylePapperoniPizza(); } else { return null; } } } class ChicagoPizzaStore extends PizzaStore { public function createPizza($type) { if ($type == "cheese") { return new ChicagoStyleCheesePizza(); } elseif ($type == "veggie") { return new ChicagoStyleVeggiePizza(); } elseif ($type == "clam") { return new ChicagoStyleClamPizza(); } elseif ($type == "papperoni") { return new ChicagoStylePapperoniPizza(); } else { return null; } } } abstract class Pizza { public $name; public $dough; public $sauce; public $toppings = array(); public function prepare() { echo "Preparing " . $this->name . "\n"; echo "Yossing dough...\n"; echo "Adding sauce...\n"; echo "Adding toppings: \n"; for ($i = 0; $i < count($this->toppings); $i++) { echo " " . $this->toppings[$i] . "\n"; } } public function bake() { echo "Bake for 25 minutes at 350\n"; } public function cut() { echo "Cutting the pizza into diagonal slices\n"; } public function box() { echo "Place pizza in official PizzaStore box\n"; } 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 NYStyleVeggiePizza extends Pizza { public function __construct() { $this->name = "NY Style Sauce and veggie Pizza"; $this->dough = "Thin Crust Dough"; $this->sauce = "Marinara Sauce"; $this->toppings[] = "Grated Reggiano veggie"; } } class NYStyleClamPizza extends Pizza { public function __construct() { $this->name = "NY Style Sauce and clam Pizza"; $this->dough = "Thin Crust Dough"; $this->sauce = "Marinara Sauce"; $this->toppings[] = "Grated Reggiano clam"; } } class NYStylePapperoniPizza extends Pizza { public function __construct() { $this->name = "NY Style Sauce and papperoni Pizza"; $this->dough = "Thin Crust Dough"; $this->sauce = "Marinara Sauce"; $this->toppings[] = "Grated Reggiano papperoni"; } } 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 Mozzarella Cheese"; } public function cut() { echo "Cutting the pizza into square slices\n"; } } $myStore = new NYPizzaStore(); $chicagoStore = new ChicagoPizzaStore(); $pizza = $myStore->orderPizza("cheese"); echo "Ethan ordered a " . $pizza->getName() . "\n"; $pizza = $chicagoStore->orderPizza("cheese"); echo "Ethan ordered a " . $pizza->getName() . "\n"; ?>
Factory model
The code is as follows:
createPizza($type); $pizza->prepare(); $pizza->bake(); $pizza->cut(); $pizza->box(); return $pizza; } public abstract function createPizza($type); } class NYPizzaStore extends PizzaStore { public function createPizza($type) { $pizza = null; $ingredientFactory = new NYPizzaIngredientFactory(); if ($type == "cheese") { $pizza = new CheesePizza($ingredientFactory); $pizza->setName('New York Style Cheese Pizza'); } elseif ($type == "veggie") { $pizza = new VeggiePizza($ingredientFactory); $pizza->setName('New York Style Veggie Pizza'); } elseif ($type == "clam") { $pizza = new ClamPizza($ingredientFactory); $pizza->setName('New York Style Clam Pizza'); } elseif ($type == "papperoni") { $pizza = new PapperoniPizza($ingredientFactory); $pizza->setName('New York Style Papperoni Pizza'); } return $pizza; } } class ChicagoPizzaStore extends PizzaStore { public function createPizza($type) { if ($type == "cheese") { return new ChicagoStyleCheesePizza(); } elseif ($type == "veggie") { return new ChicagoStyleVeggiePizza(); } elseif ($type == "clam") { return new ChicagoStyleClamPizza(); } elseif ($type == "papperoni") { return new ChicagoStylePapperoniPizza(); } else { return null; } } } interface PizzaIngredientFactory { public function createDough(); public function createSauce(); public function createCheese(); public function createVeggies(); public function createPepperoni(); public function createClam(); } class NYPizzaIngredientFactory implements PizzaIngredientFactory { public function createDough() { return new ThinCrustDough(); } public function createSauce() { return new MarinaraSauce(); } public function createCheese() { return new ReggianoCheese(); } public function createVeggies() { $veggies = array( new Garlic(), new Onion(), new Mushroom(), new RedPepper(), ); return $veggies; } public function createPepperoni() { return new SlicedPepperoni(); } public function createClam() { return new FreshClams(); } } class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory { public function createDough() { return new ThickCrustDough(); } public function createSauce() { return new PlumTomatoSauce(); } public function createCheese() { return new Mozzarella(); } public function createVeggies() { $veggies = array( new BlackOlives(), new Spinach(), new EggPlant(), ); return $veggies; } public function createPepperoni() { return new SlicedPepperoni(); } public function createClam() { return new FrozenClams(); } } abstract class Pizza { public $name; public $dough; public $sauce; public $veggies = array(); public $cheese; public $pepperoni; public $clam; public abstract function prepare(); public function bake() { echo "Bake for 25 minutes at 350\n"; } public function cut() { echo "Cutting the pizza into diagonal slices\n"; } public function box() { echo "Place pizza in official PizzaStore box\n"; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function __toString() { } } class CheesePizza extends Pizza { public $ingredientFactory; public function __construct(PizzaIngredientFactory $ingredientFactory) { $this->ingredientFactory = $ingredientFactory; } public function prepare() { echo "Preparing " . $this->name . "\n"; $this->dough = $this->ingredientFactory->createDough(); $this->sauce = $this->ingredientFactory->createSauce(); $this->cheese = $this->ingredientFactory->createCheese(); } } class ClamPizza extends Pizza { public $ingredientFactory; public function __construct(PizzaIngredientFactory $ingredientFactory) { $this->ingredientFactory = $ingredientFactory; } public function prepare() { echo "Preparing " . $this->name . "\n"; $this->dough = $this->ingredientFactory->createDough(); $this->sauce = $this->ingredientFactory->createSauce(); $this->cheese = $this->ingredientFactory->createCheese(); $clam = $this->ingredientFactory->createClam(); } } $nyPizzaStore = new NYPizzaStore(); $nyPizzaStore->orderPizza('cheese'); ?>
Observer Mode
The code is as follows:
Observers = array ();} public function registerObserver (Observer $ o) {$ this-> observers [] = $ o;} public function removeObserver (Observer $ o) {if ($ key = array_search ($ o, $ this-> observers ))! = False) {unset ($ this-> observers [$ key]) ;}} public function policyobservers () {foreach ($ this-> observers as $ observer) {$ observer-> update ($ this-> temperature, $ this-> humidity, $ this-> pressure) ;}} public function measurementsChanged () {$ this-> policyobservers ();} public function setMeasurements ($ temperature, $ humidity, $ pressure) {$ this-> temperature = $ temperature; $ this-> humidity = $ humidit Y; $ this-> pressure = $ pressure; $ this-> measurementsChanged ();} class CurrentConditionsDisplay implements Observer, DisplayElement {private $ temperature; private $ humidity; private $ weatherData; public function _ construct (Subject $ weatherData) {$ this-> weatherData = $ weatherData; $ weatherData-> registerObserver ($ this);} public function update ($ temperature, $ humidity, $ pressure) {$ this-> temper Ature = $ temperature; $ this-> humidity = $ humidity; $ this-> display ();} public function display () {echo "temperature :". $ this-> temperature. "; humidity :". $ this-> humidity. "% \ n" ;}} class StatistionsDisplay implements Observer, DisplayElement {private $ temperature; private $ humidity; private $ pressure; private $ weatherData; public function _ construct (Subject $ weatherData) {$ this-> weatherData = $ weatherDa Ta; $ weatherData-> registerObserver ($ this);} public function update ($ temperature, $ humidity, $ pressure) {$ this-> temperature = $ temperature; $ this-> humidity = $ humidity; $ this-> pressure = $ pressure; $ this-> display ();} public function display () {echo "temperature :". $ this-> temperature. "; humidity :". $ this-> humidity. "%; pressure :". $ this-> pressure. "\ n" ;}}$ weatherData = new WeatherData (); $ currentDisplay = New CurrentConditionsDisplay ($ weatherData); $ statistionDisplay = new StatistionsDisplay ($ weatherData); $ weatherData-> setMeasurements (10, 20, 30); $ weatherData-> removeObserver ($ currentDisplay ); $ weatherData-> setMeasurements (30, 40, 50);?>
Command mode
The code is as follows:
light = $light; } public function execute() { $this->light->on(); } } class SimpleRemoteControl { public $slot; public function __construct() { } public function setCommand(Command $command) { $this->slot = $command; } public function buttonWasPressed() { $this->slot->execute(); } } class RemoteControlTest { public static function main() { $remote = new SimpleRemoteControl(); $light = new Light(); $lightOn = new LightOnCommand($light); $remote->setCommand($lightOn); $remote->buttonWasPressed(); } } RemoteControlTest::main(); ?>
Decoration mode
The code is as follows:
Description;} public abstract function cost ();} abstract class CondimentDecorator extends Beverage {// This is an abstract class in JAVA code. PHP does not allow public function getDescription () {return $ this-> description;} class Espresso extends Beverage {public function _ construct () {$ this-> description = "Espresso";} public function cost () {return 1.99 ;}} class HouseBlend extends Beverage {public function _ construc T () {$ this-> description = "HouseBlend";} public function cost () {return. 89 ;}} class DarkRoast extends Beverage {public function _ construct () {$ this-> description = "DarkRoast" ;}public function cost () {return. 99 ;}} class Mocha extends CondimentDecorator {public $ beverage; public function _ construct (Beverage $ beverage) {$ this-> beverage = $ beverage;} public function getDescription () {Return $ this-> beverage-> getDescription (). ", Mocha";} public function cost () {return. 20 + $ this-> beverage-> cost () ;}} class Whip extends CondimentDecorator {public $ beverage; public function _ construct (Beverage $ beverage) {$ this-> beverage = $ beverage;} public function getDescription () {return $ this-> beverage-> getDescription (). ", Whip";} public function cost () {return. 10 + $ this-> Beverage-> cost () ;}} class Soy extends CondimentDecorator {public $ beverage; public function _ construct (Beverage $ beverage) {$ this-> beverage = $ beverage ;} public function getDescription () {return $ this-> beverage-> getDescription (). ", Soy";} public function cost () {return. 15 + $ this-> beverage-> cost () ;}$ beverage = new Espresso (); echo $ beverage-> getDescription (). "\ n"; $ beverage2 = ne W DarkRoast (); $ beverage2 = new Mocha ($ beverage2); $ beverage2 = new Mocha ($ beverage2); $ beverage2 = new Whip ($ beverage2 ); echo $ beverage2-> getDescription (). "$ ". $ beverage2-> cost (). "\ n"; $ beverage3 = new HouseBlend (); $ beverage3 = new Soy ($ beverage3); $ beverage3 = new Mocha ($ beverage3 ); $ beverage3 = new Whip ($ beverage3); echo $ beverage3-> getDescription (). "$ ". $ beverage3-> cost (). "\ n";?>
Status mode
The code is as follows:
count = $count; if ($count > 0) { $this->state = self::NO_QUARTER; } } public function insertQuarter() { if ($this->state == self::HAS_QUARTER) { echo "You can't insert another quarter!\n"; } else if ($this->state == self::NO_QUARTER) { $this->state = self::HAS_QUARTER; echo "You inserted a quarter!\n"; } else if ($this->state == self::SOLD_OUT) { echo "You can't insert a quarter, the machine is sold out!\n"; } else if ($this->state == self::SOLD) { echo "Please wait, we're already giving you a gumball!\n"; } } } $obj = new GumballMachine(0); print_r($obj) ?>
The above is the PHP version of the Head First design pattern code (Object-Oriented Learning) _ php tips. For more information, see The PHP Chinese website (www.php1.cn )!