当前位置: 移动技术网 > IT编程>开发语言>PHP > php设计模式 Adapter(适配器模式)

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

2019年04月21日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网