2.12. 代理模式

未匹配的标注

目的

  1. 在不改变目标对象的情况下添加一些额外的功能
  2. 客户不想或者不能直接引用另一个对象

    代码实现

<?php
class RedisService {
    public function get($key)
    {
        return 'get key '.$key;
    }
}
class RedisProxy {

    protected $redis;
    public function __construct()
    {
        $this->redis = new RedisService();
    }

    public function __call($method, $arguments)
    {
        return $this->redis->{$method}(... $arguments);
    }

    public function gets($str) //額外功能
    {
    }

}
$redisProxy = new RedisProxy();
echo $redisProxy->get('user');

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~