设计模式之责任链模式(php实现)


github地址:https://github.com/ZQCard/design_pattern

/**
 * 责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。
 * 这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。
 * 在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,
 * 那么它会把相同的请求传给下一个接收者,依此类推。
 * 避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。
 * 优点:
 * 1、降低耦合度。它将请求的发送者和接收者解耦。
 * 2、简化了对象。使得对象不需要知道链的结构。
 * 3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。
 * 4、增加新的请求处理类很方便。
 * 缺点:
 * 1、不能保证请求一定被接收。
 * 2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。
 * 3、可能不容易观察运行时的特征,有碍于除错。
 * 例子以日志错误级别记录为例
 */

(1)AbstractLogger.class.php(抽象日志父类)

<?php

namespace ChainOfResponsibility;

abstract class AbstractLogger
{
    public static $INFO = 1;
    public static $DEBUG = 2;
    public static $ERROR = 3;

    protected $level;

    protected $nextLogger;

    public function setNextLogger(AbstractLogger $nextLogger)
    {
        $this->nextLogger = $nextLogger;
    }

    public function logMessage($level, $message)
    {
        if ($this->level <= $level){
            $this->write($message);
        }

        if ($this->nextLogger != null){
            $this->nextLogger->logMessage($level, $message);
        }
    }

    abstract protected function write($message);
}

(2)ErrorLogger.class.php(错误级别日志处理类)

<?php

namespace ChainOfResponsibility;

class ErrorLogger extends AbstractLogger
{

    public function __construct($level)
    {
        $this->level = $level;
    }
    
    protected function write($message)
    {
        echo "Error Error::Logger: " . $message;
        echo '<br/>';
    }
}

(3)DebugLogger.class.php(调试级别处理类)

<?php

namespace ChainOfResponsibility;

class DebugLogger extends AbstractLogger
{

    public function __construct($level)
    {
        $this->level = $level;
    }
    
    protected function write($message)
    {
        echo "Error File::Logger: " . $message;
        echo '<br/>';
    }
}

(4)ConsoleLogger.class.php(消息级别处理类)

<?php

namespace ChainOfResponsibility;

class ConsoleLogger extends AbstractLogger
{

    public function __construct($level)
    {
        $this->level = $level;
    }
    
    protected function write($message)
    {
        echo "Error Console::Logger: " . $message;
        echo '<br/>';
    }
}

(5)chainOfResponsibility.php(客户端)

<?php

/**
 * 责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。
 * 这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。
 * 在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,
 * 那么它会把相同的请求传给下一个接收者,依此类推。
 * 避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。
 * 优点:
 * 1、降低耦合度。它将请求的发送者和接收者解耦。
 * 2、简化了对象。使得对象不需要知道链的结构。
 * 3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。
 * 4、增加新的请求处理类很方便。
 * 缺点:
 * 1、不能保证请求一定被接收。
 * 2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。
 * 3、可能不容易观察运行时的特征,有碍于除错。
 *
 */

spl_autoload_register(function ($className){
    $className = str_replace('\\','/',$className);
    include $className.".class.php";
});

use ChainOfResponsibility\ErrorLogger;
use ChainOfResponsibility\AbstractLogger;

// 生成责任链
function getChainOfLoggers(){ $errorLogger = new ErrorLogger(AbstractLogger::$ERROR); $debugLogger = new ErrorLogger(AbstractLogger::$DEBUG); $consoleLogger = new ErrorLogger(AbstractLogger::$INFO); $errorLogger->setNextLogger($debugLogger); $debugLogger->setNextLogger($consoleLogger); return $errorLogger; } $loggerChain = getChainOfLoggers(); /** Error Error::Logger: This is an information. Error Error::Logger: This is an debug information. Error Error::Logger: This is an debug information. Error Error::Logger: This is an error information. Error Error::Logger: This is an error information. Error Error::Logger: This is an error information. */ $loggerChain->logMessage(AbstractLogger::$INFO, "This is an information."); $loggerChain->logMessage(AbstractLogger::$DEBUG, "This is an debug information."); $loggerChain->logMessage(AbstractLogger::$ERROR, "This is an error information.");

猜你喜欢

转载自www.cnblogs.com/zhouqi666/p/9153556.html