session-facade/README.md
Provide a simplified interface to a complex subsystem, reducing complexity and coupling between client and business logic in enterprise Java applications.
Real-world example
A real-world analogy for the Session Facade pattern is a hotel concierge service. Guests (clients) don't directly interact with various departments like housekeeping, kitchen, transport services, or maintenance. Instead, they interact with the concierge (the facade), who simplifies these interactions. When a guest requests services like room cleaning, dinner reservations, or taxi bookings, the concierge handles communication with multiple hotel departments behind the scenes, providing a simplified and unified interface to the guest, reducing complexity and enhancing guest satisfaction.
In plain words
Session Facade provides a simplified interface to complex business logic in Java applications, reducing client complexity and minimizing network overhead by encapsulating interactions within a single session component.
Sequence diagram
The Session Facade pattern is a structural design pattern that provides a simplified interface to complex subsystems, making the system easier for clients to interact with. It is especially useful when a client needs access to multiple underlying services without needing to understand their internal complexities.
In the context of an e-commerce website, consider a scenario where users browse products, manage their shopping carts, place orders, and process payments. Rather than directly interacting with each subsystem individually (such as the cart, order, and payment systems), a client can communicate through a single unified Session Facade interface.
In this example, the ShoppingFacade class simplifies client interactions with three services: the CartService, OrderService, and PaymentService. The client uses the facade to perform high-level operations like adding products to the cart, placing an order, and choosing a payment method, without needing to know the underlying details.
Here's a simplified Java program demonstrating this pattern:
public class App {
public static void main(String[] args) {
ShoppingFacade shoppingFacade = new ShoppingFacade();
shoppingFacade.addToCart(1);
shoppingFacade.order();
shoppingFacade.selectPaymentMethod("cash");
}
}
The ShoppingFacade serves as a centralized point of interaction for various shopping-related operations, thereby reducing direct coupling between client code and individual subsystem services:
public class ShoppingFacade {
private final CartService cartService;
private final OrderService orderService;
private final PaymentService paymentService;
public ShoppingFacade() {
Map<Integer, Product> productCatalog = new HashMap<>();
productCatalog.put(1, new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
productCatalog.put(2, new Product(2, "Gaming Keyboard", 79.99, "RGB mechanical gaming keyboard with programmable keys."));
Map<Integer, Product> cart = new HashMap<>();
cartService = new CartService(cart, productCatalog);
orderService = new OrderService(cart);
paymentService = new PaymentService();
}
public Map<Integer, Product> getCart() {
return this.cartService.getCart();
}
public void addToCart(int productId) {
this.cartService.addToCart(productId);
}
public void removeFromCart(int productId) {
this.cartService.removeFromCart(productId);
}
public void order() {
this.orderService.order();
}
public Boolean isPaymentRequired() {
double total = this.orderService.getTotal();
if (total==0.0) {
LOGGER.info("No payment required");
return false;
}
return true;
}
public void processPayment(String method) {
Boolean isPaymentRequired = isPaymentRequired();
if (Boolean.TRUE.equals(isPaymentRequired)) {
paymentService.selectPaymentMethod(method);
}
}
}
When running the provided example (App.main()), the output might look similar to:
19:43:17.883 [main] INFO com.iluwatar.sessionfacade.CartService -- ID: 1
Name: Wireless Mouse
Price: $25.99
Description: Ergonomic wireless mouse with USB receiver. successfully added to the cart
19:43:17.910 [main] INFO com.iluwatar.sessionfacade.OrderService -- Client has chosen to order [ID: 1
This simplified example demonstrates the essence of the Session Facade pattern. Your actual implementation may vary based on the specific needs of your application.
Benefits:
Trade-offs: