In the most popular words, the proxy mode of 23 design modes

Source: Internet
Author: User

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
Interface
Ticket {

Public
Void
Show ();

}

 

// This is a bus ticket

 

Public
Class
BusticketImplementsTicket {

 

@ Override

Public
Void
Show (){

System.Out. Println ("bus pass ");

}

 

}

 

// Purchase tickets through proxy

PackageCom. Fish. test;

 

Public
Class
MyproxyImplementsTicket {

PrivateTicket
Ticket;

 

PublicMyproxy (Ticket ){

Super();

This. Ticket = ticket;

}

 

@ Override

Public
Void
Show (){

Ticket. Show ();

}

Public
Void
Buy (){

System.Out. Print ("agent helped me buy :");

Show ();

}

 

}

// Write a test method

Public
Class
Test {

Public
Static void
Main (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
Class
ProxydyImplementsInvocationhandler {

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
Class
Test2 {

Public
Static void
Main (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.

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.