• 技术文章 >后端开发 >php教程

    php设计模式 Adapter(适配器模式)

    高洛峰高洛峰2016-11-30 09:33:04原创679
    复制代码 代码如下:
    <?php
    /**
    * 适配器模式
    *
    * 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作
    */

    // 这个是原有的类型
    class OldCache
    {
    public function __construct()
    {
    echo "OldCache construct<br/>";
    }

    public function store($key,$value)
    {
    echo "OldCache store<br/>";
    }

    public function remove($key)
    {
    echo "OldCache remove<br/>";
    }

    public function fetch($key)
    {
    echo "OldCache fetch<br/>";
    }
    }

    interface Cacheable
    {
    public function set($key,$value);
    public function get($key);
    public function del($key);
    }

    class OldCacheAdapter implements Cacheable
    {
    private $_cache = null;
    public function __construct()
    {
    $this->_cache = new OldCache();
    }

    public function set($key,$value)
    {
    return $this->_cache->store($key,$value);
    }

    public function get($key)
    {
    return $this->_cache->fetch($key);
    }

    public function del($key)
    {
    return $this->_cache->remove($key);
    }
    }

    $objCache = new OldCacheAdapter();
    $objCache->set("test",1);
    $objCache->get("test");
    $objCache->del("test",1);
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php
    上一篇:数据库查询 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 聊聊php百万数据怎么写入csv• 浅析PHP中的类自动加载相关文件• 系统讲解PHP缓存技术• 一文理解和实现现代PHP框架里的IOC容器• PHP文件上传后缀名与文件类型对照表整理_PHP教程
    1/1

    PHP中文网