1
Software Design
Lecture : 38
2
Proxy Design Pattern
OR
Surrogate Design Pattern
3
Sample Code fragment
//Client
class Customer{
public void someMethod(){
//Create the Service Provider Instance
FileUtil futilObj=new FileUtil();
//Access the Service
futilObj.writeToFile(“Some Data”);
}}
4
Implementation of Code
As part of its implementation, the Customer class creates
an instance of the FileUtil class and directly accesses its
services.
In other words, for a client object, the way of accessing a
FileUtil object is fairly straightforward. From the
implementation it seems to be the most commonly used
way for a client object to access a service provider object
5
Issues in Accessing Object Services
In contrast, sometimes a client object may not be able to
access a service provider object (also referred to as a target
object) by normal means may be due to:
The location of the target object – Remote Object
Uninstantiated Object till it is required to be rendered.
Special Behavior: Client object need special permission for
access
6
Solution and Motivation for
Proxy
In such cases, instead of having client objects to deal
with the special requirements for accessing the target
object, the Proxy pattern suggests using a separate object
referred to as a proxy to provide a means for different
client objects to access the target object in a normal,
straightforward manner.
7
Intend of Proxy Design Pattern
The intent of this pattern is to provide a
placeholder for an object to control references to
it.
8
Proxy Pattern Defined
“ Proxy design pattern provides a surrogate or
placeholder for another object to control
access to it “
9
Working of Proxy Design Pattern
The Proxy object offers the same interface as the target
object.
The Proxy object interacts with the target object on
behalf of a client object and takes care of the specific
details of communicating with the target object.
10
Proxy Design Pattern
Client objects are no longer needed to deal with the
special requirements for accessing the services of the
target object.
11
Working of Proxy Design Pattern
A client can call the Proxy object through its interface
and the Proxy object in turn forwards those calls to the
target object.
Client objects need not even know that they are dealing
with Proxy for the original object.
12
Proxy Design Pattern
The Proxy object hides the fact that a client object is
dealing with an object that is either remote, unknown
whether instantiated or not, or needs special
authentication.
In other words, a Proxy object serves as a transparent
bridge between the client and an inaccessible remote
object or an object whose instantiation may have been
deferred
13
Type of Proxies
Virtual Proxies: delaying the creation and
initialization of expensive objects until needed, where the
objects are created on demand (For example creating the
RealSubject object only when the doSomething method is
invoked).
Remote Proxies: providing a local representation for
an object that is in a different address space. A common
example is Java RMI stub objects. The stub object acts as
a proxy where invoking methods on the stub would cause
the stub to communicate and invoke methods on a
remote object (called skeleton) found on a different
machine
14
Protection Proxies: where a proxy controls
access to RealSubject methods, by giving access
to some objects while denying access to others.
Smart References: providing a sophisticated
access to certain objects such as tracking the
number of references to an object and denying
access if a certain number is reached, as well as
loading an object from database into memory on
demand.
•
15
Class Diagram
16
17
Flow of Application
A client obtains a reference to a Proxy, the client then handles
the proxy in the same way it handles RealSubject and thus
invoking the method doSomething().
At that point the proxy can do different things prior to
invoking RealSubjects doSomething() method. The client
might create a RealSubject object at that point, perform
initialization, check permissions of the client to invoke the
method, and then invoke the method on the object. The client
can also do additional tasks after invoking the doSomething()
method, such as incrementing the number of references to the
object.
18
Remote Method Invocation RMI
Remote Interface — A remote object must
implement a remote interface (one that extends
java.rmi.Remote). A remote interface declares
the methods in the remote object that can be
accessed by its clients. In other words, the
remote interface can be seen as the client’s view
of the remote object
19
Remote Object — A remote object is responsible for
implementing the methods declared in the associated
remote interface.
RMI Registry — RMI registry provides the storage area
for holding different remote objects. It is directory of
remote objects with reference name which client can
access
Client — Client is an application object attempting to
use the remote object.
– Can search for a remote object using a name
reference in the RMI
Registry. Once the remote object reference is found, it
can invoke methods on this object reference.
20
RMIC Java RMI Stub Compiler :
Once a remote object is compiled successfully,
RMIC, the Java RMI stub compiler can be used to
generate stub and skeleton class files for the
remote object. Stub and skeleton classes are
generated from the compiled remote object class.
These stub and skeleton classes make it possible
for a client object to access the remote
object in a seamless manner.
21
Communication Mechanism
i.
In general, a client object cannot directly
access a remote object by normal means.
ii. In order to make it possible for a client object
to access the services of a remote object as if it
is a local object, the RMICgenerated stub of
the remote object class and the remote
interface need to be copied to the client
computer.
22
iii. The stub acts as a (Remote) proxy for the remote object
and is responsible for forwarding method invocations on
the remote object to the server where the actual remote
object implementation resides. Whenever a client
references the remote object, the reference is, in fact,
made to a local stub. That means, when
a client makes a method call on the remote object, it is first
received by the local stub instance. The stub forwards
this call to the remote server. On the server the RMIC
generated skeleton of the remote object receives this call.
As shown in diagram next
23
24
Example of Proxy Design Pattern
Consider an image viewer program that lists and
displays high resolution photos. The program has
to show a list of all photos however it does not
need to display the actual photo until the user
selects an image item from a list.
25
Class Diagram