Proxy Pattern

This lesson discusses how objects can act on behalf of other objects without the clients knowing they aren't talking to the intended object.

What is it ?

The literal definition of proxy is the authority to represent someone else. In a proxy pattern setup, a proxy is responsible for representing another object called the subject in front of clients. The real subject is shielded from interacting directly with the clients. There could be several reasons why one would want to front a subject with a proxy, some are listed below:

  • To access remote objects over the internet, running in another JVM or another address space

  • To protect a subject from clients not authorized to access it

  • To stand in place of an object that may be expensive to create and delay the object's creation till it is accessed

  • To cache queries or results from subject for clients

  • There are a number of other use cases such as the firewall proxy, synchronization proxy etc.

Formally, the pattern is defined as a mechanism to provide a surrogate or placeholder for another object to control access to it.

Class Diagram

The class diagram consists of the following entities

  • Proxy
  • Subject
  • Real Subject
Class Diagram
Class Diagram

Remote Proxy

An ambassador appointed by a country to another acts like a proxy for his/her country. He or she acts as the communication channel between the host country and the ambassador's country. A remote proxy acts in a somewhat similar fashion and facilitates ...