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

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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);

相关文章

PHP使用函数用法详解

1.php_check_syntax 这个函数可以用来检查特定文件中的PHP语法是否正确。 <?php $error_message = ""; $filename =...

lnmp安装多版本PHP共存的方法详解

lnmp安装多版本PHP共存的方法详解

通过lnmp安装了PHP7版本,但是发现与程序不兼容,需要降低到7.0以下的版本。 查找lnmp的install.sh文件,一般在/root/lnmp1.5/install.sh 下执行...

探讨php中header的用法详解

 header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more info...

php的POSIX 函数以及进程测试的深入分析

php的POSIX 函数以及进程测试的深入分析

复制代码 代码如下:<?phpecho posix_getpid(); //8805sleep(10);?> 再用 #ps -ax 这个时候如果多开开个浏览器请求,就会发...

浅析PHP数据导出知识点

最近在做后台管理的项目,后台通常有数据导出到 excel 的需要,经过之前搜索通常推荐使用的是 php excel ,我经常使用的是 laravel ,对于 php excel 也有很好...