Java design pattern-Proxy pattern

Source: Internet
Author: User

Java design pattern-Proxy pattern

Proxy mode provides a Proxy or placeholder for an object to control access to the object.

There are several application scenarios:

 

RemoteProxy provides a local representation of an object in different address spaces.
 
VirtualProxy creates objects with high overhead as needed.
 
ProtectionProxy controls access to the original object.
 
SmartReference replaces simple pointers and performs some additional operations when accessing objects.

 

Image proxy
The design using the Proxy mode is sometimes very fragile. They rely on the forwarding method to call their underlying objects. Forwarding may create a very fragile design that requires frequent maintenance.

The load () method uses a JFrame object as a parameter for callback after the specified image is loaded. When executing the load () method, it first calls setImage () with the image object referenced by LOADING as the parameter, then re-draws the graphic display window, and finally starts a separate thread for itself. The run () method is executed in a separate thread. This method creates a new ImageIcon object based on the image file name specified in the constructor, then, the setImage () method is called with the image object as the parameter, and the window is re-painted.

 

Remote proxy
In Java remote method call (RMI), a proxy object is used to forward call requests to a specified object running on another computer, so that you can easily obtain this proxy object.

To use RMI, you must first create a remote interface, define the messages to be exchanged between computers, and then create a remote object. This remote object can implement remote interfaces and expand the UnicastRemoteObject class.
public interface Rocket extends Remote{void boost(double factor) throws RemoteException;double getApogee() throws RemoteException;double getPrice() throws RemoteException;}
public class RocketImpl extends UnicastRemoteObject implements Rocket{protected double price;protected double apogee;public RocketImpl(double price,double apogee) throws RemoteException{   this.price=price;   this.apogee=apogee;}public void boost(double factor){   apogee *= factor;}public double getApogee(){   return apogee;}public double getPrice(){   return price;}}
The RocketImpl object runs on the server as a service provider class. The client can access the RocketImpl object through a local proxy object.
The RocketImpl instance runs on one machine. to make it accessible to Java programs running on other machines, you must provide a proxy object for the RocketImpl object on the client. To achieve this, the client needs a proxy for the RocketImpl object. This proxy class must implement the Rocket interface and provide additional features for communicating with remote objects. One of the greatest conveniences of RMI is that it can automatically create this proxy class. To automatically generate the proxy class, you must store the RocketImpl. java file and the Rocket. java interface file in the running directory of the RMI registration program.
Before the object can be remotely accessed, We must register the object with the RMI registration program running on the server. After running the registration program on the server, we can create and register the RocketImpl object.
public class RegisterRocket{public static void main(String[] args){   try{     Naming.rebind(rmi://localhost:5000/Biggie,biggie); System.out.println(Registered biggie);   }catch(Exception e){     e.printStackTrace();   }}}
Run the RegisterRocket application to create and run a RocketImpl object biggie on the server. As long as the client can access the Rocket interface and RocketImpl_stub class, it can access the biggie object that runs remotely. If there is only one machine, we can still test this RMI application, but we need to run the server code on localhost rather than on another host.
public class ShowRocketClient{public static void main(String[] args){   try{      Object obj=Naming.lookup(rmi://localhost:5000/Biggie);  Rocket biggie = (Rocket) obj;  System.out.println(Apogee is  + biggie.getApogee());   }catch(Exception e){      System.out.println(Exception while looking up a rocket:);  e.printStackTrace();   }}}
RMI enables client programs to communicate with remote objects only by interacting with local proxy objects. RMI users define the client and
The interface for sharing objects on the server. RMI provides a Rocket interface implementation class for the client and server respectively. The two implementation classes work together to provide
Complete seamless communication between processes. The server and client do not have to care about these details.


Dynamic proxy
Java supports dynamic proxy features. With the help of dynamic proxy, you can use the proxy object to wrap other objects, and use the proxy object to intercept the call to the encapsulated object
Use the request, and then the proxy continues to forward these requests to the encapsulated object. Before or after the intercepted call is executed, we can write related enhanced code.
Dynamic proxy restriction can prevent other arbitrary objects from being packaged. Under normal conditions, dynamic proxy enables you to fully control the operations of the encapsulated object.
The dynamic proxy must use the interface implemented by the object class. A call that can be intercepted by a proxy object is a call defined by these interfaces. If a class can be intercepted

You can use a dynamic proxy to wrap the instance of that class.

 

public class ShowDynamicProxy{public static void main(String[] args){   Set s = new HashSet();   s = ImpatientProxy.newInstance(s);   s.add(new BadApple(Lemon));   ...   System.out.println(The set contains  + s.size() +  things.);}}

 

 

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.