Learn Design Pattern - Proxy Pattern

This article is all about the Proxy Pattern, the last pattern in the structural category.

Agenda

  • What is the Proxy Pattern?
  • When to use the Proxy Pattern?
  • Implement a Proxy Pattern using a .Net console based application.
  • Implement it using a modern approach, WCF
Some More Articles

    What is Proxy Pattern?

    According to the GOF the "Proxy Pattern provides a surrogate or placeholder for another object to control access to it".

    :( Is it very difficult to understand the definition?
    :) Don't worry; we have an explanation.

    When to use Proxy Pattern?

    Consider the following situation:

    • Where we want to expose functionality to the external world, but also we want to add some restrictions.
    • We often face a problem, where creating an object of a class is not possible directly. For example WCF services or Web Services where we just have an URL, creating the object directly is not possible or very complex.
      (Note: In the case of WCF Services and Web Services we create objects of something else, we will discuss it as we move further. Don't be confused now).
    • We often don't want our user to use the objects directly.
      For example: If CustomerBAL is the class then not anyone can use it just by saying,
      CustomerBAL objCustomer=new CustomerBAL();

    Practical example

    Consider if we are supposed to develop a customer management system.
    The system consists of 2 separate Panels.

    • One is Admin Panel - Which allows us to create new customers.
    • The second one is User Panel - Which is just for browsing existing customers.

    Now we have something called Customer Business Layer; a single class which encapsulates all functionalities related to customers (like AddCustomer and GetCustomer).

    Note: We have to make sure that while developing the User Panel, functions such as AddCustomer will not be available to the developer (In other words he will see only what is required).

    In such a scenario exposing objects directly is not a good solution; we need something which will act as a proxy for the original object and which can be passed around.

    (I am about to start an online training for design patterns and other technical stuff, you can contact me at 9870148461 or at [email protected].)

    Proxy Pattern as a solution

    To use the Proxy Pattern let us create a class which has a composition relationship with the original class and lets us invoke methods in the original class.

    Let's start coding.

    Step 1

    Create the original class as:

    public class CustomerBAL
    {
              List<Customer> objCustomers = null;
              string strDataPath = "D:\\Customer.data";
              public void PrepareBAL()
              {
                       if(File.Exists(strDataPath))
                       {
                                 FileStream fs2 = new FileStream(strDataPath, FileMode.Open);
                                 BinaryFormatter formatter3 = new BinaryFormatter();

                                 objCustomers = (List<Customer>)formatter3.Deserialize(fs2);
                                 fs2.Close();
                       }
                       else
                       {
                                 objCustomers = new List<Customer>();
                       }
              }

              public void CommitInsert()
              {
                       FileStream fs = new FileStream(strDataPath, FileMode.Create);
                       BinaryFormatter formatter2 = new BinaryFormatter();
                       formatter2.Serialize(fs, objCustomers);
                       fs.Close();
                       objCustomers = null;
              }
              public void ClearCustomer()
              {
                       objCustomers.Clear();
              }
              public void AddCustomer(Customer objCustomer)
              {
                       objCustomers.Add(objCustomer);
              }
              public List<Customer> GetCustomers()
              {
                       PrepareBAL();
                       return objCustomers;
              }
    }

    Step 2

    Create a proxy for the CustomerBAL class as:

    public class CustomerBALProxy
    {
              private CustomerBAL objCustomerBal=new CustomerBAL();
              public List<Customer> GetCustomers()
              {                
                       return objCustomerBal.GetCustomers();
              }
    }

    Step 3

    Create client code for the Admin Panel as:

    CustomerBAL objBal = new CustomerBAL();
    objBal.PrepareBAL();
    objBal.ClearCustomer();
     
    objBal.AddCustomer( new Customer()
    {
              CustomerName = "A",
              CustomerNo = "1"
    });
    objBal.AddCustomer(new Customer()
    {
              CustomerName = "B",
              CustomerNo = "2"
    });
    objBal.AddCustomer(new Customer()
    {
              CustomerName = "C",
              CustomerNo = "3"
    });
     
    objBal.CommitInsert();
    foreach(Customer objCustomer in objBal.GetCustomers())

    {
              Console.WriteLine("Customer No:"+objCustomer.CustomerNo);

              Console.WriteLine("Customer Name:" + objCustomer.CustomerName+"\n");
    }

    Step 4

    Create client code for the User Panel as:

    CustomerBALProxy objBal = new CustomerBALProxy();
    foreach(Customer objCustomer in objBal.GetCustomers())
    {
              Console.WriteLine("Customer No:" + objCustomer.CustomerNo);
              Console.WriteLine("Customer Name:" + objCustomer.CustomerName + "\n");
    }

    Let's talk of WCF, Web Services

    Currently in .Net we have something called WCF services, for which a proxy object is automatically created as soon as we add a Service Reference.

    ProxyPattern.jpg

    (This Article is more intended towards the design pattern, so we are not covering what WCF is and how to consume those using proxies, you can download the source code attached for a practical understanding.)

    Thanks for reading.
    Feedbacks and suggestions are always welcome.
    You can tweet me @SukeshMarla.

    Subscribe for my all article updates at:
    https://www.facebook.com/pages/Blogs-By-Sukesh-Marla/168078149903213

    www.sukesh-Marla.com

    Hope all of you enjoyed reading this article.

    Up Next
      Ebook Download
      View all
      Learn
      View all