技术开发 频道

浅析.NET开发中代理模式的使用

  定义一个简单的接口:

Public Interface Imager
  Function getImage() As image
  End Interface

 

  实现接口:

  预先载入的图像的类:

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

 

  载入实际图像的类:

Public Class FinalImage
  Implements Imager
  Public Funtion getImage() As Image Implements Imager.getImage
  Return New Bitmap(
"flowrtree.jpg")
  End Function
  End Class

  在显示图像的窗体中,定义一个图像代理的(Proxy)实例,在载入图像按钮事件中,载入图像:

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)为对象提供相同的接口。

0
相关文章