Head First design pattern PHP (Object Oriented Learning) page 1/2

Source: Internet
Author: User

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
Copy codeThe Code is as follows:
<? Php
/**
* Rule Mode
* Defines algorithm families and encapsulates them separately so that they can be replaced with each other,
* This mode makes the algorithm changes independent of the customers who use the algorithm.
*/
// Flight behavior Interface
Interface FlyBehavior {
Public function fly ();
}
// Call the behavior Interface
Interface QuackBehavior {
Public function quack ();
}
// Wings flight
Class FlyWithWings implements FlyBehavior {
Public function fly (){
Echo "I'm flying !! \ N ";
}
}
// Not 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 extends mquack (){
$ This-> quack_obj-> quack ();
}
Public function implements 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-> mongomfly ();
$ Model-> queue mquack ();
// Provide new capabilities
$ Model-> setFlyBehavior (new FlyRocketPowered ());
$ Model-> setQuackBehavior (new Squeak ());
$ Model-> mongomfly ();
$ Model-> queue mquack ();

?>

Single-piece Mode
Copy codeThe Code is as follows:
<? Php
/**
* Single-piece Mode
* Make sure 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:
<? Php
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 chicagstylecheesepizza ();
} Elseif ($ type = "veggie "){
Return new chicagstyleveggiepizza ();
} Elseif ($ type = "clam "){
Return new chicagstyleclampizza ();
} Elseif ($ type = "papperoni "){
Return new chicagstylepapperonipizza ();
} 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 Chicago stylecheesepizza 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 ();
$ Chicago store = new ChicagoPizzaStore ();
$ Pizza = $ myStore-> orderPizza ("cheese ");
Echo "Ethan ordered a". $ pizza-> getName (). "\ n ";

$ Pizza = $ chicagstore-> orderPizza ("cheese ");
Echo "Ethan ordered a". $ pizza-> getName (). "\ n ";

?>

Factory Model
Copy codeThe Code is as follows:
<? Php
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 chicagstylecheesepizza ();
} Elseif ($ type = "veggie "){
Return new chicagstyleveggiepizza ();
} Elseif ($ type = "clam "){
Return new chicagstyleclampizza ();
} Elseif ($ type = "papperoni "){
Return new chicagstylepapperonipizza ();
} 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 ');
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.