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

yipeiwu_com5年前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彩蛋信息介绍和阻止泄漏的方法(隐藏功能)

Easter Eggs(复活节彩蛋)外行人估计不了解这是神木玩意,彩蛋的网络解释是:用于电脑、电子游戏、电脑游戏、影碟或其他互动多媒体之中的隐藏功能或信息。PHP包含一个安全漏洞,可能导...

Php Cookie的一个使用注意点

复制代码 代码如下:<?php setcookie('test', 'this is a cookie test'); echo ($_COOKIE['test']); ?>...

请离开include_once和require_once

诚然, 这个理由是对的, 不过, 我今天要说的, 是另外一个的原因.我们知道, PHP去判断一个文件是否被加载, 是需要得到这个文件的opened_path的, 意思是说, 比如:复制代...

php上传功能集后缀名判断和随机命名(强力推荐)

不废话了,具体请看下文代码示例讲解。 form.php <html> <head> <meta http-equiv="content-type"...

php中函数的形参与实参的问题说明

当实参个数<形参个数 时php会发出警告,因为php的解释机制会认为,有参数被定义了却没有被使用,那很可能会影响函数的功能。所以会发出警告。然而,当 实参个数>形参个数 时,...