On the use of .NET development agency model

When we need to use the object is complex or takes a long time to construct, then you can use proxy mode (Proxy). For example: If you build an object is time consuming and computer resources, the agent mode (Proxy) allows us to control this situation, until we need to use the actual object. The method of a proxy (Proxy) typically comprises the use and the object will be the same, once you start using this object, the method passes to the actual object through a proxy (Proxy).

   Some cases can use proxy mode (Proxy) are:

   1, an object, such as a large image, you need to load a long time.

   2, requires a long time before the results can be done, and it needs to display an intermediate calculation result of the process

   3, an object that exists on a remote computer, the remote object needs to be loaded through the network it takes a very long time, especially in the network transmission peak.

   4, only a limited access to the object, the proxy mode (Proxy) can verify the user's permission

   Agent mode (Proxy) may also be used to distinguish an object instance of the request and the actual access, for example: the program may be established during initialization multiple objects, but not all at once using proxy mode (proxy) to load the real object need.

   This is a great need to load and display an image of the program, when the program starts, it must determine the image to be displayed, but the actual image can be displayed only after fully loaded! Then we can use proxy mode (Proxy).

   The proxy mode (Proxy) can delay the loading of the actual image, until it receives a request paint. During the loading of the actual image we can use proxy mode (Proxy) in the actual position of the image to be displayed pre-load a relatively small, simple graphics.

   Proxy image Code:

Public Class ImageProxy

  Private done As Boolean
  Private tm As Timer

  Public Sub New()
   done = False
   '设置timer 延迟5秒
   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
   '显示预先的图像,直到实际图像载入完成
   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


   Implementing the interface:

   an image preloaded classes:

Public Class QuickImage

  Implements Imager
  Public Function getImage() As Image Implements Imager.getImage
   Return New bitmap("Box.gif")
  End Function

End Class


   The actual image of the class is loaded:

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 a display image, (the Proxy) defining an instance of the proxy image, the button image in the event of loading, loading the image:

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


   总结:

   这只是一个很简单的例子(例子来自于《c#设计模式》),通过这个例子可以对代理(Proxy)有初步的认识!Adapter模式和代理模式(Proxy)都是在对象间构造一个简单的层。然而,Adapter模式向对象提供一个不同的接口,代理模式(Proxy)为对象提供相同的接口。 深圳代孕电13802269370  代孕微13802269370  北京代孕微13802269370

Guess you like

Origin www.cnblogs.com/bbc2020/p/12457286.html