zoukankan      html  css  js  c++  java
  • 代理模式

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

    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();

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

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


  • 相关阅读:
    线程的创建和运行
    Spring SpringMvc Mybatis Maven整合
    使用 Redis 缓存来实现用户最近浏览的商品列表
    Java泛型
    HDFS的HA集群原理分析
    MapReduce-自动化运行配置
    大数据-HDFS 集群搭建的配置文件
    鼠标线状移动特效
    Mac 终端 Tomcat 环境配置过程
    oracle列转行
  • 原文地址:https://www.cnblogs.com/paulversion/p/8426676.html
Copyright © 2011-2022 走看看