Click here to Skip to main content
15,886,199 members
Articles / Memory

Proxy

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Oct 2013CPOL 8.9K   2  
Proxy Design PatternThe Gang Of Four definition of this design pattern is "Provides a surrogate or placeholder for another object to control access

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

VB
' My Products Interface
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


UML

Proxy Design Pattern UML

This pattern is implemented to do memory management. Suppose in application it is required to initialize a class which contains a lot of properties and methods. If busness logic fails here, then loading this heavy class in the memory will be useless. Eg.

C#
if (driver.Age <= 16)
        return "Young driver";
else
{
        realCar = new Car();
        return realCar.MoveCar();
}

    public class Car : ICar
    {
      // Properties

     // Functions

    }

Here the Car class will only be initiallised if the age is greater than 16 else it will not be initiallized.

This article was originally posted at http://wiki.asp.net/page.aspx/355/proxy

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --