Java का Facade Pattern
In this page:
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?
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Simplifying a Complex Subsystem
एक facade खासतौर पर तब उपयोगी है जब किसी task को एक specific sequence में कई स्वतंत्र services या classes coordinate करनी हों, क्योंकि यह उस पूरी coordination logic को एक सीधे method के पीछे wrap कर देता है।
उदाहरण: Simplifying a Complex Subsystem
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Facade Hides Subsystem Details
चूंकि client code सिर्फ facade के साधारण interface पर निर्भर करती है, इसके पीछे की subsystem classes को स्वतंत्र रूप से refactor, replace, या reorganize किया जा सकता है बिना किसी ऐसे code को तोड़े जो सिर्फ facade को call करता हो।
उदाहरण: Facade Hides Subsystem Details
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
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Facades for Different Clients
एक system एक ही underlying subsystems पर कई अलग-अलग facades उजागर कर सकता है, हर एक अलग तरह के client के लिए तैयार, जैसे एक सरल UserFacade और एक ज़्यादा शक्तिशाली AdminFacade।
उदाहरण: Multiple Facades for Different Clients
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Facade vs Direct Subsystem Access
किसी facade के बिना, calling code को खुद हर subsystem class को समझना और सही तरीके से coordinate करना होता है, जबकि एक facade उस ज़िम्मेदारी को एक single, अच्छी तरह test किए entry point में समेट देता है जो उपयोग करना कहीं ज़्यादा आसान है।
उदाहरण: Facade vs Direct Subsystem Access
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();
}
}
Login to try C/C++/Java/PHP code in the editor
- facade को एक विशाल class में बढ़ने देना जिसमें business logic हो, जबकि इसे सिर्फ subsystem को coordinate करना चाहिए।
- facade को subsystem तक पहुंचने का इकलौता तरीका बनाना, जिससे advanced clients ज़रूरत पड़ने पर classes को सीधे उपयोग नहीं कर सकते।
- ऐसे facade का उपयोग करना जब आपको सिर्फ एक असंगत interface को दूसरे में फिट कराना है, जो Adapter का काम है।
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