PHP design mode-proxy mode _ PHP Tutorial

Source: Internet
Author: User
PHP design mode-proxy mode. PHP design pattern-Proxy pattern Statement: This series of blog reference material "big talk design pattern", author Cheng Jie. Proxy mode provides a proxy for other objects to control the PHP design mode for this object-proxy mode

Disclaimer: Cheng Jie, author of this series of blog references "big talk Design Mode.

The proxy mode provides a proxy for other objects to control access to this object. In some cases, an object is not suitable or cannot directly reference another object, and the proxy object can play a mediation role between the client and the target object.

UML class diagram:

Role introduction:

Abstract topic role (IGiveGift): defines the Follower and Proxy public interfaces so that Proxy can be used wherever Follower is used.

Follower: defines the real entity represented by Proxy.

Proxy: saves a reference so that the Proxy can access the object and provides the same interface as the Follower interface, so that the Proxy can be used to replace the Follower ).

Code implementation:

 GirlName = $ name;} function giveRose () {echo "{$ this-> girlName}: this is my rose. hope you like it. ";} Function giveChocolate () {echo" {$ this-> girlName}: this is the chocolate I sent you. I hope you can accept it. ";}}/** Proxy * Class Proxy */class Proxy implements IGiveGift {private $ follower; function _ construct ($ name = 'Girl ') {$ this-> follower = new Follower ($ name);} function giveRose () {$ this-> follower-> giveRose ();} function giveChocolate () {$ this-> follower-> giveChocolate ();}}

Client code:

Header ("Content-Type: text/html; charset = utf-8"); // ------------------------ test code in proxy mode ------------------ require_once ". /Proxy. php "; $ proxy = new Proxy ('Fan Bingbing '); $ proxy-> giveRose (); $ proxy-> giveChocolate ();

Advantages:

1. clear responsibilities

The real role is to implement the actual business logic, so you do not need to care about other transactions that are not in this responsibility. you can complete a transaction through a later Agent. The accompanying result is that the programming is concise and clear.

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

3. high scalability

Applicable scenarios:

1) Remote Proxy provides a local Proxy object for objects in different address spaces.

This different address space can be in the same host, but in another host, remote proxy is also called Ambassador)
2) Virtual Proxy creates objects with high overhead as needed.

If you want to create an object that consumes a large amount of resources, first create an object that consumes a relatively small amount of resources. a real object is created only when necessary.
3) Protection Proxy controls access to the original object.

Protection proxy is used when the object should have different access permissions.
4) Smart Reference replaces simple pointers and performs some additional operations when accessing objects.
5) Copy-on-Write proxy: this is a virtual proxy that delays the replication (clone) operation until it is actually needed by the client.

In general, object deep cloning is a costly operation. the Copy-on-Write proxy can delay this operation and the object will be cloned only when it is used.

PHP object-oriented design model

Author's statement: Cheng Jie, author of this series of blog references "big talk Design Mode. Proxy mode provides a proxy for other objects to control this object...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.