工厂模式

摘自:     http://www.cnblogs.com/hongfei/archive/2012/07/07/2580776.html

 

 1 <?php
 2     /**
 3      * 工程类,主要用来创建对象
 4      * 功能:根据输入的运算符号,工厂就能实例化出合适的对象
 5      *
 6      */
 7     class Factory{
 8         public static function createObj($operate){
 9             switch ($operate){
10                 case '+':
11                     return new OperationAdd();
12                     break;
13                 case '-':
14                     return new OperationSub();
15                     break;
16                 case '*':
17                     return new OperationSub();
18                     break;
19                 case '/':
20                     return new OperationDiv();
21                     break;
22             }
23         }
24     }
25     $test=Factory::createObj('/');
26     $result=$test->getValue(23,0);
27     echo $result;
28 ?>

这段代码还是不太理解;
2017-3-25
posted @ 2017-04-10 08:31  taoing  阅读(1)  评论(0)    收藏  举报