The examples in the book are relatively easy to understand, but because it is written by foreigners, the habit of example is not very Chinese characteristics, may occasionally look awkward, there are language habits are not Chinese wind. Of course, after reading this book, you will be able to understand exactly what design patterns can solve for you, and what problems cannot be solved for you (such as not replacing your code).
I've changed some of the code in the book to PHP, and it's easier to understand how the code fits into the concept.
Policy mode
Copy CodeThe code is as follows:
/**
* Strategy mode
* Define the algorithm families, encapsulate each other, so that they can replace each other,
* This mode allows the algorithm to change independently of the customer using the algorithm.
*/
Flight Behavior Interface
Interface Flybehavior {
Public function fly ();
}
Quack Behavior Interface
Interface Quackbehavior {
Public function quack ();
}
Wings flying
Class Flywithwings implements Flybehavior {
Public Function Fly () {
echo "I ' m flying!! \ n ";
}
}
Can't fly
Class Flynoway implements Flybehavior {
Public Function Fly () {
echo "I can ' t fly!\n";
}
}
Class Flyrocketpowered implements Flybehavior {
Public Function Fly () {
echo "I ' m flying with a rocket!\n";
}
}
Class Qquack implements Quackbehavior {
Public Function Quack () {
echo "quack\n";
}
}
Class Squeak implements Quackbehavior {
Public Function Quack () {
echo "squeak\n";
}
}
Class Mutequack implements Quackbehavior {
Public Function Quack () {
echo "<< silence >>\n";
}
}
Abstract class Duck {
protected $quack _obj;
protected $fly _obj;
Public abstract function display ();
Public Function Performquack () {
$this->quack_obj->quack ();
}
Public Function Performfly () {
$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->performfly ();
$model->performquack ();
To provide new capabilities
$model->setflybehavior (New flyrocketpowered ());
$model->setquackbehavior (New Squeak ());
$model->performfly ();
$model->performquack ();
?>
Single-piece mode
Copy CodeThe code is as follows:
/**
* Single-piece mode
* Ensure that a class has only one instance and provides a global access point.
*/
Class MyClass {
private static $uniqueInstance;
Private Function __construct () {
}
public static function getinstance () {
if (self:: $uniqueInstance = = null) {
Self:: $uniqueInstance = new MyClass ();
}
Return self:: $uniqueInstance;
}
}
$myClass = Myclass::getinstance ();
Var_dump ($myClass);
$myClass = Myclass::getinstance ();
Var_dump ($myClass);
?>
Factory method Mode
Copy CodeThe code is as follows:
Abstract class Pizzastore {
Public Function Orderpizza ($type) {
$pizza = $this->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 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 mode
Copy CodeThe code is as follows:
Abstract class Pizzastore {
Public Function Orderpizza ($type) {
$pizza = $this->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 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 ');
?>
http://www.bkjia.com/PHPjc/322141.html www.bkjia.com true http://www.bkjia.com/PHPjc/322141.html techarticle the examples in the book are relatively easy to understand, but because it is written by foreign people, so the example of the habit is not very attached to Chinese characteristics, may occasionally look awkward, there are language habits also do not ...