Proxy Design Pattern

 definition
 UML diagram
 participants
 sample code in C#



definition

Provide a surrogate or placeholder for another object to control access to it.

Frequency of use:   medium high

UML class diagram


participants

    The classes and/or objects participating in this pattern are:

  • Proxy   (MathProxy)
    • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
    • provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.
    • controls access to the real subject and may be responsible for creating and deleting it.
    • other responsibilites depend on the kind of proxy:
      • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
      • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.
      • protection proxies check that the caller has the access permissions required to perform a request.
  • Subject   (IMath)
    • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject   (Math)
    • defines the real object that the proxy represents.

sample code in C#

This structural code demonstrates the Proxy pattern which provides a representative object (proxy) that controls access to another similar object.

Show code

// Proxy pattern -- Structural example




This real-world code demonstrates the Proxy pattern for a Math object represented by a MathProxy object.

Show code

// Proxy pattern -- Real World example




This .NET optimized code demonstrates the same real-world situation as above but uses modern, built-in .NET features, such as, generics, reflection, object initializers, automatic properties, etc.

Show code

// Proxy pattern -- .NET optimized




   Better Code 
   Better Career 
   Better Lifestyle 


.NET
Design Pattern
Framework 4.0


 
C# and VB.NET


   Includes:

  Gang of Four Patterns
  Head First Patterns
  Enterprise Patterns
  SOA Patterns

  WPF Best Practices
  WCF Best Practices
  LINQ Best Practices

  Model View Controller
  Model View Presenter
  Model View ViewModel

  Much more...

Click here for details

-- Instant Access --
Instant Download