Java proxy mode articles Design Patterns (1) Author: Feng Rui article taken from: CCID in software engineering, proxy mode (Proxy Pattern) are very useful in many cases March 11, 2003. For example, in Java XML, developers can use the agent to access Web services. Example 1 demonstrated an example of a classic Hello World Web service: Example 1 An example of a SOAP agent
Public class helloclient {
Public static void main (String [] args) {
Try {
Helloif_stub Proxy = (New HelloWorldImpl (). GetHelloif ());
Proxy._settargetendpoint (args [0]);
System.out.println (Proxy.SAYHELLO ("Hello World!");
} catch (exception ex) {
EX.PrintStackTrace ();
}
}
}
In Example No. 1, the client first obtains a reference to the agent, then uses the command line parameter to set the endpoint of the agent (ie the web service's URL address), and then call the agent's Sayello () method, the agent will transfer the method to pass the method. Corresponding web services. Agent mode and decorative mode have a certain similarity to the Decorator Pattern. The two modes use the agent to pass the method call to another object, which is called a real subject. The difference between proxy mode and modification mode is that in agent mode, the relationship between proxy and real objects is determined when the program is compiled, and the modification mode is created at runtime. This article first provides an ImageICON example to illustrate the proxy mode, and then explore how JDK supports the proxy mode.
Agent mode
The proxy mode replaces the actual object by using the agent, allowing the program to control access to the object. Here is an example of ImageICON. Example 2 ImageICON
Import java.awt. *;
Import java.awt.event. *;
Import javax.swing. *;
Public class icontest extends jframe {
Private static string image_name = "Hands.jpg";
Private static int frame_x = 150, frame_y = 200,
Frame_width = 430, frame_height = 392;
Private icon imageicon = null, imageiconproxy = null;
Static Public Void Main (String Args []) {
Icontest app = new icontest ();
app.show ();
}
Public icontest () {
Super ("ImageICON Test");
ImageICON = new imageicon (image_name);
SetBounds (frame_x, frame_y, frame_width, frame_height);
SetDefaultCloseOperation (jframe.exit_on_close);
}
Public void paint (graphics g) {
Super.Paint (g);
INSETS INSETS = GetInsets ();
Imageicon.painticon (this, g, insets.left, insets.top);
}
}
Figure 1 ImageICON Test
The program created a Javax.swing.Imageicon object in the above example, and overloaded the Paint () method to display the icon. The code in the second embodiment has some defects, and the developers can only use a small picture in the program. Because you create a graphic spending a lot of system resources, the imageicon's instance is created in the initialization of graphics objects. If the program needs to display many relatively large graphics objects in a shorter time, the system is likely to handle. At the same time, if the application does not use these graphics, create these graphics at the front desk is also a waste of system resources. A better solution is to load graphics when you need to display graphics. In order to achieve this, it can be implemented by using the agent. When the agent's PaintICON () method is first called, the program creates a graphic. Figure 2 shows an example of both an ImageICON and an ImageICON Agent (right). The case where the program is just loaded in the above figure above. Since ImageICON objects need to load graphics when initialization, the picture is displayed on the left side of the window when the window appears. The image in the ImageICON agent is to be called until the first time is drawn. Figure 2 The following figure shows the two pictures that are loaded after loading.
Figure 2 ImageICON and ImageICON agents
Example 3 ImageICON objects and imageicon agents
Import java.awt. *;
Import java.awt.event. *;
Import javax.swing. *;
Public class proxytest extends jframe {
Private static string image_name = "Hands.jpg";
Private static int image_width = 430, image_height = 390,
Spacing = 5, Frame_x = 150,
Frame_y = 200, frame_width = 880,
Frame_height = 394;
Private icon imageicon = null, imageiconproxy = null;
Static Public Void Main (String Args []) {
ProxyTest App = New Proxytest ();
app.show ();
}
Public proxytest () {
Super ("ImageICON Agent Test");
/ / Generate an instance of ImageICON and ImageICON agents
ImageICON = new imageicon (image_name);
ImageiconProxy = new imageiconproxy (image_name,
Image_width,
Image_height);
// Set the border and default exit operation
SetBounds (frame_x, frame_y, frame_width, frame_height);
SetDefaultCloseOperation (jframe.exit_on_close);
}
Public void paint (graphics g) {
Super.Paint (g);
INSETS INSETS = GetInsets ();
Imageicon.painticon (this, g, insets.left, insets.top);
ImageiconProxy.painticon (this, g,
INSETS.LEFT Image_WIDTH SPACING, // Wide INSETS.TOP); // High
}
}
From the above code we can notice an ImageICON object and an ImageICONPROXY object in the Proxytest constructor, and rewrite the Paint () method of the base class. Let's take a look at ImageICON's class structure diagrams before discussing the agent's implementation code:
Figure 3 Image of ImageICON
It can be seen from the class structure diagram that three most basic methods are defined in the Javax.swing.icon interface: PainTicon (), GeticonWidth () and geticonheight (). The ImageICON class implements an ICON interface and adds some methods. At the same time, the reference to the graphic object included in it is also stored in ImageICON. The ImageICON proxy class also implements an ICON interface while saving a reference to true objects - IMAGEICON. Figure 4 shows the class structure diagram of ImageICONPROXY.
Figure 4 Image of ImageICONPROXY
Below is ImageiconProxy's Implementation Code: Example 4 ImageICON Agent
// ImageICONPROXY is the agent of the ImageICON object, it delays the display of the graphic to the first time
// When drawing it. When the graph has not been drawn, the agent displays the information on the interface on the interface.
Class imageiconproxy import. javax.swing.icon {
PRIVATE ICON REALICON = NULL;
Boolean isiconcreated = false;
Private string imagename;
Private int width, height;
Public ImageiconProxy (String ImageName, Int Width, Int Height) {
this.imagename = imagename;
THIS.WIDTH = Width;
THISHEIGHT = HEIGHT;
}
Public int GeticonHeight () {
RETURN ISICONCREATED? HEIGHT: REALICON.GETICONHEIGHT ();
}
Public int geticonwidth () {
RETURN ISICONCREATED Realicon == NULL? Width: Realicon.Geticonwidth ();
}
// The proxy's Paint () method covers the method of accumulation. Pay attention to the agent until you need to display the graphics
// contains a graphic.
Public Void PaintICON (Final Component C,
Graphics g, int x, int y) {
ISICONCREATED {
Realicon.PaintICON (C, G, X, Y);
}
Else {
g.drawRect (x, y, width-1, height-1);
g.drawstring ("Load Picture ...", X 20, Y 20);
// ImageICON object is now created in another thread
Synchronized (this) {
Swingutilities.invokelater (new runnable () {
Public void run () {
Try {
/ / In order to make the difference between the ImageICON object and the ImageICON agent
// More significant, this thread sleeps 2 seconds
Thread.currentthread (). SLEEP (2000);
Realicon = new imageicon (iMageName); isiconcreated = true;
}
Catch (InterruptedException EX) {
EX.PrintStackTrace ();
}
// Call the repaint () method after the ImageICON object is created.
C.REPAINT ();
}
});
}
}
}
}
ImageICONPROXY saves a reference to a graphic with Realicon. When drawing the agent for the first time, the ImageICON object is created in a separate thread, then the graph is loaded and drawn through the repaint () method. Figure 5 illustrates the relationship between these events through the timing chart.
Figure 5 timing diagram of imageicon agent