← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 12 of 14

Java का Proxy Pattern

Proxy pattern एक असली object जैसा ही interface implement करने वाला एक stand-in object प्रदान करता है, उस तक access control करते हुए और lazy loading, access control, या logging जैसा behavior जोड़ते हुए।
Syntax
java
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?

java
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();
	}
}

Virtual Proxy (Lazy Loading)

एक virtual proxy किसी महंगे object के बनने को तब तक टाल देता है जब तक असल में इसकी ज़रूरत न हो, सिर्फ तभी असली object बनाते हुए जब इसके किसी method को पहली बार वाकई call किया जाए, इसे कभी उपयोग न होने पर resources बचाते हुए।

उदाहरण: Virtual Proxy (Lazy Loading)

java
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();
	}
}

Protection Proxy (Access Control)

एक protection proxy किसी request को असली object तक पहुंचने की अनुमति देने से पहले यह जांचता है कि caller authorized है या नहीं, एक security gatekeeper की तरह काम करते हुए जो असली object को शामिल किए बिना ही access को पूरी तरह deny कर सकता है।

उदाहरण: Protection Proxy (Access Control)

java
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();
	}
}

Remote Proxy

एक remote proxy किसी ऐसे object को represent करता है जो किसी अलग machine या process पर रहता है, network communication और serialization की details को caller को एक साधारण local method call जैसा दिखने वाले के पीछे छुपाते हुए।

उदाहरण: Remote Proxy

java
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());
	}
}

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

java
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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. proxy को असली object जैसा ही interface implement न करवाना, जिससे clients इसे उसकी जगह उपयोग नहीं कर सकते।
  2. किसी virtual proxy में हर call पर फिर से असली object बनाना बजाय इसे एक बार बनाकर रखने के।
  3. बिना किसी interface वाली class के लिए java.lang.reflect.Proxy का उपयोग करना, जो fail हो जाता है क्योंकि dynamic proxies सिर्फ interfaces के साथ काम करते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.