Object-Oriented Programming

Design Pattern Proxy

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Scenario

Problem

  • Don't want to change the functionality of the action()
  • But you may want to change some of the non-functional features of it
  • Adding additional features means changing the Subject
  • Want to do so flexibly, i.e., opportunistically add additional features on how the functionality is delivered
  • This may mean caching, protection, etc.

Proxy

Proxy Pattern

Proxy Pattern

  • Changes the behavior of the service but preserves the interface
  • Delegate action to another class
  • Client uses the proxy just like it was the real thing (in general)

Adapter vs. Proxy

Examples

Examples

  • Virtual Proxy Provide a placeholder for an expensive object and delay its instantiation until it is needed
  • Remote Proxy Represent an object in a different address space, e.g., access a remote database through a remote proxy
  • Protection Proxy Control access to the original object based on specific permissions or authentication, e.g., check user access rights before granting access to a secured system
  • Cache Proxy Store the result of an expensive operation and returns cached results, e.g., cache frequently accessed web pages
  • Firewall Proxy Protects a network from unauthorized access by filtering incoming and outgoing traffic, e.g., filter packets
  • Smart Reference Proxy Adds extra behavior when an object is accessed, like reference counting, e.g., keep track of the number of times an object is accessed
  • Logging Proxy Add logging to an object for debugging or auditing, e.g., log requests made to a web service
  • Synchronization Proxy Provide thread-safe access to a shared resource, e.g., ensure only one thread can access a shared data structure at a time

Protection Proxy

Chains of Proxies