php设计模式 Interpreter(解释器模式)
作者:bea
代码如下: <?php /** * 解释器 示例 * * @create_date: 2010-01-04 */ class Expression { function interpreter($str) { return $str; } } class ExpressionNum extends Expression { function interpreter($str) { switch($str) { case "0"
代码如下:
<?php
/**
* 解释器 示例
*
* @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<strlen($string);$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");
?>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- PHP学习笔记之数组篇
- php设计模式 Visitor 访问者模式
- php懒人函数 自动添加数据
- php中比较简单的导入phpmyadmin生成的sql文件的方法
- php skymvc 一款轻量、简单的php
- 关于php mvc开发模式的感想
- yii框架源码分析之创建controller代码
- 关于PHP中Object对象的笔记分享
- php dirname(__FILE__) 获取当前文件的绝对路径
- php新建文件自动编号的思路与实现
- 调整优化您的LAMP应用程序的5种简单方法
- php开启安全模式后禁用的函数集合
- php设计模式 Adapter(适配器模式)
- php设计模式 Builder(建造者模式)
- php设计模式 DAO(数据访问对象模式)
- php设计模式 Decorator(装饰模式)
- php设计模式 Delegation(委托模式)
- php设计模式 Facade(外观模式)
- php设计模式 Factory(工厂模式)