Proxy mode in php design mode

Source: Internet
Author: User
Proxy pattern in php design mode [proxy pattern]

Header ("Content-type: text/html; charset = UTF-8 ");
/**
* What is the proxy mode? I am very busy. I am not busy with you. if you want to contact me, please first look for my agent,
* The agent must always know what the agent can do and what cannot do. that is, two people have the same interface,
* Although the agent cannot work, the agent can work.
*/

// For example, if Simon Qing is looking for Pan Jinlian, Pan Jinlian is embarrassed to reply. what should I do? look for the Wang Po as an agent, as shown in the program:

// Define a type of woman first:
/**
* Defines a type of women. both Wang Po and Pan Jinlian belong to this type of women.
*/
Interface KindWomen {
// What can a woman of this type do?
Public function makeEyesWithMan (); // give an eye
Public function happyWithMan (); // happy what? You know that!
}



/**
* Defines what kind of person Pan Jinlian is.
*/
Class PanJinLian implements KindWomen {

Public function makeEyesWithMan (){
Echo ("Pan Jinlian eye-catching ");
}

Public function happyWithMan (){
Echo ("Pan Jinlian is working with a man .....");
}
}


// Set another ugly Wang Po:

/**
* Wang Po is an old smart man. she is too old to be a man,
* But she has wisdom and experience. she acts as a proxy for a type of women!
*/
Class WangPo implements KindWomen {
Private $ kindWomen;

/**
* She can be the agent of any KindWomen woman, as long as you are the agent of this type
* By default, this is Pan Jinlian's proxy.
* @ Param null | KindWomen $ kindWomen
*/
Public function _ construct ($ kindWomen = null ){
If (is_object ($ kindWomen) | $ kindWomen instanceof KindWomen ){
$ This-> kindWomen = $ kindWomen;
} Else {
$ This-> kindWomen = new PanJinLian ();
}
}

Public function makeEyesWithMan (){
$ This-> kindWomen-> makeEyesWithMan (); // Who cares if she is so old ?!
}

Public function happyWithMan (){
$ This-> kindWomen-> happyWithMan (); // If you are old, you cannot do it. you can replace it with a young one.
}
}


// The two heroines are playing, and the hero should also appear:

/**
* Define a Simon Qing. this person is hungry.
*/
Class XiMenQing {

/**
* This is written in the figure: Simon Qing was knocked on by Pan Jinlian with a bamboo. is it so obsessed,
* When Wang saw this, he began to merge the two good deeds. Wang was the agent of Pan Jinlian.
* If you have received a lot of benefits, let's assume:
* If there is no Wang Po in the middle, can these two faces become shameless? It's hard to say!
*/
Public function _ construct (){
// Call Wang Po
$ WangPo = new WangPo ();

// Then Simon said, "I want to have a happy time with Pan Jinlian. then Wang arranged the scene where Simon Qing lost chopsticks:
$ WangPo-> makeEyesWithMan (); // No. although Wang Po was doing this on the surface, what is really nice is Pan Jinlian.
Echo"



";
$ WangPo-> happyWithMan ();
Echo"



";

// Adapted the history. James was checked out by Ximen Qing:
$ JiaShi = new JiaShi ();
$ WangPo = new WangPo ($ jiaShi); // let Wang Po act as Jia's agent
$ WangPo-> makeEyesWithMan ();
Echo"



";
$ WangPo-> happyWithMan ();
Echo"



";
}
}


/*
* This is a living example. an agent can achieve a certain purpose. if the intermediary link of Wang Po is removed, it is the West.
* Menqing and Pan Jinlian hooked up, and it is estimated that it is difficult to achieve the Wusong killing event.
* So let's think about it again. Is there any other women in the queue? Yes, Lu Junyi's wife Jia (that is, he lives with the gubutler
*), The name is "if", then we also let Wang Po act as her proxy:
*/

// Sketch Jia:
Class JiaShi implements KindWomen {

Public function makeEyesWithMan (){
Echo ("Jia's eye-catching ");
}

Public function happyWithMan (){
Echo ("Jia is in Happy state ......");
}
}


// Ximen Qing Gou Jia's: XiMenQing

New XiMenQing ();



The running result is as follows:

Pan Jinlian glared at me



Pan Jinlian is working with a man .....



Jia's eye-catching



Jia is in Happy state ......

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.