FluentInterface
最近时不时的会去学习下设计模式,在DesignPatternsPHP学习中,发现一个特别喜欢的设计模式————FluentInterface,我找不到它的翻译。我个人把它理解为连贯操作/级联操作。在实际中,我发现ThinkPHP和Typecho中均使用了这种模式,并且都在数据库操作类使用这种模式进行连贯操作。于是我用这个模式编写了一个计算器类Calculator,代码如下:
/**
* 使用设计模式中的FluentInterface来实现计算器类
*
* @author unique@hiunique.com
* @copyright 2015-02-08
* @version 0.0.1
*/
class Calculator
{
/**
* 待运算数集合
*
* @access protected
* @var array
*/
protected $_nums = array();
/**
* 待操作的运算符集合
*
* @access protected
* @var array
*/
protected $_operators = array();
/**
* 运算表达式
*
* @access protected
* @var string
*/
protected $_expression = null;
/**
* 格式化结果
*
* @access protected
* @var string
*/
protected $_format = '%s';
/**
* 合法的运算符集合
*
* @access protected
* @var string
*/
protected static $_validOperators = array(
'+', '-', '*', '/'
);
/**
* 构造器
*
* @access public
*/
public function __construct()
{
}
/**
* 设置待运算数
*
* @access public
* @param string|number $num 运算数
* @throws Exception 异常
* @return Calculator Calculator
*/
public function setNum($num)
{
if (floatval($num)) {
$this->_nums[] = floatval($num);
return $this;
}
throw new Exception('The parameter is an unvalid number');
}
/**
* 设置运算符
*
* @access public
* @param string $operator 运算符
* @throws Exception 异常
* @return Calculator Calculator
*/
public function setOperator($operator)
{
if (in_array($operator, self::$_validOperators)) {
$this->_operators[] = $operator;
return $this;
}
throw new Exception('The paramter is an unvalid operator');
}
/**
* 重置
*
* @access public
*/
public function clearAll()
{
$this->_nums = array();
$this->_operators = array();
$this->_expression = null;
$this->_format = '%s';
return $this;
}
/**
* 设置表达式
*
* @access public
* @param string $expression 表达式
* @throws Exception 异常
* @return Calculator Calculator
*/
public function setExpression($expression)
{
if (is_numeric($expression) || is_string($expression)) {
$this->_expression = strval($expression);
return $this;
}
throw new Exception('The expression is an unvalid expression');
}
/**
* 设置结果格式化
*
* @access public
* @param string $format 格式化
* @throws Exception 异常
*/
public function setFormat($format)
{
if (is_string($format)) {
$this->_format = $format;
return $this;
}
throw new Exception('The format is an unvalid format');
}
/**
* 将运算数和运算符拼凑转化为表达式
*
* @access public
* @throws Exception 异常
* @return string 表达式
*/
protected function _linkExpression()
{
$numLen = count($this->_nums);
$operatorLen = count($this->_operators);
if ($numLen - $operatorLen == 1) {
$this->_expression = $this->_nums[0];
for ($i = 1; $i < $numLen; $i ++) {
$this->_expression .= $this->_operators[$i - 1] . $this->_nums[$i];
}
return $this->_expression;
}
throw new Exception('The expression is an unvalid expression');
}
/**
* 获取转化后或者设置的表达式
*
* @access public
*/
public function getExpression()
{
return $this->_linkExpression();
}
/**
* 获取计算结果
*
* @access public
* @param string $format 格式化
* @return string 计算结果
*/
public function getResult($format = null)
{
$this->_expression = $this->_expression ? $this->_expression : $this->_linkExpression();
$this->_format = $format ? $format : $this->_format;
$result = eval('return ' . $this->_expression . ';');
return sprintf($this->_format, $result);
}
}
//测试
$calculator = new Calculator();
var_dump($calculator->setNum(1)->setNum(3)->setOperator('/')->setNum(6)->setOperator('*')->getResult());
转载需经作者允许并注明出处(http://blog.hiunique.com/php/94.html)
来自:你好创造者