Proxy mode of PHP design mode

Source: Internet
Author: User

Agent Mode "proxy pattern"

<?php

Header ("content-type:text/html; Charset=utf-8 ");
/**
* What is proxy mode? I am very busy, busy no time to talk to you, then you want to find me, first find my agent,
* The agent always has to know what the agent can do to do something, that is, two people have the same interface,
* Although the agent can not work, but the agent of the people to work ah.
*/

For example, Simon Qing find pan Jinlian, that pan Jinlian embarrassed reply ah, how to, find that the king of the agent, performance in the program when this:

Define a type of woman first:
/**
* Define a type of woman, Wang and pan Jinlian all belong to this type of woman
*/
Interface kindwomen{
What can a woman of this type do?
Public function Makeeyeswithman (); Throw a wink.
Public function Happywithman (); Happy what? You know that!
}



/**
* Define a pan Jinlian is what kind of person
*/
Class Panjinlian implements Kindwomen {

Public Function Makeeyeswithman () {
Echo ("Pan Jinlian-wink");
}

Public Function Happywithman () {
Echo ("Pan Jinlian is doing that with a man ...");
}
}


and set an ugly king:

/**
* The man is old and clever, she is too old, a man can not see,
* But she has wisdom and experience, she acts as a kind of woman's agent!
*/
Class Wangpo implements Kindwomen {
Private $kindWomen;

/**
* She can be the agent of any woman in Kindwomen, as long as you are of this type
* By default, it is the agent of Pan Jinlian
* @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 (); Wang is such a big age, who watch her wink?!
}

Public Function Happywithman () {
$this->kindwomen->happywithman (); I am old, can not do, let the young replace
}
}


Two heroine are on the pitch, the hero should appear:

/**
* Define a Simon Qing, the Hungry Ghost in the man's color
*/
Class Ximenqing {

/**
* This is written in the margin of the river: Simon Qing was pan Jinlian with a bamboo stick knocked a bit is it, obsessed with,
When the king saw it, she began to set up two good deeds, and she was the agent of Pan Jinlian.
* After a lot of facilitation payments, let's assume that:
If there is no king in the middle of the strings, these two shameless can become? It's hard to say!
*/
Public Function __construct () {
Call the King.
$wangPo = new Wangpo ();

Then Simon Qing said, I want to and Pan Jinlian happy, and then the king will arrange the Simon Qing lost chopsticks that play:
$wangPo->makeeyeswithman (); See no, although on the surface when Wang is doing, actually cool is pan Jinlian
echo "<br><br><br><br>";
$wangPo->happywithman ();
echo "<br><br><br><br>";

Adaptation of History, chia by Simon Qing hook away:
$jiaShi = new Jiashi ();
$wangPo = new Wangpo ($jiaShi); Let her be the agent of Chia.
$wangPo->makeeyeswithman ();
echo "<br><br><br><br>";
$wangPo->happywithman ();
echo "<br><br><br><br>";
}
}


/*
* That is a living example, through the agent to achieve a certain purpose, if really remove the link between Wang, is directly west
* Menqing and Pan Jinlian, estimated that it is difficult to achieve Wu song kill sister-in-the-death incident.
* Then we'll think about it, is there any type of woman in the margin? Yes, Lu Junyi's wife Chia (with the solid housekeeper fornication
The name: "If", then we also let the King do her proxy:
*/

Sketch out the Jia:
Class Jiashi implements Kindwomen {

Public Function Makeeyeswithman () {
Echo ("Chia-wink");
}

Public Function Happywithman () {
Echo ("Chia is happy ...");
}
}


Ximen Qing Zhangjia: see above class ximenqing

New Ximenqing ();



The results of the operation are as follows:

Pan Jinlian Wink



Pan Jinlian is doing that with a man.



Chia Wink



Chia is happy ...









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.