There is a saying that "there is something for the secretary to do, nothing for the secretary ". In fact, the secretary is a typical proxy in life. To put it bluntly, the customer doesn't talk about business because of the secretary, but the boss. However, the boss has a lot of business, so he uses a secretary to act as a proxy. So in Baidu Baike, I explained this understanding of the proxy mode: in some cases, one object is not suitable or cannot directly reference another object, proxy objects can play a mediation role between the client and the target object. The intermediary explains that the proxy class has no implementation function, but it only has an additional layer of packaging. The function is not as fast as directly referencing objects. However, this relationship is very common.
As one of the most common design patterns, the proxy mode must be understood. Because proxy mode is everywhere. In the SSH framework, each framework uses the proxy mode. The essence of AOP in spring is the proxy mode.
There are two types of proxy: static proxy and dynamic proxy. First of all, the proxy is built on the Interface relationship.
For example, I am very busy but need to buy a bus ticket. So I can ask the Secretary to do it.
Example of writing a static Proxy:
// This is the ticket Interface
PackageCom. Fish. test;
Public
InterfaceTicket {
Public
VoidShow ();
}
// This is a bus ticket
Public
ClassBusticketImplementsTicket {
@ Override
Public
VoidShow (){
System.Out. Println ("bus pass ");
}
}
// Purchase tickets through proxy
PackageCom. Fish. test;
Public
ClassMyproxyImplementsTicket {
PrivateTicket
Ticket;
PublicMyproxy (Ticket ){
Super();
This. Ticket = ticket;
}
@ Override
Public
VoidShow (){
Ticket. Show ();
}
Public
VoidBuy (){
System.Out. Print ("agent helped me buy :");
Show ();
}
}
// Write a test method
Public
ClassTest {
Public
Static voidMain (string [] ARGs ){
// Create a ticket using polymorphism first
Ticket =NewBusticket ();
// Send this ticket to the agent
Myproxy =NewMyproxy (ticket );
// Purchase by proxy
Myproxy. Buy ();
}
}
Result Display
The proxy helped me buy the Proxy: bus ticket.
How can I use dynamic proxy? It is actually very simple. First, what does the Proxy do? To put it bluntly, you have created a bus ticket and called a bus ticket.
Dynamic proxy is implemented by reflection. In Java, there are several methods in the proxy class proxy. You can check the API and I will not talk about it. We need to implement the handle object of a reflection method. Use this class to create an instance. See the Code. If other codes remain unchanged, the proxy class is modified.
PackageCom. Fish. test;
ImportJava. Lang. Reflect. invocationhandler;
ImportJava. Lang. Reflect. method;
ImportJava. Lang. Reflect. proxy;
Public
ClassProxydyImplementsInvocationhandler {
PrivateObject
OBJ; // accepts an object from the external
PublicProxydy (){
System.Out. Println ("the proxy helped me buy it :");
}
// This is a binding method.
PublicObject BIND (Object delegate ){
This. OBJ = delegate; // returns an external object to OBJ.
ReturnProxy.Newproxyinstance(Delegate. getclass (). getclassloader (),
Delegate. getclass (). getinterfaces (),This); // Here, the Java proxy class, which means creating a proxy instance. The first parameter is the class loader for obtaining the external object, and the second parameter is the interface for obtaining the object. This is an instance of the invocationhandler interface. Isn't this the object itself.
}
@ Override
PublicObject invoke (Object proxy, method, object [] ARGs)
ThrowsThrowable {
// This is a reflection method. You need to reflect the OBJ object. All methods of OBJ are included in the args.
Method. Invoke (OBJ, argS );
Return
Null;
}
}
Write a test class below
PackageCom. Fish. test;
ImportJava. Lang. Reflect. invocationhandler;
ImportJava. Lang. Reflect. method;
ImportJava. Lang. Reflect. proxy;
Public
ClassTest2 {
Public
Static voidMain (string [] ARGs ){
// Create a proxy
Proxydy proxy =NewProxydy ();
// Call an agent to buy tickets
Ticket = (ticket) proxy. BIND (NewBusticket ());
// The agent buys the ticket
Ticket. Show ();
}
}
The result is:
The agent helped me buy a bus ticket
In fact, dynamic proxy is not difficult. I can use a piece of code to implement the above two classes.
FinalTicket =
NewBusticket ();
Ticket ticket2 = (ticket) proxy.Newproxyinstance(Ticket. getclass ()
. Getclassloader (), ticket. getclass (). getinterfaces (),
NewInvocationhandler (){
@ Override
PublicObject invoke (Object proxy, method,
Object [] ARGs)ThrowsThrowable {
Method. Invoke (ticket,Null);
Return
Null;
}
});
Ticket2.show ();
This code can implement the code of the above two classes. The only thing I want to talk about is that dynamic proxy is implemented through the prxoy class of Java itself. In fact, we can also use reflection to implement our own dynamic proxy.