注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

php开发lamp

《西安--木木》-经历丰富了生活。 架构师QQ群: 246695517

 
 
 

日志

 
 

Responsibility责任链模式  

2018-03-12 15:01:30|  分类: 设计模式--php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


<?php

/**
* Responsibility责任链模式
*/
//Abstract:抽象处理者类
abstract class Handler
{
private $name = ''; //姓名
private $successor = null;//下一步操作者

public function handleRequest($objectPrice, $objectName)
{
}

public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;
}

public function getSuccessor()
{
return $this->successor;
}

public function setSuccessor($successor)
{
$this->successor = $successor;
}
}


//Abstract:部门经理
class DepartmentManager extends Handler
{
public function __construct($name)
{
$this->setName($name);
}

public function handleRequest($objectPrice, $objectName)
{
if ($objectPrice > 10000) {
$this->getSuccessor()->handleRequest($objectPrice, $objectName);
} else {
print("部门经理:" . $this->getName() . "处理申请,购买:" . $objectName . '<br/>');
}
}

}


// Abstract:部门领导

class DepartmentLeader extends Handler
{
public function DepartmentLeader($name)
{
$this->setName($name);
}

public function handleRequest($objectPrice, $objectName)
{
if ($objectPrice > 20000) {
$this->getSuccessor()->handleRequest($objectPrice, $objectName);
} else {
print("部门领导:" . $this->getName() . "处理申请,购买:" . $objectName . '<br/>');
}
}
}


//Abstract:总公司经理

class CompanyManager extends Handler
{
public function CompanyManager($name)
{
$this->setName($name);
}

public function handleRequest($objectPrice, $objectName)
{
if ($objectPrice > 30000) {
$this->getSuccessor()->handleRequest($objectPrice, $objectName);
} else {
print("总公司经理:" . $this->getName() . "处理申请,购买:" . $objectName . '<br/>');
}
}

}


// Abstract:总公司领导
class CompanyLeader extends Handler
{
public function CompanyLeader($name)
{
$this->setName($name);
}

public function handleRequest($objectPrice, $objectName)
{
if ($objectPrice > 40000) {
$this->getSuccessor()->handleRequest($objectPrice, $objectName);
} else {
print("总公司领导:" . $this->getName() . "处理申请,购买:" . $objectName . '<br/>');
}
}
}


//Abstract:客户端测试
//新建员工
$zhangsan = new DepartmentManager("张三");
$lisi = new DepartmentLeader("李四");
$wangwu = new CompanyManager("王五");
$zhaoliu = new CompanyLeader("赵六");

//设置下一步审批者
$zhangsan->setSuccessor($lisi);
$lisi->setSuccessor($wangwu);
$wangwu->setSuccessor($zhaoliu);

//审批流程执行
$zhangsan->handleRequest(8000, "材料1");
$zhangsan->handleRequest(18000, "材料2");
$zhangsan->handleRequest(28000, "材料3");
$zhangsan->handleRequest(38000, "材料4");


/**
* 定义一个request,response
* 请求头
*/
class Request
{
private $request_str = '';

public function getRequestStr()
{
return $this->request_str;
}

public function setRequestStr($request_str)
{
$this->request_str = $request_str;
}
}

/**
* 定义一个request,response
* 返回头
*/
class Response
{
private $response_str;

public function getResponseStr()
{
return $this->response_str;
}

public function setResponseStr($response_str)
{
$this->response_str = $response_str;
}
}


/**
* 下边是过滤器类
* Interface Filter
*/
//过滤接口
interface Filter
{
public function doFilter(Request $request, Response $response, FilterChain $filterChain);
}

/**
* 定义责任过滤链
* Class FilterChain
*/
class FilterChain implements Filter {
private $filters = array();
private $index = 0;

public function addFilter($filter)
{
$this->filters[] = $filter;
return $this;
}

public function doFilter(Request $request, Response $response, FilterChain $filterChain)
{

if ($this->index >= count($this->filters)) {
return;
}

$f = $this->filters[$this->index];
$this->index++;
$f->doFilter($request, $response, $filterChain);
}
}


/**
* html过滤
* Class HTMLFilter
*/
class HTMLFilter implements Filter
{
public function doFilter(Request $request, Response $response, FilterChain $filterChain)
{
$str = $response->getResponseStr();
//过滤
$response->setResponseStr($str.'过滤Html');
$filterChain->doFilter($request, $response, $filterChain);
$resStr = $response->getResponseStr();
print($resStr);
}
}

/**
* 简单过滤
* Class SmileFilter
*/
class SmileFilter implements Filter
{
public function doFilter(Request $request, Response $response, FilterChain $filterChain)
{
$str = $response->getResponseStr();
//过滤
$response->setResponseStr($str.'过滤Smile');

$filterChain->doFilter($request, $response, $filterChain);
$resStr = $response->getResponseStr();
print($resStr);
}
}

/**
* 简单过滤
* Class SmileFilter
*/
class SensitivityFilter implements Filter
{

public function doFilter(Request $request, Response $response, FilterChain $filterChain)
{
$str = $response->getResponseStr();
//过滤
$response->setResponseStr($str.'过滤Sensitivity');
$filterChain->doFilter($request, $response, $filterChain);

//业务定义是否保留原有字符
$resStr = $response->getResponseStr();
print($resStr);
}
}


,
//客户端
$str = '你好:),这句话中有敏感词汇,需要处理。<script></script>,敏感!';
//$str = '你好:),这句话中有敏感词汇,需要处理,敏感!';
$request = new Request();
$response = new Response();
$filterChain = new FilterChain();
$filterChain->addFilter(new HTMLFilter())->addFilter(new SmileFilter())->addFilter(new SensitivityFilter());

$request->setRequestStr($str);
$response->setResponseStr($str);
$filterChain->doFilter($request,$response,$filterChain);



















  评论这张
 
阅读(18)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018