Home  >  Article  >  Backend Development  >  interpreter php design pattern Interpreter interpreter pattern

interpreter php design pattern Interpreter interpreter pattern

WBOY
WBOYOriginal
2016-07-29 08:45:46930browse

复制代码 代码如下:


/**
* Interpreter Example
*
* @create_date: 2010-01-04
*/
class Expression
{
function interpreter($str)
{
return $str;
}
}
class ExpressionNum extends Expression
{
function interpreter($str)
{
switch($str)
{
case "0": return "零";
case "1": return "一";
case "2": return "二";
case "3": return "三";
case "4": return "四";
case "5": return "五";
case "6": return "六";
case "7": return "七";
case "8": return "八";
case "9": return "九";
}
}
}
class ExpressionCharater extends Expression
{
function interpreter($str)
{
return strtoupper($str);
}
}
class Interpreter
{
function execute($string)
{
$expression = null;
for($i = 0;$i$temp = $string[$i];
switch(true)
{
case is_numeric($temp): $expression = new ExpressionNum(); break;
default: $expression = new ExpressionCharater();
}
echo $expression->interpreter($temp);
}
}
}
$obj = new Interpreter();
$obj->execute("12345abc");
?>

以上就介绍了interpreter php设计模式 Interpreter解释器模式,包括了interpreter方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn