Skip to lesson

Facade: one simple entry point

Placing a ShopLite order means five classes in the right order. If the website, the app and the admin panel each repeat that code, one forgotten step loses stock or money. What is the fix?

The problem: checkout touches five subsystems

A subsystem is a class or a group of classes with one area of work, such as inventory, pricing or payment.

Placing one ShopLite order needs five subsystems: inventory, pricing, payment, shipping and notification. Example 1 shows a caller that runs every step itself.

Example 1 · Without a facade: every caller repeats the steps

NoFacadeDemo.javaJava
import java.util.HashMap;import java.util.Map;
record PaymentResult(boolean success, String message) {}
interface PaymentGateway {    PaymentResult pay(String orderId, int rupees);    PaymentResult refund(String orderId, int rupees);}
interface NotificationService {    void send(String to, String message);}
class InventoryService {    private final Map<String, Integer> stock = new HashMap<>(Map.of("PEN-01", 50, "BAG-07", 3));
    boolean reserve(String sku, int qty) {        int left = stock.getOrDefault(sku, 0);        if (left < qty) {            return false;        }        stock.put(sku, left - qty);        System.out.println("  inventory: reserved " + qty + " x " + sku);        return true;    }
    void release(String sku, int qty) {        stock.merge(sku, qty, Integer::sum);        System.out.println("  inventory: released " + qty + " x " + sku);    }
    int stockOf(String sku) {        return stock.getOrDefault(sku, 0);    }}
class PricingService {    private final Map<String, Integer> prices = Map.of("PEN-01", 20, "BAG-07", 2999);
    int total(String sku, int qty, String coupon) {        int subtotal = prices.get(sku) * qty;        int discount = "FEST10".equals(coupon) ? subtotal / 10 : 0;        int delivery = subtotal - discount < 500 ? 49 : 0;        return subtotal - discount + delivery;    }}
class CardGateway implements PaymentGateway {    public PaymentResult pay(String orderId, int rupees) {        if (rupees > 5000) {            return new PaymentResult(false, "card limit exceeded");        }        System.out.println("  payment: charged Rs " + rupees);        return new PaymentResult(true, "paid");    }
    public PaymentResult refund(String orderId, int rupees) {        System.out.println("  payment: refunded Rs " + rupees);        return new PaymentResult(true, "refunded");    }}
class ShippingService {    String book(String orderId, String pincode) {        System.out.println("  shipping: courier booked to " + pincode);        return "AWB" + orderId.substring(1);    }}
class SmsNotificationService implements NotificationService {    public void send(String to, String message) {        System.out.println("  sms to " + to + ": " + message);    }}
public class NoFacadeDemo {    public static void main(String[] args) {        InventoryService inventory = new InventoryService();        PricingService pricing = new PricingService();        PaymentGateway payments = new CardGateway();        ShippingService shipping = new ShippingService();        NotificationService sms = new SmsNotificationService();
        // every screen that places an order must repeat these steps, in this order        String orderId = "A101";        if (!inventory.reserve("BAG-07", 1)) {            System.out.println("Out of stock");            return;        }        int amount = pricing.total("BAG-07", 1, "FEST10");        PaymentResult paid = payments.pay(orderId, amount);        if (!paid.success()) {            inventory.release("BAG-07", 1);            System.out.println("Payment failed: " + paid.message());            return;        }        String awb = shipping.book(orderId, "411001");        sms.send("9811100001", "Order " + orderId + " placed, tracking " + awb);        System.out.println("Order " + orderId + " placed for Rs " + amount);    }}
Output
  inventory: reserved 1 x BAG-07
  payment: charged Rs 2700
  shipping: courier booked to 411001
  sms to 9811100001: Order A101 placed, tracking AWB101
Order A101 placed for Rs 2700

What this code does

  • The caller must know five classes, the order of the steps and the undo rule for a failed payment. The mobile app and the admin panel would need the same block of code.
  • The total is Rs 2,999 minus the 10% coupon (Rs 299), so Rs 2,700 with free delivery above Rs 500.

The steps must run in the right order, and a failed payment must give the stock back. Without a facade, every place that takes an order (the website, the mobile app, the admin panel) repeats all of this.

Each copy is a chance to forget a step, and a change to the order of steps means editing every copy.

CheckoutFacade.placeOrder(): one call for the whole flow

A facade is one simple class in front of a group of classes. It offers the few operations most callers need and runs the steps in the right order.

CheckoutFacade holds the five subsystems and offers one method, placeOrder(request). Example 2 shows a success, a declined card and an out-of-stock order.

Example 2 · CheckoutFacade: success, a declined card, and out of stock

CheckoutFacadeDemo.javaJava
import java.util.HashMap;import java.util.Map;
record PaymentResult(boolean success, String message) {}
interface PaymentGateway {    PaymentResult pay(String orderId, int rupees);    PaymentResult refund(String orderId, int rupees);}
interface NotificationService {    void send(String to, String message);}
class InventoryService {    private final Map<String, Integer> stock = new HashMap<>(Map.of("PEN-01", 50, "BAG-07", 3));
    boolean reserve(String sku, int qty) {        int left = stock.getOrDefault(sku, 0);        if (left < qty) {            return false;        }        stock.put(sku, left - qty);        System.out.println("  inventory: reserved " + qty + " x " + sku);        return true;    }
    void release(String sku, int qty) {        stock.merge(sku, qty, Integer::sum);        System.out.println("  inventory: released " + qty + " x " + sku);    }
    int stockOf(String sku) {        return stock.getOrDefault(sku, 0);    }}
class PricingService {    private final Map<String, Integer> prices = Map.of("PEN-01", 20, "BAG-07", 2999);
    int total(String sku, int qty, String coupon) {        int subtotal = prices.get(sku) * qty;        int discount = "FEST10".equals(coupon) ? subtotal / 10 : 0;        int delivery = subtotal - discount < 500 ? 49 : 0;        return subtotal - discount + delivery;    }}
class CardGateway implements PaymentGateway {    public PaymentResult pay(String orderId, int rupees) {        if (rupees > 5000) {            return new PaymentResult(false, "card limit exceeded");        }        System.out.println("  payment: charged Rs " + rupees);        return new PaymentResult(true, "paid");    }
    public PaymentResult refund(String orderId, int rupees) {        System.out.println("  payment: refunded Rs " + rupees);        return new PaymentResult(true, "refunded");    }}
class ShippingService {    String book(String orderId, String pincode) {        System.out.println("  shipping: courier booked to " + pincode);        return "AWB" + orderId.substring(1);    }}
class SmsNotificationService implements NotificationService {    public void send(String to, String message) {        System.out.println("  sms to " + to + ": " + message);    }}
record CheckoutRequest(String orderId, String sku, int qty, String coupon, String phone, String pincode) {}
class CheckoutFacade {    private final InventoryService inventory;    private final PricingService pricing;    private final PaymentGateway payments;    private final ShippingService shipping;    private final NotificationService notifier;
    CheckoutFacade(InventoryService inventory, PricingService pricing, PaymentGateway payments,                   ShippingService shipping, NotificationService notifier) {        this.inventory = inventory;        this.pricing = pricing;        this.payments = payments;        this.shipping = shipping;        this.notifier = notifier;    }
    String placeOrder(CheckoutRequest r) {        if (!inventory.reserve(r.sku(), r.qty())) {            return r.orderId() + " failed: out of stock";        }        int amount = pricing.total(r.sku(), r.qty(), r.coupon());        PaymentResult paid = payments.pay(r.orderId(), amount);        if (!paid.success()) {            inventory.release(r.sku(), r.qty());              // undo the reservation            return r.orderId() + " failed: " + paid.message();        }        String awb = shipping.book(r.orderId(), r.pincode());        notifier.send(r.phone(), "Order " + r.orderId() + " placed, tracking " + awb);        return r.orderId() + " placed for Rs " + amount;    }}
public class CheckoutFacadeDemo {    public static void main(String[] args) {        CheckoutFacade checkout = new CheckoutFacade(new InventoryService(), new PricingService(),                new CardGateway(), new ShippingService(), new SmsNotificationService());
        System.out.println(checkout.placeOrder(                new CheckoutRequest("A101", "BAG-07", 1, "FEST10", "9811100001", "411001")));        System.out.println(checkout.placeOrder(                new CheckoutRequest("A102", "BAG-07", 2, null, "9811100002", "560001")));        System.out.println(checkout.placeOrder(                new CheckoutRequest("A103", "BAG-07", 5, null, "9811100003", "600001")));    }}
Output
  inventory: reserved 1 x BAG-07
  payment: charged Rs 2700
  shipping: courier booked to 411001
  sms to 9811100001: Order A101 placed, tracking AWB101
A101 placed for Rs 2700
  inventory: reserved 2 x BAG-07
  inventory: released 2 x BAG-07
A102 failed: card limit exceeded
A103 failed: out of stock

What this code does

  • Each caller now makes one call. The facade runs the steps in order, and for A102 (Rs 5,998, above the card limit) it gives the two bags back before returning the error.
  • A103 asks for 5 bags when only 2 are left, so the facade stops at the first step. No payment or shipping call is made.

Callers pass one CheckoutRequest record and get one result. The facade coordinates: it knows the order of the steps and what to undo when one fails, but it does not do the subsystems' work. The coupon rule stays in PricingService and the card limit stays in the gateway.

The roles:

  • Facade: the simple entry point (CheckoutFacade).
  • Subsystem classes: do the real work and do not know the facade exists.
  • Client: code that needs the whole flow (a controller, a screen, a test).

Order of execution

Order the checkout steps

Put the steps of placeOrder in the order the facade runs them.

A facade does not block direct access

The admin stock report reads InventoryService directly, although CheckoutFacade exists. Example 3 shows both using the same inventory object.

Example 3 · The admin report uses a subsystem directly

DirectAccessDemo.javaJava
import java.util.HashMap;import java.util.Map;
record PaymentResult(boolean success, String message) {}
interface PaymentGateway {    PaymentResult pay(String orderId, int rupees);    PaymentResult refund(String orderId, int rupees);}
interface NotificationService {    void send(String to, String message);}
class InventoryService {    private final Map<String, Integer> stock = new HashMap<>(Map.of("PEN-01", 50, "BAG-07", 3));
    boolean reserve(String sku, int qty) {        int left = stock.getOrDefault(sku, 0);        if (left < qty) {            return false;        }        stock.put(sku, left - qty);        System.out.println("  inventory: reserved " + qty + " x " + sku);        return true;    }
    void release(String sku, int qty) {        stock.merge(sku, qty, Integer::sum);        System.out.println("  inventory: released " + qty + " x " + sku);    }
    int stockOf(String sku) {        return stock.getOrDefault(sku, 0);    }}
class PricingService {    private final Map<String, Integer> prices = Map.of("PEN-01", 20, "BAG-07", 2999);
    int total(String sku, int qty, String coupon) {        int subtotal = prices.get(sku) * qty;        int discount = "FEST10".equals(coupon) ? subtotal / 10 : 0;        int delivery = subtotal - discount < 500 ? 49 : 0;        return subtotal - discount + delivery;    }}
class CardGateway implements PaymentGateway {    public PaymentResult pay(String orderId, int rupees) {        if (rupees > 5000) {            return new PaymentResult(false, "card limit exceeded");        }        System.out.println("  payment: charged Rs " + rupees);        return new PaymentResult(true, "paid");    }
    public PaymentResult refund(String orderId, int rupees) {        System.out.println("  payment: refunded Rs " + rupees);        return new PaymentResult(true, "refunded");    }}
class ShippingService {    String book(String orderId, String pincode) {        System.out.println("  shipping: courier booked to " + pincode);        return "AWB" + orderId.substring(1);    }}
class SmsNotificationService implements NotificationService {    public void send(String to, String message) {        System.out.println("  sms to " + to + ": " + message);    }}
record CheckoutRequest(String orderId, String sku, int qty, String coupon, String phone, String pincode) {}
class CheckoutFacade {    private final InventoryService inventory;    private final PricingService pricing;    private final PaymentGateway payments;    private final ShippingService shipping;    private final NotificationService notifier;
    CheckoutFacade(InventoryService inventory, PricingService pricing, PaymentGateway payments,                   ShippingService shipping, NotificationService notifier) {        this.inventory = inventory;        this.pricing = pricing;        this.payments = payments;        this.shipping = shipping;        this.notifier = notifier;    }
    String placeOrder(CheckoutRequest r) {        if (!inventory.reserve(r.sku(), r.qty())) {            return r.orderId() + " failed: out of stock";        }        int amount = pricing.total(r.sku(), r.qty(), r.coupon());        PaymentResult paid = payments.pay(r.orderId(), amount);        if (!paid.success()) {            inventory.release(r.sku(), r.qty());              // undo the reservation            return r.orderId() + " failed: " + paid.message();        }        String awb = shipping.book(r.orderId(), r.pincode());        notifier.send(r.phone(), "Order " + r.orderId() + " placed, tracking " + awb);        return r.orderId() + " placed for Rs " + amount;    }}
public class DirectAccessDemo {    public static void main(String[] args) {        InventoryService inventory = new InventoryService();        CheckoutFacade checkout = new CheckoutFacade(inventory, new PricingService(),                new CardGateway(), new ShippingService(), new SmsNotificationService());
        System.out.println(checkout.placeOrder(                new CheckoutRequest("A101", "PEN-01", 10, "FEST10", "9811100001", "411001")));
        // The admin tool has no checkout to do, so it skips the facade.        System.out.println("Stock report: PEN-01 = " + inventory.stockOf("PEN-01")                + ", BAG-07 = " + inventory.stockOf("BAG-07"));    }}
Output
  inventory: reserved 10 x PEN-01
  payment: charged Rs 229
  shipping: courier booked to 411001
  sms to 9811100001: Order A101 placed, tracking AWB101
A101 placed for Rs 229
Stock report: PEN-01 = 40, BAG-07 = 3

What this code does

  • The checkout and the admin report share the same InventoryService object. The report reads it directly: the facade added an easy path but closed no door.
  • Ten pens cost Rs 200, minus Rs 20 for the coupon, plus Rs 49 delivery because the order is under Rs 500: Rs 229.

A facade is a convenience, not a wall. This is a key difference from a proxy: a proxy has the same interface as the real object and controls access to it, while a facade offers a new, smaller interface and leaves the old ones open.

If you must stop direct calls, use package-private classes or Java modules, not the facade.

Predict the output

Is the subsystem still open?

A CheckoutFacade has been added. Pick what the admin report does.

The admin report reads stock without the facade.

CheckoutFacade facade = new CheckoutFacade(inventory, pricing, payments, shipping, notifier);
System.out.println(inventory.stockOf("PEN-01"));

Avoiding a facade that becomes a god class

A god class is a class that knows and does too much, so it changes for every reason and every team edits the same file.

ShopFacade has grown placeOrder, returnOrder, addProduct, changePrice, monthlyReport and more. Example 4 splits it into two focused facades.

Example 4 · Two focused facades instead of one god facade

FocusedFacadesDemo.javaJava
import java.util.HashMap;import java.util.Map;
record PaymentResult(boolean success, String message) {}
interface PaymentGateway {    PaymentResult pay(String orderId, int rupees);    PaymentResult refund(String orderId, int rupees);}
interface NotificationService {    void send(String to, String message);}
class InventoryService {    private final Map<String, Integer> stock = new HashMap<>(Map.of("PEN-01", 50, "BAG-07", 3));
    boolean reserve(String sku, int qty) {        int left = stock.getOrDefault(sku, 0);        if (left < qty) {            return false;        }        stock.put(sku, left - qty);        System.out.println("  inventory: reserved " + qty + " x " + sku);        return true;    }
    void release(String sku, int qty) {        stock.merge(sku, qty, Integer::sum);        System.out.println("  inventory: released " + qty + " x " + sku);    }
    int stockOf(String sku) {        return stock.getOrDefault(sku, 0);    }}
class PricingService {    private final Map<String, Integer> prices = Map.of("PEN-01", 20, "BAG-07", 2999);
    int total(String sku, int qty, String coupon) {        int subtotal = prices.get(sku) * qty;        int discount = "FEST10".equals(coupon) ? subtotal / 10 : 0;        int delivery = subtotal - discount < 500 ? 49 : 0;        return subtotal - discount + delivery;    }}
class CardGateway implements PaymentGateway {    public PaymentResult pay(String orderId, int rupees) {        if (rupees > 5000) {            return new PaymentResult(false, "card limit exceeded");        }        System.out.println("  payment: charged Rs " + rupees);        return new PaymentResult(true, "paid");    }
    public PaymentResult refund(String orderId, int rupees) {        System.out.println("  payment: refunded Rs " + rupees);        return new PaymentResult(true, "refunded");    }}
class ShippingService {    String book(String orderId, String pincode) {        System.out.println("  shipping: courier booked to " + pincode);        return "AWB" + orderId.substring(1);    }}
class SmsNotificationService implements NotificationService {    public void send(String to, String message) {        System.out.println("  sms to " + to + ": " + message);    }}
record CheckoutRequest(String orderId, String sku, int qty, String coupon, String phone, String pincode) {}
class CheckoutFacade {    private final InventoryService inventory;    private final PricingService pricing;    private final PaymentGateway payments;    private final ShippingService shipping;    private final NotificationService notifier;
    CheckoutFacade(InventoryService inventory, PricingService pricing, PaymentGateway payments,                   ShippingService shipping, NotificationService notifier) {        this.inventory = inventory;        this.pricing = pricing;        this.payments = payments;        this.shipping = shipping;        this.notifier = notifier;    }
    String placeOrder(CheckoutRequest r) {        if (!inventory.reserve(r.sku(), r.qty())) {            return r.orderId() + " failed: out of stock";        }        int amount = pricing.total(r.sku(), r.qty(), r.coupon());        PaymentResult paid = payments.pay(r.orderId(), amount);        if (!paid.success()) {            inventory.release(r.sku(), r.qty());              // undo the reservation            return r.orderId() + " failed: " + paid.message();        }        String awb = shipping.book(r.orderId(), r.pincode());        notifier.send(r.phone(), "Order " + r.orderId() + " placed, tracking " + awb);        return r.orderId() + " placed for Rs " + amount;    }}
class ReturnsFacade {    private final InventoryService inventory;    private final PaymentGateway payments;    private final NotificationService notifier;
    ReturnsFacade(InventoryService inventory, PaymentGateway payments, NotificationService notifier) {        this.inventory = inventory;        this.payments = payments;        this.notifier = notifier;    }
    String returnOrder(String orderId, String sku, int qty, int paidRupees, String phone) {        inventory.release(sku, qty);        payments.refund(orderId, paidRupees);        notifier.send(phone, "Refund of Rs " + paidRupees + " started for " + orderId);        return orderId + " returned";    }}
public class FocusedFacadesDemo {    public static void main(String[] args) {        InventoryService inventory = new InventoryService();        PaymentGateway payments = new CardGateway();        NotificationService sms = new SmsNotificationService();
        CheckoutFacade checkout = new CheckoutFacade(inventory, new PricingService(), payments,                new ShippingService(), sms);        ReturnsFacade returns = new ReturnsFacade(inventory, payments, sms);
        System.out.println(checkout.placeOrder(                new CheckoutRequest("A101", "BAG-07", 1, null, "9811100001", "411001")));        System.out.println(returns.returnOrder("A101", "BAG-07", 1, 2999, "9811100001"));        System.out.println("BAG-07 stock: " + inventory.stockOf("BAG-07"));    }}
Output
  inventory: reserved 1 x BAG-07
  payment: charged Rs 2999
  shipping: courier booked to 411001
  sms to 9811100001: Order A101 placed, tracking AWB101
A101 placed for Rs 2999
  inventory: released 1 x BAG-07
  payment: refunded Rs 2999
  sms to 9811100001: Refund of Rs 2999 started for A101
A101 returned
BAG-07 stock: 3

What this code does

  • ReturnsFacade needs only three subsystems, and CheckoutFacade does not grow a returnOrder method. Each facade changes for one reason.
  • Both facades share the same subsystem objects, so the bag returned through one is back in the stock the other reads.

A facade turns into a god class when every new feature lands on it. Keep one facade per use case: CheckoutFacade for buying, ReturnsFacade for returns, CatalogFacade for the admin catalogue. They can share the same subsystem objects.

Warning signs of a god facade:

  • It has more than about 7 to 10 public methods.
  • It contains business rules (prices, limits) instead of calls to the classes that own them.
  • It needs every subsystem in the application in its constructor.

Find the bug

Spot the facade mistake

Each snippet is part of placeOrder. Click the line that breaks the facade rules.

After a declined card, the shop shows less stock than it has.

Round 1 of 2

Facade vs service layer in Spring applications

A service layer is a group of @Service classes that coordinate repositories and clients behind one simple method per use case, so a service often plays the facade role.

In a Spring application, a @Service class such as CheckoutService usually plays the facade role: the controller makes one call, and the service coordinates repositories and clients behind it.

CheckoutService.javaJava
@Servicepublic class CheckoutService {                // the facade of ShopLite's checkout    private final InventoryService inventory;    private final PricingService pricing;    private final PaymentGateway payments;    private final ShippingService shipping;    private final NotificationService notifier;    // constructor injection omitted
    @Transactional    public OrderReceipt placeOrder(CheckoutRequest request) {        // reserve, price, pay, book shipping, notify: the same steps as CheckoutFacade    }}

Its public method is also the natural place for @Transactional, so all the database work of one order commits or rolls back together.

Not every service is a facade. A service with real business rules of its own is just a service. Call it a facade when its job is mainly to give one simple entry point over several collaborators.

Next step

Check your understanding

12 quick questions