Kubernate

Design Pattern # Facade with Real word Example

 Here is a clear, interview-ready explanation of the Facade Design Pattern with a real-world Java/J2EE example you can use immediately.





Facade Design Pattern (Simple Definition)

The Facade Pattern provides one simplified interface to a complex system of classes, libraries, or subsystems.

πŸ‘‰ It hides complexity and exposes only what the client needs.


πŸ’‘ Why we use Facade?

  • To simplify complex logic

  • To reduce dependencies between client and subsystems

  • To improve readability and maintainability

  • To provide a single entry point for multiple services


🎯 REAL J2EE LIVE EXAMPLE — Payment Processing System

In enterprise Java applications (Spring Boot / J2EE), the payment process involves many subsystems:

  • CardValidatorService

  • PaymentGatewayService

  • NotificationService

  • InvoiceService

  • AuditService

The client shouldn't deal with 5–6 services every time.

So we create one Facade: PaymentFacade


Image

Image

Image


FACADE DESIGN PATTERN — JAVA EXAMPLE

Subsystem Classes

class CardValidatorService {
    public boolean validateCard(String cardNumber) {
        System.out.println("Validating Card...");
        return true;
    }
}

class PaymentGatewayService {
    public void debitAmount(double amount) {
        System.out.println("Debiting Amount: " + amount);
    }
}

class InvoiceService {
    public void generateInvoice() {
        System.out.println("Invoice Generated.");
    }
}

class NotificationService {
    public void sendNotification(String msg) {
        System.out.println("Notification Sent: " + msg);
    }
}

🎯 FACADE CLASS (Single Entry Point)

public class PaymentFacade {

    private CardValidatorService validator;
    private PaymentGatewayService gateway;
    private InvoiceService invoice;
    private NotificationService notification;

    public PaymentFacade() {
        validator = new CardValidatorService();
        gateway = new PaymentGatewayService();
        invoice = new InvoiceService();
        notification = new NotificationService();
    }

    public void makePayment(String cardNumber, double amount) {
        if (validator.validateCard(cardNumber)) {
            gateway.debitAmount(amount);
            invoice.generateInvoice();
            notification.sendNotification("Payment Successful!");
        }
    }
}

πŸš€ Client Code (Super Simple)

public class Client {
    public static void main(String[] args) {
        PaymentFacade payment = new PaymentFacade();
        payment.makePayment("1234-5678-9999", 4500.00);
    }
}

Client sees only one method:
πŸ‘‰ payment.makePayment(...)

All complexity stays hidden inside the Facade.


πŸ“Œ Where Facade is used in real Java J2EE projects?

1. Spring Boot → Spring JPA Facade

Repositories hide DB complexity.

2. Hibernate

SessionFactory is a Facade for multiple DB operations.

3. JDBC Template

Hides connection, statement, resultSet handling.

4. Service Layer in Microservices

A Service Facade exposes one method for:

  • validation

  • calling multiple microservices

  • logging

  • auditing

5. REST API Gateways (API Gateway Pattern)

A single API endpoint calls multiple backend services.


πŸ“ Interview-Ready Summary

Facade Pattern
πŸ‘‰ “Provides a single simplified interface to a complex subsystem.”

Benefits:

  • Reduces coupling

  • Easy to use

  • Clean, readable code

  • Loose coupling between layers

Real example:

  • PaymentFacade in J2EE hides card validation, debit, invoice, SMS



No comments:

Post a Comment

Spring Boot - Bean LifeCycle

 Here is a clear, step-by-step lifecycle of a Spring Boot application , explained in a simple + interview-ready way. πŸ”„ Spring Boot Applica...

Kubernate