简单工厂
类如果直接都用new,那有一天命名需要改动的时候得搜索每一行代码去改,而工厂避免了这种情况
会让我们的代码更具结构化,便于管理和扩展。
比如,我们有一些类,它们都继承自交通工具类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| interface Vehicle { public function drive(); }
class Car implements Vehicle { public function drive() { echo '汽车靠四个轮子滚动行走。'; } }
class Ship implements Vehicle { public function drive() { echo '轮船靠螺旋桨划水前进。'; } }
class Aircraft implements Vehicle { public function drive() { echo '飞机靠螺旋桨和机翼的升力飞行。'; } }
|
再创建一个工厂类,专门用作类的创建,:
1 2 3 4 5 6 7 8 9 10 11 12 13
| class VehicleFactory { public static function build($className = null) { $className = ucfirst($className); if ($className && class_exists($className)) { return new $className(); } return null; } }
|
工厂类用了一个静态方法来创建其他类,在客户端中就可以这样使用:
1 2 3 4
| VehicleFactory::build('Car')->drive(); VehicleFactory::build('Ship')->drive(); VehicleFactory::build('Aircraft')->drive();
|
省去了每次都要new类的工作。