Proxy

 Rate It (1)

Proxy Design Pattern

The Gang Of Four definition of this design pattern is "Provides a surrogate or placeholder for another object to control access to it". In the example below we use the pattern to enable caching on an ASP.net page. This code uses the Factory design pattern as well.

A VB example of the Proxy Design Pattern

' This code would be run at the web page code behind class
Dim
productsProvider As IProducts
Dim cachingEnabled As Boolean = CType(System.Configuration.ConfigurationManager.AppSettings("caching"), Boolean)

productsProvider = ProductsFactory.getProductsProvider(cachingEnabled)

Me.ProductsGrid.DataSource = productsProvider.getProducts()
Me.ProductsGrid.DataBind()

' My Products Interface
Public
Interface IProducts

     Function getProducts() As String

End Interface

' Factory Class to create concrete class
Public
Class ProductsFactory

     Public Shared Function getProductsProvider(ByVal UseCaching As Boolean) As IProducts
        
If UseCaching Then
           
Return New ProxyProducts
       
Else
           
Return New Products
       
End If
    
End Function

End Class

' The Proxy Products Class
Public
Class ProxyProducts
     
Implements IProducts

      Public Function getProducts() As String Implements IProducts.getProducts
        
' Check if we have the products in cache, if not
         
' then call the getProducts method from the Products class.

     
End Function

End Class

' Standard Products Class
Public
Class Products
     
Implements IProducts

     Public Function getProducts() As String Implements IProducts.getProducts
       
' Query the database for a list of products and return
    
End Function

End
Class

 

Revision number 1, Wednesday, March 05, 2008 7:56:20 AM by
This is not the most up to date version of this article. The most recent version can be found here.

Comments

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter