Design patterns and domain modeling

Decorator

Add local audit behavior around a price lookup. Executed Java 21 fixture.

Formal Syntax & Structure

java syntaxjava
Price audited

Working Example

Example Snippetjava
public class Main {    interface Price { int cents(String sku); }    static class Legacy {        int centsFor(String code) {            if (!code.equals("PEN")) throw new IllegalArgumentException("unknown");            return 250;        }    }    public static void main(String[] args) {        Legacy legacy = new Legacy();        Price adapted = sku -> legacy.centsFor(sku.toUpperCase(java.util.Locale.ROOT));        Price audited = sku -> {            int cents = adapted.cents(sku);            System.out.println("quoted " + sku);            return cents;        };        System.out.println(audited.cents("pen"));    }}
Java Reference