Agent mode
When we need to use the objects, you can use a proxy mode (Proxy). For example, if you build an object takens to time and computer resources, proxy mode allows us to control this situation until we need to use the actual object. A agent (Proxy) typically contains the same method as the object to be used, once you start using this object, which will pass the agent (Proxy) to the actual object.
Some situations where proxy mode (proxy) can be used:
u One object, such as a large image, is very long.
u One requirement for a long time to complete, and need to display intermediate results during its calculation
u An object existing on the remote computer, it takes a long time to load this remote object through the network, especially in the peak period of network transmission.
u One object only has limited access, proxy mode (proxy) can verify user permissions
Proxy mode (Proxy) can also be used to distinguish a request and actual access of an object instance, for example: during initialization
Multiple objects may be created, but it is not all used immediately, and the proxy mode (proxy) can load the real object required.
This is a program that needs to load and display a large image. When the program starts, you must determine the image to display, but the actual image can only be displayed after full load! At this time we can use the proxy mode (Proxy).
This proxy mode (Proxy) can delay the load of the actual image until it receives a PAINT request. During the load period of the actual image, we can preload a relatively small, simple graphic by proxy mode (Proxy).
Image Proxy code:
Public Class ImageProxy
Private done as boolean
Private TM as Timer
Public Sub New ()
DONE = FALSE
'Setting Timer Delay for 5 seconds
TM = New Timer (_
New TimerCallback (Addressof Tcallback, ME, 5000, 0)
End Sub
Public function isready () as boolean
Return Done
END FUNCTION
Public function getImage () as image
DIM IMG As IMAGER
'Display a pre-image until the actual image is loaded
IF isready then
IMG = new finalImage ()
Else
IMG = New QuickImage ()
END IF
Return Img.getimage
END FUNCTION
Public Sub Tcallback (Byval Obj As Object)
DONE = TRUE
Tm.dispose ()
End Sub
END CLASS
Define a simple interface:
Public Interface Imager
Function GetImage () as image
End interface
Implement the interface:
Pre-loaded images:
Public Class QuickImage
IMPLEMENTS IMERER
Public function getImage () as image_
Implements iMager.getimage
Return New Bitmap ("Box.gif")
END FUNCTION
END CLASS
Categories that load the actual image:
Public Class FinalImageImplements iMager
Public function getImage () as image_
Implements iMager.getimage
Return New Bitmap ("Flowrtree.jpg")
END FUNCTION
END CLASS
In a form of displaying an image, define an image agent (Proxy) instance, in the image button event, load the image:
Private imgproxy as imageproxy
Public Sub New ()
Mybase.new
FORM1 = ME
InitializeComponent
IMGPROXY = new imageProxy ()
End Sub
Protected Sub BTload_Click (Byval e as system.eventargs) Handles BTload.click
Pic.Image = imgproxy.getimage
End Sub
to sum up:
This is just a very simple example (example from "C # Design Mode"), you can have a preliminary understanding of the agent (Proxy) through this example! Adapter mode and proxy mode (Proxy) are constructed between the objects to construct a simple layer. However, the Adapter mode provides a different interface to the object, and proxy mode provides the same interface to the object.