Java का Proxy Pattern
In this page:
class Proxy implements Subject {
private RealSubject real;
public void request() {
// access control / lazy loading
real.request();
}
}
What is the Proxy Pattern?
Proxy pattern एक असली object जैसा ही interface implement करने वाला एक stand-in या placeholder object प्रदान करता है, उस तक access control करते हुए, और delegated call के आस-पास lazy loading, access control, या logging जैसा behavior जोड़ते हुए।
उदाहरण: What is the Proxy Pattern?
public class Main {
interface Image { void display(); }
static class RealImage implements Image {
public void display() { System.out.println("Displaying real image"); }
}
static class ProxyImage implements Image {
RealImage real = new RealImage();
public void display() {
System.out.println("Logging access"); // adds behavior around the delegated call
real.display();
}
}
public static void main(String[] args) {
Image img = new ProxyImage();
img.display();
}
}
Login to try C/C++/Java/PHP code in the editor
Virtual Proxy (Lazy Loading)
एक virtual proxy किसी महंगे object के बनने को तब तक टाल देता है जब तक असल में इसकी ज़रूरत न हो, सिर्फ तभी असली object बनाते हुए जब इसके किसी method को पहली बार वाकई call किया जाए, इसे कभी उपयोग न होने पर resources बचाते हुए।
उदाहरण: Virtual Proxy (Lazy Loading)
public class Main {
interface Image { void display(); }
static class RealImage implements Image {
RealImage() { System.out.println("Expensive load happening now"); }
public void display() { System.out.println("Displaying"); }
}
static class VirtualProxy implements Image {
RealImage real;
public void display() {
if (real == null) real = new RealImage(); // constructed only when first needed
real.display();
}
}
public static void main(String[] args) {
Image img = new VirtualProxy();
System.out.println("Proxy created, real image not loaded yet");
img.display();
}
}
Login to try C/C++/Java/PHP code in the editor
Protection Proxy (Access Control)
एक protection proxy किसी request को असली object तक पहुंचने की अनुमति देने से पहले यह जांचता है कि caller authorized है या नहीं, एक security gatekeeper की तरह काम करते हुए जो असली object को शामिल किए बिना ही access को पूरी तरह deny कर सकता है।
उदाहरण: Protection Proxy (Access Control)
public class Main {
interface Document { void open(); }
static class RealDocument implements Document {
public void open() { System.out.println("Document opened"); }
}
static class ProtectionProxy implements Document {
RealDocument real = new RealDocument();
boolean isAdmin;
ProtectionProxy(boolean isAdmin) { this.isAdmin = isAdmin; }
public void open() {
if (isAdmin) real.open(); else System.out.println("Access denied"); // gatekeeper
}
}
public static void main(String[] args) {
new ProtectionProxy(false).open();
}
}
Login to try C/C++/Java/PHP code in the editor
Remote Proxy
एक remote proxy किसी ऐसे object को represent करता है जो किसी अलग machine या process पर रहता है, network communication और serialization की details को caller को एक साधारण local method call जैसा दिखने वाले के पीछे छुपाते हुए।
उदाहरण: Remote Proxy
public class Main {
interface Service { String call(); }
static class RemoteProxy implements Service {
public String call() {
return "Simulated network response"; // hides network details behind a local-looking call
}
}
public static void main(String[] args) {
Service service = new RemoteProxy();
System.out.println(service.call());
}
}
Login to try C/C++/Java/PHP code in the editor
Java's Dynamic Proxy
Java java.lang.reflect.Proxy के ज़रिए dynamic proxies के लिए built-in support प्रदान करता है, जो किसी भी interface के लिए runtime पर एक proxy class generate करता है और हर method call को एक single InvocationHandler से route करता है, बिना हाथ से एक dedicated proxy class लिखे।
उदाहरण: Java's Dynamic Proxy
import java.lang.reflect.*;
public class Main {
interface Greeter { String greet(); }
public static void main(String[] args) {
Greeter proxy = (Greeter) Proxy.newProxyInstance(
Main.class.getClassLoader(),
new Class[]{Greeter.class},
(p, method, args2) -> "Hello from dynamic proxy" // one InvocationHandler routes every call
);
System.out.println(proxy.greet());
}
}
Login to try C/C++/Java/PHP code in the editor
- proxy को असली object जैसा ही interface implement न करवाना, जिससे clients इसे उसकी जगह उपयोग नहीं कर सकते।
- किसी virtual proxy में हर call पर फिर से असली object बनाना बजाय इसे एक बार बनाकर रखने के।
- बिना किसी interface वाली class के लिए
java.lang.reflect.Proxyका उपयोग करना, जो fail हो जाता है क्योंकि dynamic proxies सिर्फ interfaces के साथ काम करते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern