Java design pattern (proxy) proxy

Source: Internet
Author: User
Tags lenovo
Document directory
  • Proxy in Design Mode)

Proxy in Design Mode)

Transferred from:

Http://www.blogjava.net/lusm/archive/2007/08/08/135355.html

Http://www.blogjava.net/lusm/archive/2007/08/09/135433.html

 

 

Definition in Design Pattern

: Fronting for another object (provides a proxy for other objects to control access to this object ).


Why use proxy?


1. Authorization Mechanism
Users of different levels have different access rights to the same object. For example, in the jive forum system, the proxy is used for authorization control. There are two types of users who access the Forum: registered users and tourists (not registered for use
In jive, you can use a proxy like forumproxy to control the access permissions of these two users to the Forum.

2. A client cannot directly operate on an object, but must interact with the object.
Two specific cases are given:
(1) if the object is a large image that takes a long time to be displayed, when the image is included in the document, open the document using the editor or browser, open the document must be very
Fast, you cannot wait for the processing of large images to complete. In this case, you need to create an image proxy to replace the real image.

(2) if the object is on a remote server on the internet, it may be slow to directly operate on the object because of the network speed, we can replace the object with a proxy.

In short, the principle is that objects with high overhead are created only when they are used. This principle can save us a lot of valuable Java memory. therefore, some people think that Java consumes resources and memory. I think this is also related to programming ideas.

 

Other cases that require proxy are not listed here.

 

How to use proxy?

Proxy is mainly divided into static proxy mode and dynamic proxy mode.

 

Static proxy Mode
:

A simple example:

Package com. Proxy; <br/>/** <br/> * computer wholesaler. <Br/> */<br/> Public interface computer {<br/> Public void buy (string name); <br/>}< br/>

Package com. Proxy; <br/>/** <br/> * Lenovo computer company. <Br/> */<br/> public class Lenovo implements computer {<br/> @ override <br/> Public void buy (string name) {<br/> system. out. println (name + "Lenovo products"); <br/>}< br/>

Package com. Proxy; <br/>/** <br/> * Samsung computer company. <Br/> */<br/> public class Samsung implements computer {<br/> @ override <br/> Public void buy (string name) {<br/> system. out. println (name + "Samsung products"); <br/>}< br/>

Package com. Proxy; <br/>/** <br/> * computer sales agent. <Br/> */<br/> public class computerproxy implements computer {</P> <p> private computer; </P> <p> Public computerproxy (computer) {<br/> This. computer = computer; <br/>}</P> <p> @ override <br/> Public void buy (string name) {<br/> system. out. println ("before method invoking"); <br/> Computer. buy (name); <br/> system. out. println ("after method invoking"); <br/>}< br/>}

Package com. Proxy; <br/>/** <br/> * customers who buy computers, one for Samsung and the other for Lenovo. <Br/> */<br/> public class buycomputer {<br/> Public static void main (string [] ARGs) {<br/> Computer proxy1 = new computerproxy (new Samsung (); <br/> proxy1.buy ("I want to buy a Samsung computer "); <br/> Computer proxy2 = new computerproxy (new Lenovo (); <br/> proxy2.buy ("I want to buy a Lenovo computer "); <br/>}< br/>

 

Running result:

Before method invoking
I want to buy a Samsung computer.
After method invoking
Before method invoking
I want to buy a Lenovo computer Lenovo Product
After method invoking

 

In the above example, Lenovo. Java and Samsung. Java are the classes that truly implement the buy method, computerproxy is the proxy class, and computer is their common base class.

Computerproxy does not have a new method. It determines whether to use the buy method in Lenovo or the buy method in Samsung based on the type of the input object (Lenovo or Samsung, it acts as the frontend of Lenovo and Samsung, and corresponds to the definition of the proxy mode:Fronting for another object
.

 

Dynamic proxy mode:

 

The code in the previous article is very simple (just to let everyone know what proxy is). It implements static proxy, computerproxy as a computer agent, and is a computer manufacturer (Samsung, Lenovo) in the computer industry) and customers provide services to provide a variety of convenience.
Sadly, if I want to add another industry, for example, the car industry to be discussed below, we can only add one proxy, that is, we need to write another carproxy code, now we assume we have many industries, so there is no doubt that our workload is getting heavy. Is there any way for our agents to implement cross-industry agents?
What about it?
The answer is yes. This is the significance of dynamic proxy.

 

If you want to add an automotive industry, the following code will be added:

Package com. Proxy. Dynamic; <br/>/** <br/> * auto wholesalers. <Br/> */<br/> Public interface car {<br/> Public void buy (string name); <br/>}< br/>

Package com. Proxy. Dynamic; <br/>/** <br/> * wholesaler of Rolls-Royce. <Br/> */<br/> public class rollsroyce implements car {<br/> Public void buy (string name) {<br/> system. out. println (name + "Rolls-Royce products"); <br/>}< br/>}

Package com. Proxy. Dynamic; <br/>/** <br/> * automobile sales agency. <Br/> */<br/> public class carproxy implements car {</P> <p> private car; </P> <p> Public carproxy (car) {<br/> This. car = car; <br/>}</P> <p> @ override <br/> Public void buy (string name) {<br/> system. out. println ("before method invoking"); <br/> Car. buy (name); <br/> system. out. println ("after method invoking"); <br/>}< br/>}

 

If there are many industries, there will also be a lot of proxy classes (the carproxy in the above example), while the workload increases, it also brings about the maintainability problem. If dynamic proxy is used, this problem can be easily solved.

Next we will add an automotive industry based on the above static proxy model example.

The complete code is as follows:

 

Package com. Proxy; <br/>/** <br/> * computer wholesaler. <Br/> */<br/> Public interface computer {<br/> Public void buy (string name); <br/>}< br/>

Package com. Proxy; <br/>/** <br/> * Lenovo computer company. <Br/> */<br/> public class Lenovo implements computer {<br/> @ override <br/> Public void buy (string name) {<br/> system. out. println (name + "Lenovo products"); <br/>}< br/>

Package com. Proxy; <br/>/** <br/> * Samsung computer company. <Br/> */<br/> public class Samsung implements computer {<br/> @ override <br/> Public void buy (string name) {<br/> system. out. println (name + "Samsung products"); <br/>}< br/>

Package com. Proxy. Dynamic; <br/>/** <br/> * auto wholesalers. <Br/> */<br/> Public interface car {<br/> Public void buy (string name); <br/>}< br/>

Package com. Proxy. Dynamic; <br/>/** <br/> * wholesaler of Rolls-Royce. <Br/> */<br/> public class rollsroyce implements car {<br/> Public void buy (string name) {<br/> system. out. println (name + "Rolls-Royce products"); <br/>}< br/>}

 

Dynamic Proxy:

Package COM. proxy. dynamic; <br/> Import Java. lang. reflect. invocationhandler; <br/> Import Java. lang. reflect. method; <br/> Import Java. lang. reflect. proxy; <br/> public class allproxy implements invocationhandler {</P> <p> private object; </P> <p> Public object BIND (Object object) {<br/> This. object = object; </P> <p> return proxy. newproxyinstance (object. getclass (). getclassloader (), <br/> object. getclass (). getinterfaces (), this); <br/>}</P> <p> @ override <br/> Public object invoke (Object proxy, method, object [] ARGs) <br/> throws throwable {<br/> system. out. println ("before method invoking"); <br/> Object result = method. invoke (object, argS); <br/> system. out. println ("after method invoking"); <br/> return result; <br/>}< br/>

Package COM. proxy. dynamic; <br/> public class buyall {<br/> Public static void main (string [] ARGs) {<br/> allproxy = new allproxy (); <br/> Computer proxy1 = (Computer) allproxy. BIND (new Samsung (); <br/> proxy1.buy ("Samsung"); </P> <p> Computer proxy2 = (Computer) allproxy. BIND (new Lenovo (); <br/> proxy2.buy ("Lenovo"); </P> <p> Car proxy3 = (CAR) allproxy. BIND (New rollsroyce (); <br/> proxy3.buy ("rollsroyce"); <br/>}< br/>}

 

Running result:

Before method invoking
Samsung products
After method invoking
Before method invoking
Lenovo products
After method invoking
Before method invoking
Rollsroyce Rolls-Royce Products
After method invoking

 

In dynamic proxy mode, only one proxy class (allproxy) is required to complete all proxy work and reduce code.

 

Related Article

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.