0% found this document useful (0 votes)
8 views2 pages

Proxy in Java Notes

The Proxy Design Pattern in Java provides a surrogate for another object to control access, often used for functionalities like lazy loading and logging. There are various types of proxies, including virtual, protective, remote, and smart proxies, each serving different purposes. While proxies add functionality and control access, they can also introduce complexity and latency in the code.

Uploaded by

kumaranmuthu8412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Proxy in Java Notes

The Proxy Design Pattern in Java provides a surrogate for another object to control access, often used for functionalities like lazy loading and logging. There are various types of proxies, including virtual, protective, remote, and smart proxies, each serving different purposes. While proxies add functionality and control access, they can also introduce complexity and latency in the code.

Uploaded by

kumaranmuthu8412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Proxy in Java

1. Introduction to Proxy Design Pattern


The Proxy Design Pattern is a structural design pattern that
provides a surrogate or placeholder for another object to control
access to it. In Java, proxies are often used to implement
functionalities like lazy loading, access control, logging, and
more.

2. Types of Proxies in Java


Java supports different types of proxies, including:

• - Virtual Proxy: Controls access to a resource that is


expensive to create.
• - Protective Proxy: Controls access rights to a resource based
on access permissions.
• - Remote Proxy: Represents an object in a different address
space.
• - Smart Proxy: Adds additional functionality like reference
counting, logging, etc.

3. Real-world Analogy
Imagine a celebrity who doesn't want to be contacted directly by
fans. A manager (proxy) handles communication. The fans contact
the manager, who decides whether to pass the message along.
Similarly, in programming, a proxy controls access to a resource.

4. Use Cases
• - Access control/security
• - Lazy initialization
• - Logging and auditing
• - Remote method invocation

5. Example Code (Static Proxy)


interface Service {
void performAction();
}
class RealService implements Service {
public void performAction() {
System.out.println("Real service is performing an
action");
}
}

class ProxyService implements Service {


private RealService realService = new RealService();
public void performAction() {
System.out.println("Proxy in action - Access granted");
realService.performAction();
}
}

public class Main {


public static void main(String[] args) {
Service service = new ProxyService();
service.performAction();
}
}

6. Advantages and Disadvantages


• Advantages:

• - Controls access to the original object


• - Adds additional functionality without changing actual
code

• Disadvantages:

• - Adds complexity to the code


• - May introduce latency if not implemented properly

7. Summary
The Proxy pattern in Java is a powerful concept used to manage
access to objects. By using proxies, developers can introduce
additional behavior like logging, security checks, and lazy
loading without modifying the actual business logic.

You might also like