<?php /** * 適配器模式 * * 將一個類的接口轉換成客戶但願的另一個接口,Adapter模式使用本來不兼容的而不能在一塊兒工做的那些類能夠在一塊兒工做 */ // 這個是原有的類型 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);