Real World Example

Swiggy-style Pluggable Pricing Rules

Food-delivery apps change pricing rules all the time: surge on rainy nights, free delivery above a cart value, coupon codes. Keeping each rule as a lambda in a list lets the team add or remove rules without touching the checkout code.

Example

Every rule is a Function<Order, Fee>; the checkout just applies all of them.

import java.util.*;
import java.util.function.*;

public class Main {
    public static void main(String[] args) {
        Map<String, Function<Order, Integer>> feeRules = new LinkedHashMap<>();
        feeRules.put("Delivery fee", o -> o.cartValue() >= 499 ? 0 : 40);
        feeRules.put("Rain surge", o -> o.raining() ? 25 : 0);
        feeRules.put("Late-night fee", o -> o.hour() >= 23 ? 30 : 0);
        feeRules.put("Platform fee", o -> 5);

        Predicate<Order> eligibleForCoupon = o -> o.cartValue() >= 300;

        List<Order> orders = List.of(new Order("A101", 350, true, 20), new Order("A102", 650, false, 23));
        for (Order o : orders) {
            System.out.println("Order " + o.id() + " (cart Rs " + o.cartValue() + ")");
            int total = o.cartValue();
            for (var rule : feeRules.entrySet()) {
                int fee = rule.getValue().apply(o);
                if (fee > 0) System.out.println("  + " + rule.getKey() + ": " + fee);
                total += fee;
            }
            if (eligibleForCoupon.test(o)) { total -= 50; System.out.println("  - Coupon SAVE50: 50"); }
            System.out.println("  Total: Rs " + total);
        }
    }
}

record Order(String id, int cartValue, boolean raining, int hour) {}

Output

Order A101 (cart Rs 350)
  + Delivery fee: 40
  + Rain surge: 25
  + Platform fee: 5
  - Coupon SAVE50: 50
  Total: Rs 370
Order A102 (cart Rs 650)
  + Late-night fee: 30
  + Platform fee: 5
  - Coupon SAVE50: 50
  Total: Rs 635

Where Else

  • Spring: RouterFunction handlers, @Bean Function<Order, Invoice> in Spring Cloud Function, Spring Security lambda DSL.
  • Android: click listeners button.setOnClickListener(v -> ...).
  • Banking apps: validation rules as Predicate<Transaction> (limit check, KYC check, fraud score).