酆叔のBlog

  • 首页
  • 分享技术
  • 八卦黑料
  • 生活日常
  • 日记
酆叔のBlog
上辈子作恶多端,这辈子早起上班。
  1. 首页
  2. IT技术
  3. 正文

适配器模式

2024年2月26日 834点热度 0人点赞 0条评论

特点:将各种截然不同的函数接口封装成统一的API。

应用:PHP中的数据库操作有MySQL,MySQLi,PDO三种,可以用适配器模式统一成一致,使不同的数据库操作,统一成一样的API。类似的场景还有cache适配器,可以将memcache,redis,file,apc等不同的缓存函数,统一成一致。

#例如:封装一个缓存类,它支持  redis 和 memcache,只需切换使用时只需修改相关配置就能实现切换了,而不需要修改大量的代码。
#减少代码间的耦合,可以方便增减需要实现的类。
# 适配器模式
        #定义一个缓存类
        interface Cache
        {
            public function connect();
            public function set($key,$value,$time=0);
            public function get($key);
            public function del($key);
            public function close();
        }
        # 使用 redis 做为缓存
        class Rediss implements Cache
        {
            private $redis;

            public function __construct()
            {
                $this->connect();
            }
            public function connect()
            {
                $this->redis = new Redis();
                return $this->redis->connect('127.0.0.1','6379');
            }

            public function set($key,$value,$time=0)
            {
                if($time==0){
                    return $this->redis->set($key,$value);
                }else{
                    return $this->redis->setex($key,$time,$value);
                }
            }

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

            public function del($key)
            {
                return $this->redis->delete($key);
            }
            public function close()
            {
                return $this->redis->close();
            }
        }
        
        #使用 memcache 做为缓存
        class Memcaches implements Cache
        {
            private $memcache;
    
            public function __construct()
            {
                $this->connect();
            }
            public function connect()
            {
                $this->memcache = new Memcache();
                return $this->memcache->connect('127.0.0.1','11211');
            }
    
            public function set($key,$value,$time=0)
            {
                return $this->memcache->set($key,$value,false,$time);
            }
    
            public function get($key)
            {
                return $this->memcache->get($key);
            }
    
            public function del($key)
            {
                return $this->memcache->delete($key);
            }
            public function close()
            {
                return $this->memcache->close();
            }
        }
        
        #调用
        if($cache_config == 'redis'){
            # 使用 redis 
            $cache = new Rediss();  
        }else{
            # 使用 memcache
            $cache = new Memcaches();
        }
        
        $cache->set('key','value');
        $cache->get('key'));
        $cache->del('key');
        $cache->close();    
标签: PHP 设计模式
最后更新:2024年2月20日

酆叔

上辈子作恶多端,这辈子早起上班。

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

最新 热点 随机
最新 热点 随机
2025/05/15 周四 晴 2025/05/12 周一 晴 2025/05/08 周四 多云 2025/05/07 周三 阵雨 2025/05/06 周二 阵雨 2025/04/30 周三 多云
常用的魔术方法 离开家半个小时就带男的回家睡觉 2025/04/29 周二 晴 PHP 接口类 VUE生命周期 PHP 命名空间
腾讯云
又拍云
订阅
订阅

COPYRIGHT © 2024 酆叔のBlog. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备2023016219号