设计模式之工厂模式

工厂模式是另一种非常常用的模式,正如其名字所示:确实是对象实例的生产工厂。某些意义上,工厂模式提供了通用的方法有助于我们去获取对象,而不需要关心其具体的内在的实现。

 1 /**
 2  * Factory claa[工厂模式]
 3  */
 4 interface SystemFactory
 5 {
 6     public function createSystem($type);
 7 }
 8 class MySystemFactory implements SystemFactory
 9 {
10     //实现工厂方法
11     public function createSystem($type)
12     {
13         // TODO: Implement createSystem() method.
14         switch ($type) {
15             case 'Mac':
16                 return new MacSystem();
17             case 'Win':
18                 return new WinSystem();
19             case 'Linux':
20                 return new LinuxSystem();
21         }
22     }
23 }
24 
25 class System{}
26 class WinSystem extends System{}
27 class MacSystem extends System{}
28 class LinuxSystem extends System{}
29 
30 $System_obj = new MySystemFactory();
31 var_dump($System_obj->createSystem('Mac'));
原文地址:https://www.cnblogs.com/caohongchang/p/11537501.html