DCSIMG
Design Patterns - Structural Patterns - Proxy - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Design Patterns - Structural Patterns - Proxy

Proxy Pattern

In my last post I wrote about structural patterns and introduced the decorator pattern.
In this post I'll introduce one of the most useful structural patterns - the proxy pattern.

What is the Proxy Pattern?
The proxy pattern is all about the making of small public objects that create and access complex objects.
The proxy object is the gateway to the complex object and it communicate (send requests) with that object.
One other reason to create a proxy is to hand the creation of an object to the proxy if that object is expensive to create.
For a UML diagram of the pattern go to dofactory site.
There are several kind of proxies for example remote proxies (WCF service reference, Web Services web reference, Remoting references etc)
or authentication proxies (logging control is a proxy for a logging system).

Real Life Proxy Example
My favorite example for the use of proxies is the Policy Injection Application Block.
The PIAB provides a factory (another pattern that I'll write about in the future) for creating or wrapping policy-enabled objects.
A policy in PIAB is the combination of a series of handlers that execute when client code calls methods of the class.
When defining policies for objects, a proxy object is returned instead of the real object.
Making a request to the proxy object execute a handler pipeline before and after the real object is called.
Each handler can add or change the request to the real object.

A diagram that is taken from the enterprise library documentation shows the behavior of PIAB:

image 

For more details about PIAB go to Patterns & Practices site or open the enterprise library documentation.
The PIAB is an example for a kind of proxies that is called smart proxies.

Example in C#
Lets look at a proxy example:
In the example we have 4 players - 
Book object that represent a data object.
ILibrary that represent an interface for borrowing and returning books.
BookLibrary that represent a book library which implement the ILibrary interface.
LibraryProxy that represent the proxy for the BookLibrary class.

    public class Book

    {

        #region Members

 

        private string _strBookName;

        private string _strBookISBN;

 

        #endregion

 

        #region Properties

 

        public string BookName

        {

            get { return _strBookName; }

            set { _strBookName = value; }

        }

 

        public string BookISBN

        {

            get { return _strBookISBN; }

            set { _strBookISBN = value; }

        }

 

        #endregion

 

        #region Ctor

 

        public Book(string bookName, string bookISBN)

        {

            BookName = bookName;

            BookISBN = bookISBN;

        }

 

        #endregion

    }

 

    public interface ILibrary

    {

        void BorrowBook(Book book);

        void ReturnBook(Book book);

 

    }

 

    public class BookLibrary : ILibrary

    {

        #region ILibrary Members

 

        public void BorrowBook(Book book)

        {

            Console.WriteLine("Borrow the book: {0} with the ISBN: {1}", book.BookName, book.BookISBN);

        }

 

        public void ReturnBook(Book book)

        {

            Console.WriteLine("Return the book: {0} with the ISBN: {1}", book.BookName, book.BookISBN);

        }

 

        #endregion

    }

 

    public class LibraryProxy : ILibrary

    {

        #region Members

 

        private BookLibrary _library;

 

        #endregion

 

        #region Ctor

 

        public LibraryProxy()

        {

            _library = new BookLibrary();

        }

 

        #endregion

 

        #region ILibrary Members

 

        public void BorrowBook(Book book)

        {

            _library.BorrowBook(book);

        }

 

        public void ReturnBook(Book book)

        {

            _library.ReturnBook(book);

        }

 

        #endregion

    }

Summary
To sum up this post, the proxy pattern should be used in these situations:
1. You need to access a remote object.
2. You have an object that its creation is expensive.
3. You need to perform an action when an object is called.

In the next post I'll continue the tour in the design patterns world.

Comments

Adi said:

The proxy pattern is also widely used in communication (lately in WCF), with the proxy representing an object on the remote server.

# February 29, 2008 1:43 PM

Maor David-Pur said:

We can use it also in WCF. The proxy is an object on the server.

# February 29, 2008 3:12 PM

Gil Fink said:

Thanks Guys for your comments but I wrote briefly about remote proxies in the post.

I wanted to show other things about the proxy pattern except for remoting.

Eventhough, You are right. The proxy is widely used in remote object invocation (WCF, web services, remoting etc).

# March 2, 2008 9:37 AM

Gil Fink Blog said:

Continuing the tour in the structural patterns I'm going to present today the Facade pattern. You

# March 16, 2008 9:00 PM

Gil Fink Blog said:

Continuing the tour in the structural patterns I'm going to introduce the Adapter pattern. You can

# March 26, 2008 9:04 PM

Gil Fink Blog said:

Today it's the turn of composite pattern to be revealed. You can read my previous posts on structural

# April 11, 2008 2:00 PM

Gil Fink Blog said:

In this post I'm going to explain the use of the bridge pattern. You can read my previous posts on

# April 12, 2008 6:12 PM

Gil Fink Blog said:

In the last post about structural patterns I'm going to explain how and when you should use the flyweight

# April 20, 2008 12:32 PM

Gil Fink Blog said:

In my first post about design patterns I wrote that there are three kinds of design patterns. I covered

# April 22, 2008 3:57 PM

Gil Fink Blog said:

The first pattern in the creational pattern series is the singleton pattern. You can read my previous

# April 22, 2008 7:29 PM

Gil Fink's Blog said:

It has been a while since I wrote about design patterns. In the current post I'm going to explain

# May 11, 2008 4:34 PM

Gil Fink's Blog said:

Today I'm going to discuss the last creational pattern - the builder pattern . You can read my previous

# May 17, 2008 9:34 AM

Gil Fink's Blog said:

In this post I describe the factory method design pattern and how to implement it in C#.

# May 17, 2008 8:09 PM

Gil Fink's Blog said:

In this post I explain the abstract factory design pattern and show an example in C#.

# May 17, 2008 9:06 PM

Praveen said:

Gil,

I have a question about this . You article says we should think of proxy pattern when instantiating the real object is expensive.

But whatever examples i have seen online , the proxy object is instantiating the actual objec internally. Then how come it is better than instantiating real object directly. I am confused.

Can you please clarify this for me.

Thanks

# May 21, 2008 7:26 AM

Gil Fink said:

Praveen Hi,

A very good question.

Sometimes instantiating the real object is expensive. Building a proxy which is lightweight representation of the real object enables us to consume less resources in the client machine if the real objetc's instantation is on another machine for example (web service proxy). The example I gave is only showing a simple aspect of proxy pattern. You can look at a proxy as a client of another service that imitate that service interface but is smaller then the service.

Gil

# May 21, 2008 12:38 PM

Gil Fink's Blog said:

Today's discussion is the last group type in the design patterns' world - the behavioral patterns

# May 21, 2008 7:12 PM

Yaron Shkop said:

This is a great blog entry.

Praveen, The missing peace that confuses you, (I think) is that proxies are usually wrap in their inner implementation the communication to the real object.

This way they can wisely manage connections to the real (expensive) object.

For example - Data base - A proxy can manage a connections pool to the real object, It can also manage a queue of incoming requests and send them with load considerations and optimizations to the real busy object.

This is why the pattern is usually used to access remote objects like DB, different servers etc.

# May 23, 2008 12:35 AM

Gil Fink's Blog said:

Today I present the strategy pattern . You can read my previous posts about design patterns here: Structural

# May 23, 2008 12:19 PM

Gil Fink's Blog said:

In the post I reveal the template method design pattern and show examples of how to use it in C#.

# June 13, 2008 5:52 PM

Gil Fink's Blog said:

In this post I explain the iterator design pattern and how to implement it in C#.

# June 13, 2008 5:59 PM

Gil Fink's Blog said:

In the post I reveal the command design pattern and I show how to implement it in C#.

# June 24, 2008 1:51 PM

Gil Fink's Blog said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 12:54 PM

Gil Fink on .Net said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 7:01 PM

Gil Fink on .Net said:

Farewell Design Patterns Series Today I finished my series in the subject of design patterns . It was

# August 8, 2008 9:11 AM

Gil Fink on .Net said:

Farewell Design Patterns Series Today I finished my series in the subject of design patterns . It was

# August 9, 2008 4:53 AM

Adiel Sharabi said:

כל הסדרה ממש נהדרת אני עובר כעת על כולה.

הפוסט הראשון שבור...

# August 12, 2008 4:15 AM

Gil Fink said:

Thanks for the compliments Adiel.

I fixed the broken link. Sorry for the inconvenient I changed the post's URL and forgot to change it here in this post.

# August 12, 2008 4:47 AM

Design Patterns Index - Gil Fink on .Net said:

Pingback from  Design Patterns Index - Gil Fink on .Net

# September 16, 2009 4:19 PM