NailYourInterview
Structural Design Patterns

Facade Design Pattern in Java with Real-World Example

Explore the Facade Design Pattern in Java with a real-world example. Understand why its needed and how it simplifies client interaction with complex subsystems.

Facade Design Pattern

Video thumbnail

The Facade Design Pattern is a structural design pattern that helps simplify complex systems. It gives users a single, easy-to-use interface to interact with many parts working behind the scenes.

A Simple Real-Life Example

Facade Design Pattern Example
Facade Design Pattern Example

Let’s say you want to withdraw money from your bank account using an ATM. You press "Withdraw," and boom — the cash comes out.

But do you know what actually happens behind the scenes?

  1. Your account is verified.

  2. Your pin is verified.

  3. Your account should have enough funds.

  4. notification is sent to you.

But you didn’t do all that manually — you just clicked Withdraw.

So who took care of all these steps for you? That’s the Facade.

Problem Without Facade

Now imagine you're building this logic in code, without knowing about the Facade pattern. Here's what your code might look like:

public class Account {
    public boolean verifyAccount(String accountNumber) {
        System.out.println("Account verified: " + accountNumber);
        return true;
    }
}

What's wrong with this approach?

Tight Coupling

The main class (Main.java) is tightly connected to every small part like Account, Security, Funds, and Notification. If any of these classes change, you'll have to change the main code too.

Code Duplication

What if you need this same withdrawal logic in 5 different places? You’ll end up copying the same logic everywhere, which makes your code harder to maintain.

The Better Way: Use the Facade Design Pattern

The Facade Pattern helps solve both of the above problems by:

  1. Hiding the complexity from the user.

  2. Giving a simple method to call, like bank.withdraw(...).

When to Use It

Use the Facade Pattern when:

  • You have a complex system with many parts.

  • You want to hide the internal steps from the client.

  • You want your code to be clean, maintainable, and easy to understand.

UML Diagram

The simplified UML for our requirement would be like this:

Facade Design Pattern UML
UML for Facade Design Pattern

Now Let's Use the Facade Pattern

We'll use the same internal classes, but now add a Facade class to handle everything.

public class BankFacade {
    private Account account;
    private Security security;
    private Funds funds;
    private Notification notification;
 
    public BankFacade() {
        account = new Account();
        security = new Security();
        funds = new Funds();
        notification = new Notification();
    }
 
    public void withdraw(String accNo, String pin, double amount) { 
        System.out.println("Starting Withdrawal Process..."); 
        if (account.verifyAccount(accNo) && security.checkPin(pin) && funds.hasSufficientFunds(amount)) { 
            funds.debit(amount); 
            notification.sendNotification("Withdrawal of $" + amount + " successful."); 
        } else { 
            System.out.println("Withdrawal Failed!"); 
        } 
    }
}

Now the client only deals with the BankFacade and doesn’t care about all the internal parts.

Main.java
public class Main {
    public static void main(String[] args) {
        BankFacade bank = new BankFacade();
        bank.withdraw("ACC123", "4321", 100.0); 
    }
}

Key Benefits of the Facade Design Pattern

On this page