php設計模式 Adapter(適配器模式)
更新時間:2011年06月26日 11:25:28 作者:
將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口,使用原本不兼容的而不能在一起工作的那些類可以在一起工作
復制代碼 代碼如下:
<?php
/**
* 適配器模式
*
* 將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口,使用原本不兼容的而不能在一起工作的那些類可以在一起工作
*/
// 這個是原有的類型
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設計模式之適配器模式(Adapter)原理與用法詳解
- PHP設計模式之適配器模式代碼實例
- 學習php設計模式 php實現(xiàn)適配器模式
- PHP設計模式之適配器模式原理與用法分析
- php設計模式之適配器模式原理、用法及注意事項詳解
- PHP設計模式之適配器模式定義與用法詳解
- php設計模式之適配器模式實例分析【星際爭霸游戲案例】
- PHP設計模式(四)原型模式Prototype實例詳解【創(chuàng)建型】
- PHP設計模式(三)建造者模式Builder實例詳解【創(chuàng)建型】
- PHP設計模式(一)工廠模式Factory實例詳解【創(chuàng)建型】
- PHP設計模式概論【概念、分類、原則等】
- PHP設計模式(五)適配器模式Adapter實例詳解【結(jié)構型】
相關文章
PHP判斷一個數(shù)組是另一個數(shù)組子集的方法詳解
這篇文章主要介紹了PHP判斷一個數(shù)組是另一個數(shù)組子集的方法,結(jié)合實例形式分析了php循環(huán)遍歷、array_diff及array_intersect函數(shù)等方法實現(xiàn)數(shù)組子集判斷的相關操作技巧,需要的朋友可以參考下2017-07-07用mysql觸發(fā)器自動更新memcache的實現(xiàn)代碼
不錯的一篇文章,用于項目中可以帶來更多的便利,按照方法已經(jīng)調(diào)試成功,可以大大提高項目的速度。2009-10-10php的數(shù)組與字符串的轉(zhuǎn)換函數(shù)整理匯總
以下是對php中的數(shù)組與字符串的轉(zhuǎn)換函數(shù)進行了詳細的整理匯總,需要的朋友可以參考下2013-07-07