适配器模式

一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。也就是说,将不同的数据接口封装成统一的API

数据库适配器示例:
vim IDatabase.php

<?php

namespace Components\DatabaseAdapter;

/**
 * 适配器模式 接口
 * @author YUNDUAN
 *
 */
interface IDatabase{
	function connect($host, $user, $passwd, $dbname);
	function query($sql);
	function close();
}

vim Mysql.php
<?php

namespace Components\DatabaseAdapter;

class Mysql implements IDatabase {
	protected $conn;
	function connect($host, $user, $passwd, $dbname) {
		$conn = mysql_connect($host, $user, $passwd);
		mysql_select_db($dbname);
		$this->conn = $conn;
	}
	
	function query($sql) {
		return mysql_query($sql, $this->conn);
	}
	
	function close(){
		mysql_close($this->conn);
	}
}

vim Mysqli.php
<?php

namespace Components\DatabaseAdapter;

class Mysqli implements IDatabase {
	protected $conn;
	function connect($host, $user, $passwd, $dbname) {
		$conn = mysqli_connect($host, $user, $passwd, $dbname);
		$this->conn = $conn;
	}
	
	function query($sql) {
		return mysqli_query($this->conn, $sql);
	}
	
	function close(){
		mysqli_close($this->conn);
	}
}

vim PDO.php
<?php

namespace Components\DatabaseAdapter;

class PDO implements IDatabase {
	protected $conn;
	function connect($host, $user, $passwd, $dbname) {
		$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $passwd);;
		$this->conn = $conn;
	}
	
	function query($sql) {
		return $this->conn->query($sql);
	}
	
	function close(){
		unset($this->conn);
	}
}

使用:
$db4 = new Components\DatabaseAdapter\PDO();//适配器模式
/*这里的PDO可以根据环境来进行自由适配*/


代理模式

在客户端和实体之间建立一个代理对象,客户端对实体的操作全部委派给代理对象,隐藏实体具体实现细节。
Proxy还可以与业务代码分离,部署到另外的服务器,业务代码中通过RPC来委派任务。

代理Proxy.php:

<?php

namespace Components\Proxy;

class Proxy implements IUserProxy {
	
	function get($id) {
		$db = \Components\Register::get('slave');
		$db->query('select name from users where id ='.$id);
	}
	
	function set($id, $name) {
		$db = \Components\Register::get('master');
		$db->query("update users set name=$name where id =$id");
	}
}



IUserProxy.php:
<?php

namespace Components\Proxy;

interface IUserProxy {
	
	function get();
	
	function set();
}


调用:
/* 代理模式  适用于类似主从 */
$proxy = new Proxy();
$proxy->get('1');
$proxy->set('1', 'php');



数据对象映射模式

将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作。

用户信息映射示例:
vim User.php

<?php

namespace App\Model;

/**
 * 数据映射模式
 * @author YUNDUAN
 *
 */
class Users {
	
	public $user_id;
	public $name;
	public $reg_time;
	
	public $db;
	
	function __construct($user_id) {
		
		$this->db = new \Components\DatabaseAdapter\Mysqli();
		$this->db->connect('127.0.0.1', 'root', '123', 'test');
		$res = $this->db->query('select * from users where user_id = '.$user_id);
		$userInfo = $res->fetch_assoc();
		
		$this->user_id = $userInfo['user_id'];
		$this->name = $userInfo['name'];
		$this->reg_time = $userInfo['reg_time'];
		
	}
	
	function __destruct() {
		$this->db->query("update users set name='{$this->name}', reg_time={$this->reg_time} where user_id = {$this->user_id}");
	}
}

配合前面的工厂和注册模式调用示例:
在工厂类加入getUser方法
	public static function getUser($user_id) {
		
		$key = 'user_'.$user_id;
		$user = Register::get($key);
		if (!$user) {
			$user = new \App\Model\Users($user_id);
			Register::set($key, $user);
		}
		
		return $user;
	}

调用:
/* 数据映射模式 */
class Home {
        function index() {
		$user = \Components\Factory::getUser(1);
		$user->name = 'yunduan';
	}
	
	function regTime() {
		$user = \Components\Factory::getUser(1);
		$user->reg_time = $_SERVER['REQUEST_TIME'];
	}
}

$home = new Home();
$home->index();


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐