<?php
// 抽象处理器类
abstract class Handler
{
protected $successor;
public function setSuccessor(Handler $successor)
{
$this->successor=$successor;
}
abstract public function handleRequest($request);
}
// 具体处理器类A
class ConcreteHandlerA extends Handler
{
public function handleRequest($request)
{
if ($request==”A”) {
echo “ConcreteHandlerA handles the request.\n”;
} else if ($this->successor !=null) {
$this->successor->handleRequest($request);
}
}
}
// 具体处理器类B
class ConcreteHandlerB extends Handler
{
public function handleRequest($request)
{
if ($request==”B”) {
echo “ConcreteHandlerB handles the request.\n”;
} else if ($this->successor !=null) {
$this->successor->handleRequest($request);
}
}
}
// 客户端代码
$handlerA=new ConcreteHandlerA();
$handlerB=new ConcreteHandlerB();
$handlerA->setSuccessor($handlerB);
$handlerA->handleRequest(“A”);
$handlerA->handleRequest(“B”);
// 抽象处理器类
abstract class Handler
{
protected $successor;
public function setSuccessor(Handler $successor)
{
$this->successor=$successor;
}
abstract public function handleRequest($request);
}
// 具体处理器类A
class ConcreteHandlerA extends Handler
{
public function handleRequest($request)
{
if ($request==”A”) {
echo “ConcreteHandlerA handles the request.\n”;
} else if ($this->successor !=null) {
$this->successor->handleRequest($request);
}
}
}
// 具体处理器类B
class ConcreteHandlerB extends Handler
{
public function handleRequest($request)
{
if ($request==”B”) {
echo “ConcreteHandlerB handles the request.\n”;
} else if ($this->successor !=null) {
$this->successor->handleRequest($request);
}
}
}
// 客户端代码
$handlerA=new ConcreteHandlerA();
$handlerB=new ConcreteHandlerB();
$handlerA->setSuccessor($handlerB);
$handlerA->handleRequest(“A”);
$handlerA->handleRequest(“B”);
© 版权声明
文章版权归作者所有,未经允许请勿转载。