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整数取余返回负数的相关解决方法

PHP语言虽然功能强大,但并不代表其没有缺点,在编写代码的过程中未免会遇到一些让人头痛的问题。下面我们将为大家介绍有关PHP整数取余返回负数的解决办法。 我们先来看个例子. 复制代码 代...

php获取今日开始时间和结束时间的方法

话不多说,请看代码:  $begintime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')))...

php设计模式 Delegation(委托模式)

复制代码 代码如下: <?php /** * 委托模式 示例 * * @create_date: 2010-01-04 */ class PlayList { var $_song...

几个实用的PHP内置函数使用指南

几个实用的PHP内置函数使用指南

PHP有许多内置函数,其中大多数函数都被程序员广泛使用。但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数。 没用过的程序员不妨过来看看。   1.highli...

PHP程序漏洞产生的原因分析与防范方法说明

滥用include 1.漏洞原因: Include是编写PHP网站中最常用的函数,并且支持相对路径。有很多PHP脚本直接把某输入变量作为Include的参数,造成任意引用脚本、绝对路...