Design Pattern (2) --- Proxy Pattern, proxypattern
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 will always know
What can the agent do and cannot do? That is, two people have the same interface. Although the agent cannot work
Agents 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 following code:
First define a type of woman (Interface ):
Package com. fc. Proxy; public interface KindWoman {public void makeEyesWithMan (); // eye-catching}
Then define Pan Jinlian:
Package com. fc. Proxy; public class PanJinlian implements KindWoman {public void makeEyesWithMan () {System. out. println ("Pan Jinlian in the eye... ");}}
Let's have another Wang Po, that is, Pan Jinlian's agent:
Package com. fc. proxy; public class WangPo implements KindWoman {private KindWoman kindwoman; public WangPo () {this. kindwoman = new PanJinlian (); // Wang Po is the proxy of Pan Jinlian by default} // Wang Po can also be the proxy of any other KindWomen woman, as long as you are of this type public WangPo (KindWoman kindWomen) {this. kindwoman = kindWomen;} public void makeEyesWithMan () {this. kindwoman. makeEyesWithMan (); // who sees her eye-catching as old as Wang Po ?! She replaced others }}
Even a female pig's foot has already appeared, and we will see Ximen celebration below:
Package com. fc. proxy; public class XiMenQing {public static void main (String [] args) {// name Wang Po WangPo wangPo = new WangPo (); // then Simon said, I want to be happy with Pan Jinlian, and then Wang Po arranged the scene where Simon Qing lost chopsticks: wangPo. makeEyesWithMan (); // No. Although Wang Po was doing this on the surface, Pan Jinlian was actually doing it }}
Now, Wang Po has finished acting for Ximen Qing. If she cannot say that she has to do so, let's assume that Pan Jinlian 2 is coming again:
Package com. fc. Proxy; public class PanJinlian222 implements KindWoman {public void makeEyesWithMan () {System. out. println ("Pan Jinlian 222 in the eye... ");}}
Then Simon Qing came to Wang Po to help seduce Pan Jinlian 2: Modify Simon Qing:
Package com. fc. proxy; public class XiMenQing {public static void main (String [] args) {/* // name Wang Po WangPo wangPo = new WangPo (); // then Simon said, I want to be happy with Pan Jinlian, and then Wang Po arranged the scene where Simon Qing lost chopsticks: wangPo. makeEyesWithMan (); // No. Although Wang Po was doing this on the surface, what is actually nice is Pan Jinlian */WangPo wangPo = new WangPo (new PanJinlian222 ()); // This is not the default proxy. You need to pass the wangPo parameter to it. makeEyesWithMan ();}}
To sum up, the proxy mode mainly uses Java polymorphism, working on the proxy class, and the proxy class is mainly
Take over, you let me work, well, I will give it to the class behind the scenes, you will be satisfied with it, then how do you know whether the class can be done by the agent? With the same root,
Everyone knows what you can do. What I can do is clearly the same interface.
From: Design Model