Design Mode-Proxy mode [Proxy Pattern]
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. Then the agent will always know what the agent (Real customer) can do and what it cannot do, that's two people.Same interfaceAlthough 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 following code:
First define a type of women:
Public interface KindWomen {// What can a woman of this type do? Public void makeEyesWithMan (); // public void happyWithMan (); // happy what? You know that! }
A type, it must be an interface, and then defines Pan Jinlian:
Public class PanJinLian implements KindWomen {public void happyWithMan () {System. out. println (Pan Jinlian is working with a man .....);} public void makeEyesWithMan () {System. out. println (Pan Jinlian eye-catching );}}
Set another ugly Wang Po:
Public class WangPo implements KindWomen {private KindWomen kindWomen; public WangPo () {// by default, this is the proxy of Pan Jinlian. kindWomen = new PanJinLian ();} // she can be the proxy of any KindWomen woman, as long as you are the public WangPo (KindWomen kindWomen) {this. kindWomen = kindWomen;} public void happyWithMan () {this. kindWomen. happyWithMan (); // you are old and cannot do it. You can replace public void makeEyesWithMan () {this. kindWomen. makeEyesWithMan (); // Wang Po is so old, Who should look at her ?! }}
The two heroines are playing, and the hero should also appear:
Public class XiMenQing {public static void main (String [] args) {WangPo wangPo = new WangPo (); // call out Wang Po. // 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 wangPo was actually the best. happyWithMan ();}}
This is a living example,A certain purpose is achieved through an agentIf we really remove the middle link of Wang Po, We will hook Ximen Qing and Pan Jinlian. It is estimated that it is difficult to achieve the Wusong killing event.
After this story, the proxy mode mainly uses Java polymorphism,The work is done by the proxy class, and the proxy class is mainly engagedYou asked me to work. Well, I handed it to the class behind the scenes. If you are satisfied, how can you know whether the class can be used by the agent? The same root is the same interface.