Design mode---proxy mode

Source: Internet
Author: User

What is proxy mode? Composition: Abstract Role: A business method that declares a real role through an interface or abstract class. Proxy role: the implementation of abstract role, is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations. Real role: Implement abstract roles, define the business logic that the real role will implement, and be called by the proxy role. Proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object. To the margin of the case to explain: for example, Simon Qing find pan Jinlian, that pan Jinlian embarrassed reply ah, what to do, find the king of the agent, performance in the program, such as: first define a type of woman:
 PackageCom.oumyye. Proxy mode;/*** @authoroumyye* I ' m glad to share my knowledge and you all.* define a type of woman, Lady Wang and Pan Jinlian all belong to this type of woman*/ Public InterfaceKindwomen {//what can a woman of this type do?  Public voidMakeeyeswithman ();//throw a wink . Public voidHappywithman ();//Happy what? You know that!}
One type, that must be the interface, then define PAN Jinlian:
 Package Com.oumyye. Proxy mode;  Public class Implements kindwomen {publicvoid  Happywithman () {System. Out.println ("Pan Jinlian is doing that with a man ...") ); }publicvoid  Makeeyeswithman () {System. Out.println ("Pan Jinlian wink");}}
and set an ugly king:
 PackageCom.oumyye. Proxy mode; Public classWangpoImplementsKindwomen {Privatekindwomen kindwomen; PublicWangpo () {//by default, it's Pan Jinlian's agent.  This. Kindwomen =NewPanjinlian ();}//She can be the agent of any woman in Kindwomen, as long as you are of this type PublicWangpo (kindwomen kindwomen) { This. Kindwomen =kindwomen;} Public voidHappywithman () { This. Kindwomen.happywithman ();//I am old, can not do, let the young replace } Public voidMakeeyeswithman () { This. Kindwomen.makeeyeswithman ();//Wang is such a big age, who watch her wink?!  }}
Two heroine are on the pitch, the hero should appear:
 PackageCom.oumyye. Proxy mode; Public classximenqing {/** The river is written in this way: Simon Qing was pan Jinlian with a bamboo stick knocked a bit is that, obsessed with, * by the king to see, began to set up two good things, Wang Po as Pan Jinlian Agent * received a lot of facilitation payments, then we assume: * If there is no Wang in the middle of the strings, these two do not face can become? It's hard to say! */ Public Static voidMain (string[] args) {//call the king.Wangpo Wangpo =NewWangpo ();//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 JinlianWangpo.happywithman ();}}

That is a living example, through the agent to achieve a certain purpose, if really remove the link between Wang, is directly Simon Qing and pan Jinlian hooking up, it is 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 (the one with the solid housekeeper fornication >), the name of: "If", then we also let Wang do her agent:
Sketch out the Jia:

 Package Com.oumyye. Proxy mode;  Public class Implements kindwomen {publicvoid  Happywithman () {System. Out.println ("Chia is happy ..." ); }publicvoid  Makeeyeswithman () {System. Out.println ("Chia wink");}}

Ximen Qing Zhangjia:

 package   Com.oumyye. Proxy mode;  public  class   XiMenQing2 { static  void   main (string[] args) {  //  adapted history, chia by Simon Qing:  Jiashi Jiashi = new   Jiashi (); Wangpo Wangpo  = new  Wangpo (Jiashi); //   Wangpo.makeeyeswithman (); Wangpo.happywithman ();}}  

Finish this story, that sum up, the proxy mode is mainly used by the Java polymorphism, the work is the agent class, Agent class is mainly rejoined, you let me work, good, I give the behind the scenes of the class to do, you satisfied with, then how to know the agent can do? The same root, we know everything, you can do what, I can do what is clear, the same interface Bai.

Design mode---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.