The Proxy Pattern: Intent Also Known As
The Proxy Pattern: Intent Also Known As
Proxy
Pattern
l Intent
é Provide a surrogate or placeholder for another object to control access to it
l Also Known As
é Surrogate
l Motivation
é A proxy is
Ý a person authorized to act for another person
Ý an agent or substitute
Ý the authority to act for another
é There are situations in which a client does not or can not reference an
object directly, but wants to still interact with the object
é A proxy object can act as the intermediary between the client and the target
object
1
The Proxy Pattern
l Motivation
é The proxy object has the same interface as the target object
é The proxy holds a reference to the target object and can forward requests to
the target as required (delegation!)
é In effect, the proxy object has the authority the act on behalf of the client to
interact with the target object
l Applicability
é Proxies are useful wherever there is a need for a more sophisticated
reference to a object than a simple pointer or simple reference can provide
l Types of Proxies
é Remote Proxy - Provides a reference to an object located in a different
address space on the same or different machine
é Virtual Proxy - Allows the creation of a memory intensive object on
demand. The object will not be created until it is really needed.
é Copy-On-Write Proxy - Defers copying (cloning) a target object until
required by client actions. Really a form of virtual proxy.
é Protection (Access) Proxy - Provides different clients with different levels
of access to a target object
é Cache Proxy - Provides temporary storage of the results of expensive target
operations so that multiple clients can share the results
é Firewall Proxy - Protects targets from bad clients (or vice versa)
é Synchronization Proxy - Provides multiple accesses to a target object
é Smart Reference Proxy - Provides additional actions whenever a target
object is referenced such as counting the number of references to the object
Design Patterns In Java
The Proxy Pattern Bob Tarr
4
2
The Proxy Pattern
l Structure
3
Copy-On-Write Proxy Example (Continued)
l But this method may require holding the collection object's lock
for a long period of time, thus preventing other threads from
accessing the collection
l Solution 2: Have the client clone the collection prior to
performing its fetch operations. It is assumed that the collection
object is cloneable and provides a clone method that performs a
sufficiently deep copy.
l For example, java.util.Hashtable provides a clone method that
makes a copy of the hash table itself, but not the key and value
objects
l The collection lock is held while the clone is being created. But
once the clone is created, the fetch operations are done on the
cloned copy, without holding the original collection lock.
l But if no other client modifies the collection while the fetch
operations are being done, the expensive clone operation was a
wasted effort!
4
Copy-On-Write Proxy Example (Continued)
5
Copy-On-Write Proxy Example (Continued)
// The proxy.
public class LargeHashtable extends Hashtable {
// Constructor
public LargeHashtable() {
theHashTable = new ReferenceCountedHashTable();
}
6
Copy-On-Write Proxy Example (Continued)
// Constructor
public ReferenceCountedHashTable() {
super();
}
7
Copy-On-Write Proxy Example (Continued)
}
Design Patterns In Java
The Proxy Pattern Bob Tarr
15
8
Synchronization Proxy Example
9
Synchronization Proxy Example (Continued)
Table realTable;
Integer[] locks;
10
Virtual Proxy Example
l Scenario: A Java applet has some very large classes which take a
long time for a browser to download from a web server. How can
we delay the downloading of these classes so that the applet starts
as quickly as possible?
l Solution: Use a Virtual Proxy!
l When using a Virtual Proxy:
é All classes other than the proxy itself must access the target class indirectly
through the proxy. If any class makes a static reference to the target class,
the Java Virtual Machine will cause the class to be downloaded. This is
true even if no instantiation of the target class is done.
11
Virtual Proxy Example (Continued)
// Constructor
public LargeClassProxy(String title) {
this.title = title;
}
12
Virtual Proxy Example (Continued)
13
Virtual Proxy Example (Continued)
// Do other things...
System.out.println("Doing other things...");
14