Design patterns and domain modeling

Responsibility refactoring

Separate calculation from formatting. Executed Java 21 fixture.

Formal Syntax & Structure

java syntaxjava
render(total(n))

Working Example

Example Snippetjava
public class Main {    static String before(int n) { return "Total: " + Math.multiplyExact(n, 250); }    static int total(int n) { return Math.multiplyExact(n, 250); }    static String render(int cents) { return "Total: " + cents; }    public static void main(String[] args) {        for (int n : new int[]{0, 1, 4}) {            String old = before(n);            String updated = render(total(n));            if (!old.equals(updated)) throw new AssertionError(n);            System.out.println(updated);        }    }}
Java Reference