PHP Implementation Proxy mode

Source: Internet
Author: User

Proxy pattern: Constructs an object that is transparently placed within two different objects, enabling the interception or brokering of communication or access between the two objects.

(a) Why proxy mode is required

1, the remote agent, that is, for an object in different address space to provide local representation. Hides the fact that an object exists in a different address space.

2, virtual agent, as needed to create a very expensive object, through it to hold the instantiation of the real object that takes a long time.

3, a security agent that controls the access objects of real objects.

4, smart guidance, when invoking real objects, the agent handles some things.

(ii) proxy mode UML diagram

(c) Simple examples

Case one: you want to buy a new album of alumni brother, you used to buy in the county CD store. Now the CD industry is in a slump, not sold. You can only go to Hong Kong to help you buy a card.

<?phpProxy Abstraction InterfaceInterfaceshop{PublicfunctionBuy($title);}The original CD store, the object being proxiedClassCdshopImplementsshop{PublicfunctionBuy($title) {Echo"The purchase is successful, this is your {$title}" record. Php_eol; }}CD AgentClassProxyImplementsshop{public  function buy ($title) { $this->go (); $CDshop = new cdshop; $CDshop->buy ($title);} public function go () {echo " Run to Hong Kong shopping ". Php_eol; }}//you bought a goodbye kiss in 93 $cdshop = new cdshop; $CDshop->buy ( "kiss Goodbye"); //14 year you want to buy a wake up dream can not find the CD shop, and dreaming like, have to find an agent to Hong Kong to help you purchase. $proxy = new proxy; $proxy->buy (    

Case two: Through the agent to realize the read and write separation of MySQL, if it is read operation, connected to the 127.0.0.1 database, write operations to read the 127.0.0.2 database

<?phpClassproxy{protected $reader;protected $wirter;Publicfunction__construct(){$this->reader =New PDO (' Mysql:host=127.0.0.1;port=3306;dbname=cd; ',' Root ',' Password ');$this->writer =New PDO (' Mysql:host=127.0.0.2;port=3306;dbname=cd; ',' Root ',' Password '); }PublicfunctionQuery($sql) {if (substr ($sql,0,6) = =' SELECT ') {echo  "read operation:". Php_eol; return  $this->reader->query ($sql);} else {echo " write operation: ". Php_eol; return  $this->writer->query ($sql);}} //database Agent $proxy = new proxy; Read Operation $proxy->query ( "SELECT * from table");  "INSERT into table SET title = ' Hello ' where id = 1"); //of course for the database, here should use a singleton mode to store $reader and $writer, but I just for example, do not want to add a single case to complicate the code. //but if you want to implement such a database agent, I think it's necessary to use the knowledge of the singleton mode         

In a word, the proxy mode is to access the object you want to access by using a proxy object when accessing the object. In the proxy object, we can implement the truncation or permission control of the Access object.

PHP Implementation Proxy mode

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.