适配器模式
- 适配器模式将截然不同的函数接口封装成对立的 api
-
理论利用:php 数据操作有 mysql、mysqli、pdo,能够适配器模式对立成统一的。相似还有 cache 适配器。将 memecache、redis、file、apc 等不同缓存,对立成统一
// 应用数据库进行演示 interface IDatabase {public function connect($host,$user,$passwd,$dbname); public function query($sql); public function close();} class mysql implements IDatabase {public function connect($host,$user,$pwd,$dbname){mysql_connect(); mysql_select_db();} public function query ($sql) {mysql_query($sql); } public function close(){mysql_close() } } class PDO implements IDatabase { private $pdo; public function connect($host,$user,$pwd,$dbname){$this->pdo = new \PDO('mysql:host=$host;dbname=$dbname',$user,$pwd); } public function query ($sql) {$this->pdo->query($sql) } public function close(){unset($this->pdo) } }