<?php
/**
* 抽象主题角色(Subject):定义了RealSubject和Proxy公用接口,这样就在任何使用RealSubject的地方都可以使用Proxy。
* 真正主题角色(RealSubject):定义了Proxy所代表的真实实体。
* 代理对象(Proxy):保存一个引用使得代理可以访问实体,并提供一个与RealSubject接口相同的接口,这样代理可以用来代替实体(RealSubject)。
*/
header("Content-type:text/html;Charset=utf-8");
/**
* Interface Subject
* RealSubject和Proxy共同具备的东西
*/
interface Subject
{
public function say();
public function run();
}
class RealSubject implements Subject
{
private $_name;
public function __construct($name)
{
$this->_name = $name;
}
public function say()
{
echo $this->_name . "在跑步<br>";
}
public function run()
{
echo $this->_name . "在说话<br>";
}
}
class Proxy implements Subject
{
private $_realSubject = null;
public function __construct(RealSubject $realSubject = null)
{
if (empty($realSubject)) {
$this->_realSubject = new RealSubject();
} else {
$this->_realSubject = $realSubject;
}
}
public function say()
{
$this->_realSubject->say();
}
public function run()
{
$this->_realSubject->run();
}
}
class Client
{
public static function test()
{
$subject = new RealSubject("张三");
$proxy = new Proxy($subject);
$proxy->say();
$proxy->run();
}
}
Client::test();
<?php
/**
* 通过代理实现MySQL的读写分离,如果是读操作,就连接127.0.0.1的数据库,写操作就读取127.0.0.2的数据库
*/
class Proxy
{
protected $reader;
protected $wirter;
public function __construct()
{
$this->reader = new PDO('mysql:host=127.0.0.1;port=3306;dbname=CD;', 'root', 'password');
$this->writer = new PDO('mysql:host=127.0.0.2;port=3306;dbname=CD;', 'root', 'password');
}
public function query($sql)
{
if (substr($sql, 0, 6) == 'select') {
echo "读操作: " . PHP_EOL;
return $this->reader->query($sql);
} else {
echo "写操作:" . PHP_EOL;
return $this->writer->query($sql);
}
}
}
//数据库代理
$proxy = new Proxy;
//读操作
$proxy->query("select * from table");
//写操作
$proxy->query("INSERT INTO table SET title = 'hello' where id = 1");
<?php
/**
* 这个用的是代理模式,需要比较一下装饰器模式
* 不使用的话,繁琐
*/
/**
* 你需要登陆站点,然后进行创建帖子,验证帖子,编辑帖子等等。如果你没有登陆,那么你应该直接到登陆界面。
* 而且BlogPost类,应该仅负责管理帖子。验证和辨别身份应当是分离的。我们违反了“单一职责原则”。
*/
class BlogPost extends CI_Controller
{
public function createPost() {
if (!Authentication::checkAuthentication) {
// redirect to login
} else {
// proceed
Message::notifyAdmin();
}
}
public function editPost() {
if (!Authentication::checkAuthentication) {
// redirect to login
} else {
// proceed
Message::notifyAdmin();
}
}
public function viewPost() {
if (!Authentication::checkAuthentication) {
// redirect to login
} else {
// proceed
Message::notifyAdmin();
}
}
}
/**
* 前置通知
*/
class PathController1
{
function controlPaths($className, $funcName) {
Authentication::checkAuthentication();
$classObj = new $className();
$classObj->$funcName();
}
}
/**
* 后置通知
*/
class PathController2
{
function controlPaths($className, $funcName) {
$classObj = new $className();
$classObj->$funcName();
Database::closeConnection();
}
}
/**
* 抛出通知
*/
class PathController3
{
function controlPaths($className, $funcName) {
try {
$classObj = new $className();
$classObj->$funcName();
}
catch (Exception $e) {
Error::reportError();
}
}
}
/**
* 环绕通知
*/
class PathController4
{
function controlPaths($className, $funcName) {
Logger::startLog();
$classObj = new $className();
$classObj->$funcName();
Logger::endLog();
}
}
<?php
/**
* 装饰器模式又叫装饰者模式。
* 装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。
* 它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
* 组件对象的接口:可以给这些对象动态的添加职责
* 所有装饰器的父类:需要定义一个与组件接口一致的接口,并持有一个Component对象,该对象其实就是被装饰的对象。
* 具体的装饰器类:实现具体要向被装饰对象添加的功能。用来装饰具体的组件对象或者另外一个具体的装饰器对象。
*/
/**组件对象接口
* Interface IComponent
*/
interface IComponent
{
function Display();
}
/**待装饰对象
* Class Person
*/
class Person implements IComponent
{
private $name;
function __construct($name)
{
$this->name=$name;
}
function Display()
{
echo "装扮的:{$this->name}<br/>";
}
}
/**所有装饰器父类
* Class Clothes
*/
class Clothes implements IComponent
{
protected $component;
function Decorate(IComponent $component)
{
$this->component=$component;
}
function Display()
{
if(!empty($this->component))
{
$this->component->Display();
}
}
}
//------------------------------具体装饰器----------------
class PiXie extends Clothes
{
function Display()
{
echo "皮鞋 ";
parent::Display();
}
}
class QiuXie extends Clothes
{
function Display()
{
echo "球鞋 ";
parent::Display();
}
}
class Tshirt extends Clothes
{
function Display()
{
echo "T恤 ";
parent::Display();
}
}
class Waitao extends Clothes
{
function Display()
{
echo "外套 ";
parent::Display();
}
}
$Yaoming=new Person("姚明");
$pixie=new PiXie();
$waitao=new Waitao();
$pixie->Decorate($Yaoming);
$waitao->Decorate($pixie);
$waitao->Display();