7-proxy Mode

Source: Internet
Author: User

7-proxy Mode
Zookeeper

Fall in love. But how to chase the goddess? Have you tried the proxy mode? Ah, what? Is Chasing goddess still related to the pattern? Of course, let me learn the proxy mode together.
1. What is proxy mode?
The proxy mode provides a proxy for other objects to control access to this object. A bit of interface. It's actually just A man in the middle. A and B know each other, B and C know each other, and A doesn't know C. How can A catch up with C? B Bei.
Ii. code example (chasing Goddess)
Main. java
Public class Main {
Public static void main (String [] args ){
Nvshen nvshen = new Nvshen ();
Nvshen. setName ("Shangguan xiaotong ");
Proxy proxy = new Proxy (nvshen );
Proxy. giveChocolate ();
Proxy. giveDoll ();
Proxy. giveFlower ();
}
}

Pursue. java
Public interface Pursue {
Void giveDoll ();
Void giveFlower ();
Void giveChocolate ();
}

Nvshen. java
Public class Nvshen {
Private String name;

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

}

Pursuer. java
Public class Pursuer implements Pursue {
Private Nvshen mynvshen;

Public Pursuer (Nvshen nvshen ){
This. mynvshen = nvshen;
}

Public void giveDoll (){
// TODO Auto-generated method stub
System. out. println ("Dear Goddess," + mynvshen. getName ()
+ ". I am your top fan. This is a doll for you. ");
}

Public void giveFlower (){
// TODO Auto-generated method stub
System. out. println ("Dear Goddess," + mynvshen. getName ()
+ ". I am your top fan, and this is the rose. ");
}

Public void giveChocolate (){
// TODO Auto-generated method stub
System. out. println ("Dear Goddess," + mynvshen. getName ()
+ ". I am your top fan. This is the chocolate. ");
}

}

Proxy. java
Public class Proxy implements Pursue {
Pursuer p;
Public Proxy (Nvshen nvshen ){
P = new Pursuer (nvshen );
}
Public void giveDoll (){
// TODO Auto-generated method stub
P. giveDoll ();
}

Public void giveFlower (){
// TODO Auto-generated method stub
P. giveFlower ();
}

Public void giveChocolate (){
// TODO Auto-generated method stub
P. giveChocolate ();
}

}
Iii. Summary
I have known the word proxy before, and I have also used proxy, but it is a proxy on the network. There is also what sales agents are in reality. In fact, good code means that. Man in the middle. The advantage is that one class can communicate with another unrelated class, so that the source code is not modified.

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.