PHP design pattern Adapter (Adapter pattern)_PHP tutorial

WBOY
Release: 2016-07-21 15:27:30
Original
797 people have browsed it

复制代码 代码如下:

/**
* Adapter pattern
*
* Convert the interface of a class into another interface that the customer wants, using classes that are originally incompatible and cannot work together to work together
*/

// 这个是原有的类型
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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323779.htmlTechArticle复制代码 代码如下: ?php /** * 适配器模式 * * 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!