Design Pattern: Proxy

 

Intention

 

Introduction

 

Example

 

Context

 

Problem

 

Solution

 

Structure

Class
Client

Responsibilities

  • Uses the interface provided by the proxy to request a particular service
  • Fulfills its own task

Collaborators
Proxy

 

Class
Proxy

Responsibilities

  • Provides the interface of the original to clients
  • Ensures a safe, efficient and correct access to the original

Collaborator
Original

 

Class
AbstractOriginal

Responsibilities

  • Serves as an abstract base class for the proxy and the original

Collaborators

-

 

Class
Original

Responsibilities

  • Implements a particular service

Collaborators

-

 

General Scenario

Client need to do a task

Client calls the method of Proxy (send a service message)

Proxy internally do pre-processing such as mapping to Original

Proxy sends the real service message to Original

Result returned and Proxy do post-processing and return results back to Client

 

Implementation

Remote Proxy: clients or remote components should be shielded from network addresses and inter-process communication protocols

Protection Proxy: Components under access control

Cache Proxy: Multiple clients share read-only results from remote components

Synchronization Proxy: Multiple simultaneous accesses to a component must be synchronized

Counting Proxy: Audit trail for component counts

Virtual Proxy: Shield the whole component but only provide the information as needed

Firewall Proxy: Protect local client from outside world

 

Known Uses

 

Consequences

Advantages

Disadvantages