Software

Proxy Pattern

Proxy acts as a simplified or lightweight version of the original object where the proxy object is still able to accomplish the same tasks, but may delegate requests to the original object to achieve them. The proxy object itself can hide sensitive information of the original object when interacting with another objects.

3 main reasons to use proxy design pattern:

  1. To act as a virtual proxy – where the proxy class is used in place of a real subject class that is resource intensive to instantiate (this is commonly used on images and web pages, because a single high definition image can be extremely large).
  2. To act as a protection proxy – in order to control access to the real subject class (role management system).
  3. To act as a remote proxy – where the proxy class is local and the real subject class exists remotely (like a Google documents where web browser has all the objects it needs locally, which also exists on Google servers).
Proxy Class Diagram.png
  • Proxy and RealSubject are subtypes of Subject – this is clear from the way interface inheritance is shown with an open arrowhead. Since both Proxy and RealSubject must implement the methods of Subject, they subtypes.
  • The proxy design pattern achieves polymorphism through implementing a Subject interface – Polymorphism is achieved with the interface, ensuring that both the Proxy and the RealSubject can be interacted with using the same methods, even if the internal implementation of these methods is different.
Proxy (Warehouse) Class Diagram.png

Step 1: Design the subject interface

public interface IOrder [
    public void FulfillOrder(Order);
}

Step 2: Implement the real subject class

public class Warehouse: IOrder {
    private Hashtable<string, int> stock;
    private string address;

    public void FilfillOrder(Order order){
        foreach(Item item in order.itemList){
            this.stock.Replace(item.sku, stock.Get(item)-1);
        }
        //The rest of the code like shipment and delivery processment.
    }

    public int CurrentInventory(Item item) {
        int current = 0;
 
        if(stock.ContainsKey(item.sku)){
            current = (int)stock.Get(item.sku);
        }

        return current;
    }
}

Step 3: Implement the proxy class

public class OrderFullfilment: IOrder {
    private List<Warehouse> warehouse;

    public void FulfillOrder(Order order) {
        // For each item in a customer order, check each warehouse to see if it is in stock.
        // If  it's then create a new Order for that warehouse. Else check the next warehouse.
        // Send the al the Orders to the warehouse after you finish iterating over all the 
        // items in the original Order.
        foreach(Item item in order.itemList) {
            foreach(Warehouse warehouse in warehouses) {
               // ...
            }
        }
    }
}

To summarize: to use the proxy class to wrap the real subject class.

One thought on “Proxy Pattern

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.