简单工厂模式:计算器类
浏览:2569次 出处信息
001 |
<?php |
002 |
/* |
003 |
名称 计算器类 |
004 |
作用 实现两个数字的“加减乘除”运算 |
005 |
备注 除不尽将保留到小数点后13位 |
006 |
用法 $oper = new operator(9,'/',7); |
007 |
echo $oper->getResult(); |
008 |
|
009 |
参数1 第一个数字 |
010 |
参数2 运算符号 |
011 |
参数3 第二个数字 |
012 |
|
013 |
性能 在1秒内响应 |
014 |
输出 无 |
015 |
返回 运算结果 |
016 |
|
017 |
简介 一个简单的工厂加工类。低耦合,融入了类的三大特性:封装、继承、多态。 |
018 |
第一个类作品,学习于《大话设计模式》第一章,原文用的是.net。 |
019 |
|
020 |
版权 署名 |
021 |
日期 2010-6-2 |
022 |
版本 原始版本 |
023 |
作者 崔凯 |
024 |
联系 cuikai.chn@gmail.com |
025 |
网址 http://cuikai-wh.com/ |
026 |
|
027 |
*/ |
028 |
|
029 |
class operator{ |
030 |
|
031 |
private $result; |
032 |
|
033 |
public function __construct($numberA,$operator,$numberB){ |
034 |
|
035 |
switch($operator){ |
036 |
|
037 |
case '+' : |
038 |
$newOper = new operAdd($numberA,$numberB); |
039 |
$this->result = $newOper->getResult(); |
040 |
break; |
041 |
|
042 |
case '-' : |
043 |
$newOper = new operSub($numberA,$numberB); |
044 |
$this->result = $newOper->getResult(); |
045 |
break; |
046 |
|
047 |
case '*' : |
048 |
$newOper = new operMul($numberA,$numberB); |
049 |
$this->result = $newOper->getResult(); |
050 |
break; |
051 |
|
052 |
case '/' : |
053 |
$newOper = new operDiv($numberA,$numberB); |
054 |
$this->result = $newOper->getResult(); |
055 |
break; |
056 |
|
057 |
} |
058 |
} |
059 |
|
060 |
public function getResult(){ |
061 |
return $this->result; |
062 |
} |
063 |
|
064 |
} |
065 |
|
066 |
class values{ |
067 |
|
068 |
protected $numberA; |
069 |
protected $numberB; |
070 |
|
071 |
public function __construct($numberA, $numberB){ |
072 |
$this->numberA = $numberA; |
073 |
$this->numberB = $numberB; |
074 |
} |
075 |
|
076 |
public function getResult(){ |
077 |
return 0; |
078 |
} |
079 |
|
080 |
} |
081 |
|
082 |
class operAdd extends values{ |
083 |
|
084 |
public function getResult(){ |
085 |
return $this->numberA + $this->numberB; |
086 |
} |
087 |
|
088 |
} |
089 |
|
090 |
class operSub extends values{ |
091 |
|
092 |
public function getResult(){ |
093 |
return $this->numberA - $this->numberB; |
094 |
} |
095 |
|
096 |
} |
097 |
|
098 |
class operMul extends values{ |
099 |
|
100 |
public function getResult(){ |
101 |
return $this->numberA * $this->numberB; |
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
class operDiv extends values{ |
107 |
|
108 |
public function getResult(){ |
109 |
|
110 |
if( 0 != $this->numberB){ |
111 |
return $this->numberA / $this->numberB; |
112 |
} |
113 |
else{ |
114 |
return '除数不得为0'; |
115 |
} |
116 |
} |
117 |
|
118 |
} |
119 |
?> |
建议继续学习:
- PHP内核介绍及扩展开发指南―类和对象 (阅读:3711)
- C++讲解 (阅读:2427)
- Javascript 类的实现 (阅读:2249)
- [Perl6]类, 属性, 方法和其它 (阅读:2162)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:新闻站抓取神器:正文抽取接口
后一篇:嘀咕接口示范 >>
文章信息
- 作者:崔凯(小轰) 来源: 时光立方
- 标签: 工厂模式 类 计算器
- 发布时间:2010-07-21 20:16:54
近3天十大热文
-
[903] WordPress插件开发 -- 在插件使用 -
[135] 解决 nginx 反向代理网页首尾出现神秘字 -
[56] 整理了一份招PHP高级工程师的面试题 -
[54] Innodb分表太多或者表分区太多,会导致内 -
[54] 如何保证一个程序在单台服务器上只有唯一实例( -
[52] 全站换域名时利用nginx和javascri -
[52] 海量小文件存储 -
[52] CloudSMS:免费匿名的云短信 -
[51] 用 Jquery 模拟 select -
[49] ps 命令常见用法
