代理模式

代理模式:为其他对象提供一种代理以控制对这个对象的访问

interface GiveGift
{

function giveDolls();
function giveFlowers();
function giveChocolate();

}

class Pretty
{

public $name;

public function __construct($name)
{
$this->name = $name;
}

}

class Pursuit implements GiveGift
{
private $pretty ;

public function __construct($pretty)
{
$this->pretty = $pretty;
}

public function giveDolls()
{
echo $this->pretty->name. '送你洋娃娃<br>';
}

public function giveFlowers()
{
echo $this->pretty->name. '送你花<br>';
}

public function giveChocolate()
{

echo $this->pretty->name. '送你巧克力<br>';
}


}

class Proxy implements GiveGift
{

private $pursuit;

public function __construct($pretty)
{
$this->pursuit = new Pursuit($pretty);
}

public function giveDolls()
{
$this->pursuit->giveDolls();
}

public function giveFlowers()
{
$this->pursuit->giveFlowers();
}


public function giveChocolate()
{
$this->pursuit->giveChocolate();
}
}

$pretty = new Pretty('lucy');

$proxy = new Proxy($pretty);

$proxy->giveDolls();
$proxy->giveFlowers();
$proxy->giveChocolate();

代理模式应用:代理一般分为四种:

远程代理:也就是为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在于不同地址空间
虚拟代理:根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真实对象
安全代理:用来控制真实对象访问时的权限
智能指引:当调用真实的对象时,代理处理另外一些事


原文地址:https://www.cnblogs.com/paulversion/p/8426676.html