AlgoViz

Which pattern, when

medium 45 min

A pattern is an answer to one kind of pressure.

Watch lesson4:38

From force to pattern, with the cheap answer first

Read it, run it, change a line and run it again.

Loading editor…

A small decision helper. It answers forces, not words, and it refuses to name a pattern without evidence.

The same in Java
import java.util.*;record Force(String what, int examples, String differs) {}record Answer(String cheap, String pattern) {}class Chooser {    static final Map<String, Answer> ANSWERS = Map.of(        "rule", new Answer("a function parameter", "Strategy"),        "phase", new Answer("a table of moves", "State"),        "receivers", new Answer("one direct call", "Observer"));    static String choose(Force f) {        Answer a = ANSWERS.get(f.what());        if (a == null) return "plain code: no known force";        if (f.differs().equals("value")) return "a table of values";        if (f.differs().equals("moves") || f.examples() < 2) return a.cheap();        return a.pattern();    }}
The same in TypeScript
type Differs = "value" | "behaviour" | "moves";interface Force { what: string; examples: number; differs: Differs }const ANSWERS = new Map<string, [cheap: string, pattern: string]>([  ["rule", ["a function parameter", "Strategy"]],  ["phase", ["a table of moves", "State"]],  ["receivers", ["one direct call", "Observer"]],]);function choose(f: Force): string {  const answer = ANSWERS.get(f.what);  if (!answer) return "plain code: no known force";  if (f.differs === "value") return "a table of values";  return f.differs === "moves" || f.examples < 2 ? answer[0] : answer[1];}console.log(choose({ what: "rule", examples: 3, differs: "behaviour" }));