发布于 2014-11-05 12:15:09 | 182 次阅读 | 评论: 0 | 来源: 网友投递

本文为大家讲解的是php设计模式 中的Adapter(适配器模式)示例代码,感兴趣的同学参考下。

将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作


<?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);
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务