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 Provides a placeholder for an expensive object, delaying its instantiation until it is needed, e.g., a large image that is loaded only when it becomes visible on the screen
  • Remote Proxy Represents an object in a different address space, e.g., a client accessing a remote database through a remote proxy
  • Protection Proxy Controls access to the original object based on specific permissions or authentication, e.g., a proxy that checks user access rights before granting access to a secured system
  • Cache Proxy Stores the results of expensive operations and returns cached results, e.g., a web proxy caching server that stores frequently accessed web pages to reduce load times
  • Firewall Proxy Protects a network from unauthorized access by filtering incoming and outgoing traffic, e.g., a proxy server that examines packets and determines whether they should be allowed through the network
  • Smart Reference Proxy Adds extra behavior when an object is accessed, like reference counting, e.g., a proxy that keeps track of the number of times an object is accessed
  • Logging Proxy Adds logging functionality to an object's operations for debugging or auditing purposes, e.g., a proxy that logs requests made to a web service
  • Synchronization Proxy Provides thread-safe access to a shared resource by controlling concurrent access, e.g., a proxy that ensures only one thread can access a shared data structure at a time

Protection Proxy

Chains of Proxies