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.