SIGN UP MEMBER LOGIN:    
ARTICLE

Proxy Design Pattern

Posted by Jean Paul Articles | Design & Architecture December 08, 2011
In this article we are going to discuss the Proxy design pattern. It is one among the 23 design patterns by Gof. As usual we can start with the Challenge and Solution style.
Reader Level:
Download Files:
 

In this article we are going to discuss the Proxy design pattern. It is one among the 23 design patterns by Gof. As usual we can start with the Challenge and Solution style.

Challenge

You are working on automating Excel COM objects. The business logic has to think too much about instantiating COM, doing ground works before calling the actual functionalities etc.

The same code could get replaced by DCOM tomorrow. So the Excel access code is spread throughout your application. How to do a better design?

ProxyPtrn1.gif

Definition

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

Implementation

We can solve the above problem by using a Proxy pattern. As the definition says, we will have to create a placeholder or wrapper around the original object to control access to it. In this way we can make the following advantages:

  • Talk to COM or DCOM Excel object by changing configuration in one place
  • Give application a simple interface to talk with Excel COM/DCOM
  • Feel application think like it is talking to a local object

Application

Following is the application with data:

ProxyPtrn2.gif

Old Code

Following is the old code where application is forced to think too much about the Excel COM object and method of assigning values to the cells.

//Start Excel and get Application object.
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;

//Get a new workbook.
Microsoft.Office.Interop.Excel._Workbook oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
Microsoft.Office.Interop.Excel._Worksheet oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;

//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "Name";
oSheet.Cells[1, 2] = "Address";
oSheet.Cells[1, 3] = "Salary";

//Format A1:D1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "C1").Font.Bold = true;
oSheet.get_Range("A1", "C1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;

int i = 2;
foreach (Employee employee in _list)
{
    string[] values = new string[3];
    values[0] = employee.Name;
    values[1] = employee.Address;
    values[2] = employee.Salary.ToString();

    oSheet.get_Range("A" + i.ToString(), "C" + i.ToString()).Value = values;
    i++;
}

oXL.Visible = true;
oXL.UserControl = true;


The problem with above code is too much Excel logic is mixed with the application logic. Now we can see the new code with proxy pattern implemented.

New Code with Proxy Pattern

ExcelProxy proxy = new ExcelProxy();
proxy.Save(_list);


The code is only 2 lines and the Excel COM object creation, cell value assigning etc are taken care by the ExcelProxy class.

ProxyPtrn3.gif

Application Execution

On executing the application we can see the results inside Microsoft Excel.

ProxyPtrn4.gif

Other Examples of Proxy Pattern

We can have many real world examples which implement the Proxy pattern. When we add a WCF reference a Proxy is created. This class takes care of the connection details, serialization etc.

Summary

In this article we have seen the usage of Proxy design pattern along with an example. The source code attached contains the example we have discussed.

 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Vineet for the good words..

Posted by Jean Paul Dec 11, 2011

Thank You Mike for the good words..

Posted by Jean Paul Dec 11, 2011

Good.............

Posted by Vineet Kumar Saini Dec 10, 2011

Good work jean......i find myself to learn new concept. Thanks

Posted by Mike Steward Dec 10, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor