Chapter 10. Proxies

The concept of a proxy is one of the most important aspects of modern programming. A proxy makes it possible to simplify many tasks that would be difficult and time-consuming otherwise. However, understanding proxies is often a difficult task because they are usually involved in much more complicated topics such as Enterprise JavaBeans (EJB) and remote method invocation (RMI). The developers of these technologies implement proxies for the users of these technologies, who are often not even aware that they are using a proxy.

What Is a Proxy?

To put it simply, a proxy is an object that stands in for another object and appears to perform the first object’s functions. The object that the proxy imitates is called the implementation object . Instead of using the implementation directly, classes that want to access the features of the implementation use a proxy. For example, in an EJB system, the implementations are across the network from the clients that want to use them. The advantage of proxies is that they allow you to insert code between the implementation and the proxy. A proxy can hide complex tasks such as network communication and transaction management from the proxy user, all without changing the implementation object’s functionality.

To better understand how proxies work, let’s look at a class that implements two simple methods (see Example 10-1).

Example 10-1. An implementation object
package oreilly.hcj.proxies; public class SomeClassImpl { private String ...

Get Hardcore Java now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.