1 <?php 2 /** 3 * 策略模式 4 * 定义了算法族,分别封装起来,让它们之间可以互相替换, 5 * 此模式让算法的变化独立于使用算法的客户。 6 */ 7 //飞行行为接口 8 interface FlyBehavior { 9 public function fly(); 10 } 11 //呱呱叫行为接口 12 interface QuackBehavior { 13 public function quack(); 14 } 15 //翅膀飞行 16 class FlyWithWings implements FlyBehavior { 17 public function fly() { 18 echo "I'm flying!!\n"; 19 } 20 } 21 //不会飞 22 class FlyNoWay implements FlyBehavior { 23 public function fly() { 24 echo "I can't fly!\n"; 25 } 26 } 27 class FlyRocketPowered implements FlyBehavior { 28 public function fly() { 29 echo "I'm flying with a rocket!\n"; 30 } 31 } 32 class Qquack implements QuackBehavior { 33 public function quack() { 34 echo "Quack\n"; 35 } 36 } 37 class Squeak implements QuackBehavior { 38 public function quack() { 39 echo "Squeak\n"; 40 } 41 } 42 class MuteQuack implements QuackBehavior { 43 public function quack() { 44 echo "<< Silence >>\n"; 45 } 46 } 47 abstract class Duck { 48 protected $quack_obj; 49 protected $fly_obj; 50 public abstract function display(); 51 52 public function performQuack() { 53 $this->quack_obj->quack(); 54 } 55 public function performFly() { 56 $this->fly_obj->fly(); 57 } 58 public function swim() { 59 echo "All ducks float, even decoys!\n"; 60 } 61 public function setFlyBehavior(FlyBehavior $fb) { 62 $this->fly_obj = $fb; 63 } 64 public function setQuackBehavior(QuackBehavior $qb) { 65 $this->quack_obj = $qb; 66 } 67 } 68 69 class ModelDuck extends Duck { 70 public function __construct() { 71 $this->fly_obj = new FlyNoWay(); 72 $this->quack_obj = new MuteQuack(); 73 } 74 public function display() { 75 echo "I'm a model duck!\n"; 76 } 77 } 78 79 $model = new ModelDuck(); 80 $model->performFly(); 81 $model->performQuack(); 82 //提供新的能力 83 $model->setFlyBehavior(new FlyRocketPowered()); 84 $model->setQuackBehavior(new Squeak()); 85 $model->performFly(); 86 $model->performQuack(); 87 88 ?> 89 90 单件模式 91 复制代码 代码如下: 92 93 <?php 94 /** 95 * 单件模式 96 * 确保一个类只有一个实例,并提供一个全局访问点。 97 */ 98 class MyClass { 99 private static $uniqueInstance; 100 private function __construct() { 101 102 } 103 public static function getInstance() { 104 if (self::$uniqueInstance == null) { 105 self::$uniqueInstance = new MyClass(); 106 } 107 return self::$uniqueInstance; 108 } 109 } 110 $myClass = MyClass::getInstance(); 111 var_dump($myClass); 112 $myClass = MyClass::getInstance(); 113 var_dump($myClass); 114 ?> 115 116 工厂方法模式 117 复制代码 代码如下: 118 119 <?php 120 abstract class PizzaStore { 121 public function orderPizza($type) { 122 $pizza = $this->createPizza($type); 123 124 $pizza->prepare(); 125 $pizza->bake(); 126 $pizza->cut(); 127 $pizza->box(); 128 return $pizza; 129 } 130 131 public abstract function createPizza($type); 132 } 133 class NYPizzaStore extends PizzaStore { 134 public function createPizza($type) { 135 if ($type == "cheese") { 136 return new NYStyleCheesePizza(); 137 } elseif ($type == "veggie") { 138 return new NYStyleVeggiePizza(); 139 } elseif ($type == "clam") { 140 return new NYStyleClamPizza(); 141 } elseif ($type == "papperoni") { 142 return new NYStylePapperoniPizza(); 143 } else { 144 return null; 145 146 } 147 } 148 } 149 class ChicagoPizzaStore extends PizzaStore { 150 public function createPizza($type) { 151 if ($type == "cheese") { 152 return new ChicagoStyleCheesePizza(); 153 } elseif ($type == "veggie") { 154 return new ChicagoStyleVeggiePizza(); 155 } elseif ($type == "clam") { 156 return new ChicagoStyleClamPizza(); 157 } elseif ($type == "papperoni") { 158 return new ChicagoStylePapperoniPizza(); 159 } else { 160 return null; 161 } 162 } 163 } 164 abstract class Pizza { 165 public $name; 166 public $dough; 167 public $sauce; 168 public $toppings = array(); 169 170 public function prepare() { 171 echo "Preparing " . $this->name . "\n"; 172 echo "Yossing dough...\n"; 173 echo "Adding sauce...\n"; 174 echo "Adding toppings: \n"; 175 for ($i = 0; $i < count($this->toppings); $i++) { 176 echo " " . $this->toppings[$i] . "\n"; 177 } 178 } 179 180 public function bake() { 181 echo "Bake for 25 minutes at 350\n"; 182 } 183 184 public function cut() { 185 echo "Cutting the pizza into diagonal slices\n"; 186 } 187 188 public function box() { 189 echo "Place pizza in official PizzaStore box\n"; 190 } 191 192 public function getName() { 193 return $this->name; 194 } 195 } 196 197 class NYStyleCheesePizza extends Pizza { 198 public function __construct() { 199 $this->name = "NY Style Sauce and cheese Pizza"; 200 $this->dough = "Thin Crust Dough"; 201 $this->sauce = "Marinara Sauce"; 202 203 $this->toppings[] = "Grated Reggiano Cheese"; 204 } 205 } 206 207 class NYStyleVeggiePizza extends Pizza { 208 public function __construct() { 209 $this->name = "NY Style Sauce and veggie Pizza"; 210 $this->dough = "Thin Crust Dough"; 211 $this->sauce = "Marinara Sauce"; 212 213 $this->toppings[] = "Grated Reggiano veggie"; 214 } 215 } 216 class NYStyleClamPizza extends Pizza { 217 public function __construct() { 218 $this->name = "NY Style Sauce and clam Pizza"; 219 $this->dough = "Thin Crust Dough"; 220 $this->sauce = "Marinara Sauce"; 221 222 $this->toppings[] = "Grated Reggiano clam"; 223 } 224 } 225 class NYStylePapperoniPizza extends Pizza { 226 public function __construct() { 227 $this->name = "NY Style Sauce and papperoni Pizza"; 228 $this->dough = "Thin Crust Dough"; 229 $this->sauce = "Marinara Sauce"; 230 231 $this->toppings[] = "Grated Reggiano papperoni"; 232 } 233 } 234 235 class ChicagoStyleCheesePizza extends Pizza { 236 public function __construct() { 237 $this->name = "Chicago Style Deep Dish Cheese Pizza"; 238 $this->dough = "Extra Thick Crust Dough"; 239 $this->sauce = "Plum Tomato Sauce"; 240 241 $this->toppings[] = "Shredded Mozzarella Cheese"; 242 } 243 244 public function cut() { 245 echo "Cutting the pizza into square slices\n"; 246 } 247 } 248 249 $myStore = new NYPizzaStore(); 250 $chicagoStore = new ChicagoPizzaStore(); 251 $pizza = $myStore->orderPizza("cheese"); 252 echo "Ethan ordered a " . $pizza->getName() . "\n"; 253 254 $pizza = $chicagoStore->orderPizza("cheese"); 255 echo "Ethan ordered a " . $pizza->getName() . "\n"; 256 257 ?> 258 259 工厂模式 260 复制代码 代码如下: 261 262 <?php 263 abstract class PizzaStore { 264 public function orderPizza($type) { 265 $pizza = $this->createPizza($type); 266 267 $pizza->prepare(); 268 $pizza->bake(); 269 $pizza->cut(); 270 $pizza->box(); 271 return $pizza; 272 } 273 274 public abstract function createPizza($type); 275 } 276 class NYPizzaStore extends PizzaStore { 277 public function createPizza($type) { 278 $pizza = null; 279 $ingredientFactory = new NYPizzaIngredientFactory(); 280 if ($type == "cheese") { 281 $pizza = new CheesePizza($ingredientFactory); 282 $pizza->setName('New York Style Cheese Pizza'); 283 } elseif ($type == "veggie") { 284 $pizza = new VeggiePizza($ingredientFactory); 285 $pizza->setName('New York Style Veggie Pizza'); 286 } elseif ($type == "clam") { 287 $pizza = new ClamPizza($ingredientFactory); 288 $pizza->setName('New York Style Clam Pizza'); 289 } elseif ($type == "papperoni") { 290 $pizza = new PapperoniPizza($ingredientFactory); 291 $pizza->setName('New York Style Papperoni Pizza'); 292 } 293 return $pizza; 294 } 295 } 296 class ChicagoPizzaStore extends PizzaStore { 297 public function createPizza($type) { 298 if ($type == "cheese") { 299 return new ChicagoStyleCheesePizza(); 300 } elseif ($type == "veggie") { 301 return new ChicagoStyleVeggiePizza(); 302 } elseif ($type == "clam") { 303 return new ChicagoStyleClamPizza(); 304 } elseif ($type == "papperoni") { 305 return new ChicagoStylePapperoniPizza(); 306 } else { 307 return null; 308 } 309 } 310 } 311 interface PizzaIngredientFactory { 312 public function createDough(); 313 public function createSauce(); 314 public function createCheese(); 315 public function createVeggies(); 316 public function createPepperoni(); 317 public function createClam(); 318 } 319 class NYPizzaIngredientFactory implements PizzaIngredientFactory { 320 public function createDough() { 321 return new ThinCrustDough(); 322 } 323 public function createSauce() { 324 return new MarinaraSauce(); 325 } 326 public function createCheese() { 327 return new ReggianoCheese(); 328 } 329 public function createVeggies() { 330 $veggies = array( 331 new Garlic(), 332 new Onion(), 333 new Mushroom(), 334 new RedPepper(), 335 ); 336 return $veggies; 337 } 338 public function createPepperoni() { 339 return new SlicedPepperoni(); 340 } 341 public function createClam() { 342 return new FreshClams(); 343 } 344 } 345 class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory { 346 public function createDough() { 347 return new ThickCrustDough(); 348 } 349 public function createSauce() { 350 return new PlumTomatoSauce(); 351 } 352 public function createCheese() { 353 return new Mozzarella(); 354 } 355 public function createVeggies() { 356 $veggies = array( 357 new BlackOlives(), 358 new Spinach(), 359 new EggPlant(), 360 ); 361 return $veggies; 362 } 363 public function createPepperoni() { 364 return new SlicedPepperoni(); 365 } 366 public function createClam() { 367 return new FrozenClams(); 368 } 369 } 370 abstract class Pizza { 371 public $name; 372 public $dough; 373 public $sauce; 374 public $veggies = array(); 375 public $cheese; 376 public $pepperoni; 377 public $clam; 378 379 public abstract function prepare(); 380 381 public function bake() { 382 echo "Bake for 25 minutes at 350\n"; 383 } 384 385 public function cut() { 386 echo "Cutting the pizza into diagonal slices\n"; 387 } 388 389 public function box() { 390 echo "Place pizza in official PizzaStore box\n"; 391 } 392 393 public function getName() { 394 return $this->name; 395 } 396 397 public function setName($name) { 398 $this->name = $name; 399 } 400 401 public function __toString() { 402 403 } 404 } 405 406 class CheesePizza extends Pizza { 407 public $ingredientFactory; 408 409 public function __construct(PizzaIngredientFactory $ingredientFactory) { 410 $this->ingredientFactory = $ingredientFactory; 411 } 412 413 public function prepare() { 414 echo "Preparing " . $this->name . "\n"; 415 $this->dough = $this->ingredientFactory->createDough(); 416 $this->sauce = $this->ingredientFactory->createSauce(); 417 $this->cheese = $this->ingredientFactory->createCheese(); 418 } 419 } 420 421 class ClamPizza extends Pizza { 422 public $ingredientFactory; 423 424 public function __construct(PizzaIngredientFactory $ingredientFactory) { 425 $this->ingredientFactory = $ingredientFactory; 426 } 427 428 public function prepare() { 429 echo "Preparing " . $this->name . "\n"; 430 $this->dough = $this->ingredientFactory->createDough(); 431 $this->sauce = $this->ingredientFactory->createSauce(); 432 $this->cheese = $this->ingredientFactory->createCheese(); 433 $clam = $this->ingredientFactory->createClam(); 434 } 435 } 436 437 $nyPizzaStore = new NYPizzaStore(); 438 $nyPizzaStore->orderPizza('cheese'); 439 ?>

浙公网安备 33010602011771号