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

前端之家收集整理的这篇文章主要介绍了php设计模式 Adapter(适配器模式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="27895" class="copybut" id="copybut27895" onclick="doCopy('code27895')"> 代码如下:

<div class="codebody" id="code27895">
<?PHP
/*
适配器模式

将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作
*/ // 这个是原有的类型
class OldCache
{
public function __construct()
{
echo "OldCache construct
";
} public function store($key,$value)
{
echo "OldCache store
";
} public function remove($key)
{
echo "OldCache remove
";
} public function fetch($key)
{
echo "OldCache fetch
";
}
} 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);

原文链接:https://www.f2er.com/php/27835.html

猜你在找的PHP相关文章