工厂方法模式

来源:互联网 发布:剑网三脸型数据要钱 编辑:程序博客网 时间:2024/05/28 16:08
<?php
abstractclass PizzaStore {
    publicfunction orderPizza($type) {
        $pizza= $this->createPizza($type);
  
        $pizza->prepare();
        $pizza->bake();
        $pizza->cut();
        $pizza->box();
        return$pizza;
    }
  
    publicabstract functioncreatePizza($type);
}
classNYPizzaStore extendsPizzaStore {
    publicfunction createPizza($type) {
        if($type == "cheese") {
            returnnew NYStyleCheesePizza();
        }elseif ($type== "veggie") {
            returnnew NYStyleVeggiePizza();
        }elseif ($type== "clam") {
            returnnew NYStyleClamPizza();
        }elseif ($type== "papperoni") {
            returnnew NYStylePapperoniPizza();
        }else {
            returnnull;
  
        }
    }
}
classChicagoPizzaStore extendsPizzaStore {
    publicfunction createPizza($type) {
        if($type == "cheese") {
            returnnew ChicagoStyleCheesePizza();
        }elseif ($type== "veggie") {
            returnnew ChicagoStyleVeggiePizza();
        }elseif ($type== "clam") {
            returnnew ChicagoStyleClamPizza();
        }elseif ($type== "papperoni") {
            returnnew ChicagoStylePapperoniPizza();
        }else {
            returnnull;
        }
    }
}
abstractclass Pizza {
    public$name;
    public$dough;
    public$sauce;
    public$toppings = array();
  
    publicfunction 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";
        }
    }
  
    publicfunction bake() {
        echo"Bake for 25 minutes at 350n";
    }
  
    publicfunction cut() {
        echo"Cutting the pizza into diagonal slicesn";
    }
  
    publicfunction box() {
        echo"Place pizza in official PizzaStore boxn";
    }
  
    publicfunction getName() {
        return$this->name;
    }
}
  
classNYStyleCheesePizza extendsPizza {
    publicfunction __construct() {
        $this->name ="NY Style Sauce and cheese Pizza";
        $this->dough ="Thin Crust Dough";
        $this->sauce ="Marinara Sauce";
  
        $this->toppings[] ="Grated Reggiano Cheese";
    }
}
  
classNYStyleVeggiePizza extendsPizza {
    publicfunction __construct() {
        $this->name ="NY Style Sauce and veggie Pizza";
        $this->dough ="Thin Crust Dough";
        $this->sauce ="Marinara Sauce";
  
        $this->toppings[] ="Grated Reggiano veggie";
    }
}
classNYStyleClamPizza extendsPizza {
    publicfunction __construct() {
        $this->name ="NY Style Sauce and clam Pizza";
        $this->dough ="Thin Crust Dough";
        $this->sauce ="Marinara Sauce";
  
        $this->toppings[] ="Grated Reggiano clam";
    }
}
classNYStylePapperoniPizza extendsPizza {
    publicfunction __construct() {
        $this->name ="NY Style Sauce and papperoni Pizza";
        $this->dough ="Thin Crust Dough";
        $this->sauce ="Marinara Sauce";
  
        $this->toppings[] ="Grated Reggiano papperoni";
    }
}
  
classChicagoStyleCheesePizza extendsPizza {
    publicfunction __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";
    }
  
    publicfunction cut() {
        echo"Cutting the pizza into square slicesn";
    }
}
  
$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";
  
?>