当前位置:Gxlcms > PHP教程 > PHP设计模式系列--责任链模式(ChainOfResponsibilities)

PHP设计模式系列--责任链模式(ChainOfResponsibilities)

时间:2021-07-01 10:21:17 帮助过:7人阅读

1、模式定义

责任链模式将处理请求的对象连成一条链,沿着这条链传递该请求,直到有一个对象处理请求为止,这使得多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。

责任链模式在现实中使用的很多,常见的就是 OA 系统中的工作流。

2、UML 类图

3、示例代码

Request.php

     

Handler.php

successor)) {            $this->successor = $handler;        } else {            $this->successor->append($handler);        }    }    /**     * 处理请求     *     * This approach by using a template method pattern ensures you that     * each subclass will not forget to call the successor. Besides, the returned     * boolean value indicates you if the request have been processed or not.     *     * @param Request $req     *     * @return bool     */    final public function handle(Request $req)    {        $req->forDebugOnly = get_called_class();        $processed = $this->processing($req);        if (!$processed) {            // the request has not been processed by this handler => see the next            if (!is_null($this->successor)) {                $processed = $this->successor->handle($req);            }        }        return $processed;    }    /**     * 每个处理器具体实现类都要实现这个方法对请求进行处理     *     * @param Request $req     *     * @return bool true if the request has been processed     */    abstract protected function processing(Request $req);}

Responsible/SlowStorage.php

data = $data;    }    protected function processing(Request $req)    {        if ('get' === $req->verb) {            if (array_key_exists($req->key, $this->data)) {                $req->response = $this->data[$req->key];                return true;            }        }        return false;    }}

Responsible/FastStorage.php

data = $data;    }    protected function processing(Request $req)    {        if ('get' === $req->verb) {            if (array_key_exists($req->key, $this->data)) {                // the handler IS responsible and then processes the request                $req->response = $this->data[$req->key];                // instead of returning true, I could return the value but it proves                // to be a bad idea. What if the value IS "false" ?                return true;            }        }        return false;    }}

4、测试代码

Tests/ChainTest.php

chain = new FastStorage(array('bar' => 'baz'));        $this->chain->append(new SlowStorage(array('bar' => 'baz', 'foo' => 'bar')));    }    public function makeRequest()    {        $request = new Request();        $request->verb = 'get';        return array(            array($request)        );    }    /**     * @dataProvider makeRequest     */    public function testFastStorage($request)    {        $request->key = 'bar';        $ret = $this->chain->handle($request);        $this->assertTrue($ret);        $this->assertObjectHasAttribute('response', $request);        $this->assertEquals('baz', $request->response);        // despite both handle owns the 'bar' key, the FastStorage is responding first        $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage';        $this->assertEquals($className, $request->forDebugOnly);    }    /**     * @dataProvider makeRequest     */    public function testSlowStorage($request)    {        $request->key = 'foo';        $ret = $this->chain->handle($request);        $this->assertTrue($ret);        $this->assertObjectHasAttribute('response', $request);        $this->assertEquals('bar', $request->response);        // FastStorage has no 'foo' key, the SlowStorage is responding        $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';        $this->assertEquals($className, $request->forDebugOnly);    }    /**     * @dataProvider makeRequest     */    public function testFailure($request)    {        $request->key = 'kurukuku';        $ret = $this->chain->handle($request);        $this->assertFalse($ret);        // the last responsible :        $className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';        $this->assertEquals($className, $request->forDebugOnly);    }}

5、总结

责任链模式的主要优点在于可以降低系统的耦合度,简化对象的相互连接,同时增强给对象指派职责的灵活性,增加新的请求处理类也很方便;其主要缺点在于不能保证请求一定被接收,且对于比较长的职责链,请求的处理可能涉及到多个处理对象,系统性能将受到一定影响,而且在进行代码调试时不太方便。

人气教程排行