设计模式--代理模式

2021-05-02

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

<?php

interface ProxyInterface
{
	public function query($sql);
}

class Proxy implements ProxyInterface
{
	public function query($sql)
	{
		if (substr($sql, 0, 6) == 'select') {
            echo "读操作:$sql<br />";
        } else {
            echo "写操作:$sql<br />";
        }
	}
}

$proxy = new Proxy();
$proxy->query('show databses');
$proxy->query('select * from user');

 

{/if}