When the objects we need to use are complex or need to be constructed for a long time, we can use the Proxy mode ). For example, if building an object takes a lot of time and computer resources, the Proxy mode allows us to control this situation until we need to use the actual object. A Proxy usually contains the same method as the object to be used. Once this object is used, these methods will be passed to the actual object through Proxy.
Some situations where Proxy can be used:
1. An object, such as a large image, needs to be loaded for a long time.
2. A calculation result that takes a long time to complete, and the intermediate result must be displayed during the calculation process.
3. It takes a long time to load a remote object on a remote computer, especially during peak network transmission.
4. An object has only limited access permissions. The Proxy mode can verify the user's permissions.
Proxy mode can also be used to differentiate the request and actual access of an object instance. For example, multiple objects can be created during program initialization, but not all objects can be used immediately, proxy mode can load the desired real object.
This is a program that needs to load and display a large image. When the program starts, you must determine the image to be displayed, however, the actual image can only be displayed after it is fully loaded! Then we can use Proxy ).
This Proxy mode can delay loading of the actual image until it receives a painting request. During actual image loading, We can pre-load a small, simple image at the position where the actual image is to be displayed through the Proxy mode.
Image Proxy code:
Public Class ImageProxy
Private done As Boolean Private tm As Timer
Public Sub New () Done = False 'Sets the timer Latency by 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 the 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 |
Implementation interface:
Pre-loaded image class:
Public Class QuickImage
Implements Imager Public Function getImage() As Image Implements Imager.getImage Return New bitmap("Box.gif") End Function
End Class |
Classes for loading actual images:
Public Class FinalImage
Implements Imager
Public Function getImage() As Image Implements Imager.getImage Return New Bitmap("flowrtree.jpg") End Function
End Class |
In the form of the displayed image, define an image Proxy instance and load the image in the image button event:
Private imgProxy As ImageProxy
Public Sub New()
MyBase.New Form1 = Me InitializeComponent imgproxy = New ImageProxy() End Sub
Protected Sub btLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btLoad.Click
pic.Image = imgProxy.getImage
End Sub |