标签:
1)简单工厂模式(Simple Factory)
2)工厂方法模式(Factory Method)
3)抽象工厂模式(Abstract Factory)
1 <?php 2 3 $vehicle = Factory::create("plane"); 4 $vehicle->run(); 5 6 7 abstract class vehicle{ 8 abstract function run(); 9 } 10 11 class Car extends vehicle{ 12 13 public function run(){ 14 print("汽车在路上行驶"); 15 } 16 } 17 18 class Plane extends vehicle{ 19 20 public function run(){ 21 print("飞机在天空翱翔"); 22 } 23 } 24 25 class Factory{ 26 27 public static function create($class){ 28 $class = ucfirst($class); 29 $vehicle = new $class(); 30 return $vehicle; 31 } 32 }
1 <?php 2 $factory = new CarFactory(); 3 $vehicle = $factory->create(); 4 $vehicle->run(); 5 6 abstract class vehicle{ 7 abstract function run(); 8 } 9 10 class Car extends vehicle{ 11 12 public function run(){ 13 print("汽车在路上行驶"); 14 } 15 } 16 17 class Plane extends vehicle{ 18 19 public function run(){ 20 print("飞机在天空翱翔"); 21 } 22 } 23 abstract class Factory{ 24 abstract function create(); 25 } 26 27 class CarFactory extends Factory{ 28 29 public function create(){ 30 $vehicle = new Car(); 31 return $vehicle; 32 } 33 } 34 35 class PlaneFactory extends Factory{ 36 37 public function create(){ 38 $vehicle = new Plane(); 39 return $vehicle; 40 } 41 }
标签:
原文地址:http://www.cnblogs.com/helingfeng/p/5793877.html