解释器模式和职责链模式

<?php
//解释器模式
abstract class Expression {
    abstract function interpreter($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];
		    if(is_numeric($temp)) {
			    $expression = new ExpressionNum();
			} else {
			    $expression = new ExpressionCharater();
			}
			echo $expression->interpreter($temp);
		}
	}
}


$obj = new Interpreter();
$obj->execute('1232323jawewez');

 

职责链模式:客户提交请求到职责链,请求是沿职责链处理。

优点,减低了客户和请求处理着的耦合,客户不需要知道谁处理的请求,可以随时修改审批流程。

<?php
class Request {
    private $requestType;
	private $requestContent;
	private $requestNumber;

	function getRequestType() {
	    return $this->requestType;
	}

	function setRequestType($type) {
	    $this->requestType = $type;
	}

	function getRequestContent() {
	    return $this->requestContent;
	}

	function setRequestContent($content) {
	    $this->requestContent =  $content;
	}
	
	function getRequestNumber() {
	    return $this->requestNumber;
	}

	function setRequestNumber($number) {
	    $this->requestNumber = $number;
	}
}

abstract class Manager {
    protected $name;
	protected $superior;
	function __construct($name) {
	    $this->name = $name;
	}

	function setSuperior($superior) {
	    $this->superior = $superior;
	}

	abstract function requestApplications(Request $request);
}

class CommonManager extends Manager {
    function requestApplications(Request $request) {
	    if ($request->getRequestType() =='请假' && $request->getRequestNumber() <= 2) {
            echo  $request->getRequestContent() . $request->getRequestNumber() . ''. "$this->name" . "批准\n";
		} else {
		    if (!empty($this->superior)) {
                $this->superior->requestApplications($request);
			}
		}
	}
}
//总监
class Majordomo extends Manager {
    function requestApplications(Request $request) {
       if ($request->getRequestType() =='请假' && $request->getRequestNumber() <= 5) {
            echo  $request->getRequestContent() . $request->getRequestNumber() . ''. "$this->name" . "批准\n";
		} else {
		    if (!empty($this->superior)) {
                $this->superior->requestApplications($request);
			}
		}

	}
}
// 总经理可以处理所有的请求
class GeneralManager extends Manager {
    function requestApplications(Request $request) {
        if ($request->getRequestType() =='请假') {
            echo  $request->getRequestContent() . $request->getRequestNumber() . ''. "$this->name" . "批准\n";
		} else if($request->getRequestType() == '加薪' && $request->getRequestNumber() <= 500){
            echo  $request->getRequestContent() . $request->getRequestNumber() . '' . $this->name ."批准\n";
		} else if($request->getRequestType() == '加薪' && $request->getRequestNumber() > 500) {
            echo  $request->getRequestContent() . $request->getRequestNumber() . '再说吧';
		}

	}
}

$commManager = new CommonManager('经理');
$majordomo = new Majordomo('总监');
$generalManager = new GeneralManager('总经理');
$commManager->setSuperior($majordomo);
$majordomo->setSuperior($generalManager);

$request = new Request();
$request->setRequestType('请假');
$request->setRequestContent('小菜请假');
$request->setRequestNumber(2);
$commManager->requestApplications($request);
$request->setRequestNumber(4);
$commManager->requestApplications($request);

$request->setRequestType('加薪');
$request->setRequestContent('小菜加薪');
$request->setRequestNumber(500);
$commManager->requestApplications($request);
posted @ 2012-12-13 15:07  phpzxh  阅读(...)  评论(... 编辑 收藏