Home > Backend Development > PHP Tutorial > PHP Design Patterns - Proxy Pattern_PHP Tutorial

PHP Design Patterns - Proxy Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 09:53:34
Original
689 people have browsed it

PHP Design Pattern - Proxy Pattern

Statement: The reference material for this series of blogs is "Dahua Design Pattern", written by Cheng Jie.

The proxy pattern provides a proxy for other objects to control access to this object. In some cases, one object is not suitable or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object.

UML class diagram:

Character introduction:

Abstract theme role (IGiveGift): Defines the public interfaces of Follower and Proxy, so that Proxy can be used wherever Follower is used.

Theme role (Follower): defines the real entity represented by Proxy.

Proxy object (Proxy): Saves a reference so that the proxy can access the entity, and provides an interface the same as the Follower interface, so that the proxy can be used instead of the entity (Follower).

Code implementation:

<!--?php
/**
 * Created by PhpStorm.
 * User: LYL
 * Date: 2015/5/16
 * Time: 16:33
 */

/**顶层接口
 * Interface IGiveGift
 */
interface IGiveGift
{
    function giveRose();
    function giveChocolate();
}

/**追求者
 * Class Follower
 */
class Follower implements IGiveGift
{
    private $girlName;

    function __construct($name=&#39;Girl&#39;)
    {
        $this--->girlName=$name;
    }

    function giveRose()
    {
        echo "{$this->girlName}:这是我送你的玫瑰,望你能喜欢。
";
    }

    function giveChocolate()
    {
        echo "{$this->girlName}:这是我送你的巧克力,望你能收下。
";
    }
}

/**代理
 * Class Proxy
 */
class Proxy implements IGiveGift
{
    private $follower;

    function __construct($name=&#39;Girl&#39;)
    {
        $this->follower=new Follower($name);
    }

    function giveRose()
    {
        $this->follower->giveRose();
    }

    function giveChocolate()
    {
        $this->follower->giveChocolate();
    }
}
Copy after login

Client code:

header("Content-Type:text/html;charset=utf-8");
//------------------------代理模式测试代码------------------
require_once "./Proxy/Proxy.php";
$proxy=new Proxy(&#39;范冰冰&#39;);
$proxy->giveRose();
$proxy->giveChocolate();
Copy after login

advantage:

1. Clear responsibilities

The real role is to implement the actual business logic. You don’t have to worry about other matters that are not your responsibilities. You can complete a completed transaction through the later agent. The accompanying result is that the programming is simple and clear.

2. The proxy object can act as an intermediary between the client and the target object, thus mediating and protecting the target object.

3. High scalability

Applicable scenarios:

1) Remote Proxy provides a local proxy object for an object located in a different address space.

This different address space can be on the same host or on another host. The remote agent is also called Ambassador
2) Virtual Proxy creates expensive objects as needed.

If you need to create an object that consumes a large amount of resources, first create an object that consumes a relatively small amount of resources. The real object will only be created when needed.
3) Protection Proxy controls access to the original object.

Protection proxies are used when objects should have different access rights.
4) Smart Reference replaces the simple pointer and performs some additional operations when accessing the object.
5) Copy-on-Write agent: It is a type of virtual agent that delays the copy (clone) operation until it is executed only when the client really needs it.

Generally speaking, deep cloning of an object is an expensive operation. The Copy-on-Write proxy can delay this operation and the object will only be cloned when it is used.

PHP object-oriented design patterns

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000541.htmlTechArticlePHP Design Pattern - Proxy Pattern Statement: The reference material for this series of blogs is "Dahua Design Pattern", author Cheng Jie. The proxy pattern provides a proxy for other objects to control this object...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template