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

Java का Facade Pattern

Facade pattern code के एक बड़े, ज़्यादा जटिल हिस्से को एक single, सरल interface प्रदान करता है, कई आपस में interact करने वाली subsystem classes की coordination को एक entry point के पीछे छुपाते हुए।
Syntax
java
class Facade {
    private SubsystemA a;
    private SubsystemB b;
    void simpleOperation() {
        a.operation();
        b.operation();
    }
}

What is the Facade Pattern?

Facade pattern code के एक बड़े, ज़्यादा जटिल हिस्से, जैसे कई आपस में interact करने वाली subsystem classes, को एक single, सरल interface प्रदान करता है, बिना ज़रूरत पड़ने पर उन subsystems को सीधे उपयोग करने की क्षमता छुपाए।

उदाहरण: What is the Facade Pattern?

java
public class Main {
	static class CPU { void start() { System.out.println("CPU started"); } }
	static class Memory { void load() { System.out.println("Memory loaded"); } }
	static class ComputerFacade { // simplified interface to a complex subsystem
		void start() { new CPU().start(); new Memory().load(); }
	}
	public static void main(String[] args) {
		new ComputerFacade().start();
	}
}

Simplifying a Complex Subsystem

एक facade खासतौर पर तब उपयोगी है जब किसी task को एक specific sequence में कई स्वतंत्र services या classes coordinate करनी हों, क्योंकि यह उस पूरी coordination logic को एक सीधे method के पीछे wrap कर देता है।

उदाहरण: Simplifying a Complex Subsystem

java
public class Main {
	static class InventoryService { void checkStock() { System.out.println("Stock checked"); } }
	static class PaymentService { void charge() { System.out.println("Payment charged"); } }
	static class ShippingService { void ship() { System.out.println("Order shipped"); } }
	static class OrderFacade {
		void placeOrder() { // coordinates the whole sequence in one method
			new InventoryService().checkStock();
			new PaymentService().charge();
			new ShippingService().ship();
		}
	}
	public static void main(String[] args) {
		new OrderFacade().placeOrder();
	}
}

Facade Hides Subsystem Details

चूंकि client code सिर्फ facade के साधारण interface पर निर्भर करती है, इसके पीछे की subsystem classes को स्वतंत्र रूप से refactor, replace, या reorganize किया जा सकता है बिना किसी ऐसे code को तोड़े जो सिर्फ facade को call करता हो।

उदाहरण: Facade Hides Subsystem Details

java
public class Main {
	static class Subsystem { void internalWork() { System.out.println("Internal work done"); } }
	static class Facade {
		Subsystem subsystem = new Subsystem(); // could be swapped out freely
		void perform() { subsystem.internalWork(); }
	}
	public static void main(String[] args) {
		new Facade().perform(); // caller never touches Subsystem directly
	}
}

Multiple Facades for Different Clients

एक system एक ही underlying subsystems पर कई अलग-अलग facades उजागर कर सकता है, हर एक अलग तरह के client के लिए तैयार, जैसे एक सरल UserFacade और एक ज़्यादा शक्तिशाली AdminFacade।

उदाहरण: Multiple Facades for Different Clients

java
public class Main {
	static class UserService { void viewProfile() { System.out.println("Viewing profile"); } }
	static class AdminService { void deleteUser() { System.out.println("User deleted"); } }
	static class UserFacade { void view() { new UserService().viewProfile(); } }
	static class AdminFacade { void delete() { new AdminService().deleteUser(); } } // tailored to a different client
	public static void main(String[] args) {
		new UserFacade().view();
		new AdminFacade().delete();
	}
}

Facade vs Direct Subsystem Access

किसी facade के बिना, calling code को खुद हर subsystem class को समझना और सही तरीके से coordinate करना होता है, जबकि एक facade उस ज़िम्मेदारी को एक single, अच्छी तरह test किए entry point में समेट देता है जो उपयोग करना कहीं ज़्यादा आसान है।

उदाहरण: Facade vs Direct Subsystem Access

java
public class Main {
	static class ServiceA { void a() { System.out.println("A"); } }
	static class ServiceB { void b() { System.out.println("B"); } }
	public static void main(String[] args) {
		new ServiceA().a(); // caller must know and coordinate every subsystem itself
		new ServiceB().b();
	}
}
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. facade को एक विशाल class में बढ़ने देना जिसमें business logic हो, जबकि इसे सिर्फ subsystem को coordinate करना चाहिए।
  2. facade को subsystem तक पहुंचने का इकलौता तरीका बनाना, जिससे advanced clients ज़रूरत पड़ने पर classes को सीधे उपयोग नहीं कर सकते।
  3. ऐसे facade का उपयोग करना जब आपको सिर्फ एक असंगत interface को दूसरे में फिट कराना है, जो Adapter का काम है।

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.