Java design mode--proxy (proxy) mode

Source: Internet
Author: User

The proxy mode provides an agent or placeholder for an object to control access to the object.

Image Proxy
The design of using proxy mode is sometimes very fragile, and they rely on forwarding methods to invoke their underlying objects. Forwarding can create a design that is very fragile and requires frequent maintenance.

The load () method uses the JFrame object as a parameter to make a callback after the specified image has finished loading. At the time of execution of the load () method, it calls SetImage () with the image object referenced by loading, then redraws the graphical display window, and finally starts a separate thread for itself. The run () method is executed in a separate thread, which creates a new ImageIcon object based on the image file name specified in the constructor, and then calls the SetImage () method on the image object for the parameter, and finally redraws the window.


Remote Agent
In Java, a remote method invocation (RMI), where a proxy object is used to forward a call request to a specified object running on another computer, the client can easily obtain such a proxy object.

To use RMI, you first create a remote interface that defines the messages that need to be exchanged between computers, and then create a remote object that enables remote interfaces and extends the UnicastRemoteObject class
Public interface Rocket extends remote{void boost (double factor) throws Remoteexception;double Getapogee () throws Remotee Xception;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, and the client can access the Rocketimpl object's methods by running a local proxy object.
The instance of Rocketimpl is running on a machine, and we have to provide a proxy object for the Rocketimpl object on the client side, so that it can be accessed by Java programs running on other machines. For this purpose, the client needs a proxy for the Rocketimpl object. The proxy class must implement the Rocket interface and provide additional attributes for communicating with the remote object. One of the greatest conveniences of RMI is that it can automatically create this proxy class. In order to generate the proxy class automatically, we must store the Rocketimpl.java file and the Rocket.java interface file in the operating directory of the RMI registration program.
Before an object can be accessed remotely, we must register the object with the RMI registration program running on the server. After running the registration program on the server, we can start creating and registering 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. Clients can access remote-running Biggie objects as long as they have access to the rocket interface and the Rocketimpl_stub class. If there is only one machine, we can still test the RMI application, but run the server-side code on localhost instead of 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 ();}}}   
The advantage of RMI is that it allows the client program to communicate with the remote object simply by interacting with the local proxy object. RMI users define the client and
The interface of the server-side shared object. RMI provides a rocket interface implementation class for both the client and the server, and the two implementation classes work together to
Complete seamless communication between processes. The server side and the client do not have to care about these details.


Dynamic Agent
Java can support dynamic proxy features. With dynamic proxies, you can use proxy objects to wrap other objects and use proxy objects to intercept the tuning of the wrapped objects.
With the request, and then the agent continues to forward these requests to the wrapped object. Before or after executing the intercepted call, we can write the relevant code for strengthening.
Restricting dynamic proxies can prevent other random objects from being wrapped. Under normal conditions, dynamic proxies allow you to fully control the operation of the wrapped object.
Dynamic proxies require interfaces that are implemented by classes that use objects. Calls that the proxy object can intercept are those defined by these interfaces. If a class can implement a truncated

interface, you can use dynamic proxies to wrap instances 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.");}}

Cond

Java design mode--proxy (proxy) mode

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.