WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Basic · question 13 of 100

What is the Facade pattern, and how does it simplify complex systems?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

The Facade pattern is a software design pattern that provides a simple interface to a complex system or a set of interfaces that together make up a complex system. It is used to hide the complexity of the system and provide a simplified interface to clients.

The Facade pattern provides a single, unified interface to a set of interfaces in a subsystem, making it easier to use. It encapsulates a set of subsystems and provides a simple interface to access them. This makes it easier for clients to use the subsystems without needing to understand the complexities of each subsystem.

The Facade pattern simplifies complex systems in several ways:

- It reduces the complexity by providing a simplified interface.

- It decouples the subsystems from the clients, making the subsystems easier to modify and maintain.

- It promotes code reuse, as the subsystems can be reused in other parts of the system without needing to rewrite the code.

- It improves the system’s performance by reducing the number of direct interactions between the clients and subsystems.

A simple example of the Facade pattern can be illustrated with a Banking system. The banking system is composed of different subsystems such as Account, Transaction, and Loan. The Account subsystem handles account-related operations such as creating an account, withdrawing money, and depositing money. The Transaction subsystem handles transaction-related operations such as transferring money between accounts. The Loan subsystem handles loan-related operations such as approving and disbursing loans. Without using Facade pattern, the client would need to interact with each of these subsystems individually, which would make the system difficult to understand and use. However, using the Facade pattern, a simplified interface can be provided to clients that encapsulates the complexities of each subsystem. In this way, the client need only interact with a single interface to access all the required features of the system.

Here is an example of how the Facade pattern can be implemented in Java:

//Subsystem 1: Account
class Account {
    public void createAccount() {
        System.out.println("Account created.");
    }

    public void depositMoney() {
        System.out.println("Money deposited.");
    }

    public void withdrawMoney() {
        System.out.println("Money withdrawn.");
    }
}

//Subsystem 2: Transaction
class Transaction {
    public void transferMoney() {
        System.out.println("Money transferred.");
    }
}

//Subsystem 3: Loan
class Loan {
    public void approveLoan() {
        System.out.println("Loan approved.");
    }

    public void disburseLoan() {
        System.out.println("Loan disbursed.");
    }
}

// Facade class
class BankFacade {
    private Account account;
    private Transaction transaction;
    private Loan loan;

    public BankFacade() {
        account = new Account();
        transaction = new Transaction();
        loan = new Loan();
    }

    public void createAccount() {
        account.createAccount();
    }

    public void depositMoney() {
        account.depositMoney();
    }

    public void withdrawMoney() {
        account.withdrawMoney();
    }

    public void transferMoney() {
        transaction.transferMoney();
    }

    public void approveLoan() {
        loan.approveLoan();
    }

    public void disburseLoan() {
        loan.disburseLoan();
    }
}

// Client code
public class Main {
    public static void main(String[] args) {
        BankFacade bank = new BankFacade();
        bank.createAccount();
        bank.depositMoney();
        bank.withdrawMoney();
        bank.transferMoney();
        bank.approveLoan();
        bank.disburseLoan();
    }
}

In the above example, the BankFacade class provides a simplified interface to the Account, Transaction, and Loan subsystems. The client only needs to interact with the BankFacade class to perform all the necessary banking operations, while the BankFacade class handles the complexities of the subsystems.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics