码迷,mamicode.com
首页 > 其他好文 > 详细

工厂模式之简单计算器的实现

时间:2015-07-22 01:25:12      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:


<?php abstract class operation{ public $num1; public $num2; public function setNum1($num1){ $this->num1 = $num1; } public function setNum2($num2){ $this->num2 = $num2; } public function getResult(){ return null; } } class add extends operation{ public function getResult(){ return $this->num1+$this->num2; } } class mul extends operation{ public function getResult(){ return $this->num1*$this->num2; } } class Factory{ public static function setOpratory($type){ switch($type){ case ‘+‘: return new add(); break; case ‘*‘: return new mul(); break; default: return null; break; } } } $cal = Factory::setOpratory(‘*‘); $cal->setNum1(4); $cal->setNum2(2); $result = $cal->getResult();

  

工厂模式之简单计算器的实现

标签:

原文地址:http://www.cnblogs.com/qbyyqhcz/p/4666025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!