You are on page 1of 7

5.

Using the UML Drawing Tool by implementing the code in Java demonstrate the Proxy
Design Pattern.

The Proxy design pattern makes the clients of a component communicate with a
representative rather than to the component itself. Introducing such a placeholder can serve
many purposes, including enhanced efficiency, easier access and protection from
unauthorized access.

Structure:

Dynamics:
The following diagram shows a typical dynamic scenario of a proxy structure.
 While working on its tack the client asks the proxy to carry out a service.
 The proxy receives the incoming service request and pre-processes it. This pre-processing
involves actions such as looking up the address of the original, or checking a local cache to
see if the requested information is already available.
 If the proxy has to consult the original to fulfill the request, it forwards the request to the
original using the proper communication protocols and security measures.
 The original accepts the request and fulfills it. It sends the response back to the proxy.
 The proxy receives the response. Before or after transferring it to the client it may carry out
additional post-processing actions such as caching the result, calling the destructor of the
original or releasing a lock on a resource.
Collaboration:

The participants classes in the proxy pattern are:

 Subject - Interface implemented by the RealSubject and representing its services. The
interface must be implemented by the proxy as well so that the proxy can be used in
any location where the RealSubject can be used.
 Proxy
o Maintains a reference that allows the Proxy to access the RealSubject.
o Implements the same interface implemented by the RealSubject so that the
Proxy can be substituted for the RealSubject.
o Controls access to the RealSubject and may be responsible for its creation and
deletion.
o Other responsibilities depend on the kind of proxy.
 RealSubject - the real object that the proxy represents.

Problem Statement:

A client (ProxyExample) needs access to the services (displayImage()) of another


component (RealImage). Direct Access is technically possible, but may not be the best
approach. It is often inappropriate to access a component directly. We do not want to hard-
code its physical location into clients, and direct and unrestricted access to the component
may be inefficient or even insecure. Additional control mechanisms are needed. A solution to
such a design problem has to balance the following issue:
Accessing the component should be run-time-efficient, cost effective, and safe for both the
client and the component.
Solution: Proxy Pattern

Let the client (ProxyExample) communicate with a representative (ProxyImage)


rather than the component (RealImage) itself. This representative-called a proxy-offers the
interface of the component but performs additional pre processing and post processing
such as access-control checking or making read-only copies of the original

Class Diagram:

AbstractClass
str : String
Client1

main()

proxy Server1

ServerName() getservice()

USE-CASE MODEL:

Proxy

User

Server
ACTIVITY MODEL:

Myapp

prooxy

No
has service

Yes

Server

User

Sequence Diagram:
Client Proxy Server

1: Request for Service

2: Find in the cache

3: Get the service from Server

4: Get the Service

5: Cache the new information

6: Get the Service done


Source code

import java.io.*;
class AbstractClass
{
public String str ;
public AbstractClass()
{
str="";
}
}

class Proxy extends AbstractClass


{
public Server1 m_Server1;

public void ServerName() throws IOException


{
System.out.println(" Proxy===>");
if(str=="")
{
System.out.println(" Trying to get the service from server ");
m_Server1=new Server1();
str=m_Server1.getservice();
System.out.println(" Server says " +str);
}
else
{
System.out.println(" Proxy also cached your request ");
System.out.println(" Proxy says "+str);
}
}
}

class Server1 extends AbstractClass


{
public Proxy m_proxy;
public String getservice() throws IOException
{
System.out.println(" Server Initializing");
str=" Hello.........!";
return(str);
}
}

class Client1
{
public Proxy m_Proxy;
public static void main(String args[]) throws IOException
{
Proxy m_Proxy1=new Proxy();
System.out.println("\n\n\n 1st time Establishing Connection");
m_Proxy1.ServerName();
System.out.println("\n\n\n 2nd time Retrying for Connection");

m_Proxy1.ServerName();
}
}

Output:
1st time Establishing Connection
Proxy===>
Trying to get the service from server
Server Initializing
Server says Hello.........!

2nd time Retrying for Connection


Proxy===>
Proxy also cached your request
Proxy says Hello.........!

You might also like