Skip to content

Facade Pattern: Simple Interface to Complex Subsystems

Facade Pattern: Simple Interface to Complex Subsystems

The Facade Pattern is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

🏗️ The Problem

Suppose you have a complex Credit Score System involving multiple subsystems (Address Validation, Credit Score Checker, Background Check, Income Verification). If your client code calls each of these subsystems directly, it becomes tightly coupled and brittle.

🚀 The .NET Implementation

The Facade pattern wraps a complex subsystem in a single, easy-to-use class.

1. The Complex Subsystems

public class CreditScoreChecker { public int GetScore(string ssn) => 750; }
public class BackgroundCheck { public bool IsClean(string ssn) => true; }
public class IncomeVerification { public decimal GetIncome(string ssn) => 100000m; }

2. The Facade (The “Single Entry Point”)

public class LoanFacade
{
    private readonly CreditScoreChecker _score = new CreditScoreChecker();
    private readonly BackgroundCheck _bg = new BackgroundCheck();
    private readonly IncomeVerification _income = new IncomeVerification();

    public bool IsEligible(string ssn, decimal loanAmount)
    {
        Console.WriteLine($"[FACADE]: Checking eligibility for SSN: {ssn}");
        
        var score = _score.GetScore(ssn);
        var cleanBg = _bg.IsClean(ssn);
        var income = _income.GetIncome(ssn);

        // Simple eligibility rule
        return score > 700 && cleanBg && (income * 0.5m > loanAmount);
    }
}

🛠️ Real-World Usage (Client)

// The client only needs to know about the Facade!
var loanSystem = new LoanFacade();
bool result = loanSystem.IsEligible("123-45-678", 20000m);

💡 Why use Facade?

  • Simplification: Provide a clean entry point to a complex set of classes.
  • Decoupling: Protects the client from changes in the underlying subsystems.
  • Layering: You can create facades at different layers of your application.