package support; /** * MOAD-0002: An Intertangled Defect. * * Defect: independent subsystems (Audio, Display) share a single mutable static * state object. Any write by one subsystem is immediately visible to all others. * Two independent execution contexts cannot coexist — they trample each other's * configuration silently, with no exception thrown. * * Fix: each subsystem receives an immutable Context snapshot at construction. * No shared state. N contexts exist independently, and subsystem writes * affect only the context they own. * * Scanner detects: static mutable fields accessed from multiple subsystem classes * without synchronization or phase isolation. */ public class Moad0002Algorithm { // ── Defective: shared mutable global state ──────────────────────────────── /** * God object holding all subsystem configuration. * The intertangle point: audio, display, and locale all live here. */ public static final class SharedState { public int volume = 50; public int brightness = 100; public String locale = "en"; } /** Singleton — all defective subsystems reference this one object. */ public static final SharedState GLOBAL = new SharedState(); public static final class DefectiveAudioSystem { public void setVolume(int v) { GLOBAL.volume = v; } public int getVolume() { return GLOBAL.volume; } public void setLocale(String l) { GLOBAL.locale = l; } public String getLocale() { return GLOBAL.locale; } } public static final class DefectiveDisplaySystem { public void setBrightness(int b) { GLOBAL.brightness = b; } public int getBrightness() { return GLOBAL.brightness; } public void setLocale(String l) { GLOBAL.locale = l; } public String getLocale() { return GLOBAL.locale; } } // ── Fixed: each subsystem owns an isolated immutable context ────────────── /** Immutable per-context snapshot. Passed to subsystems at construction. */ public static final class Context { public final int volume; public final int brightness; public final String locale; public Context(int volume, int brightness, String locale) { this.volume = volume; this.brightness = brightness; this.locale = locale; } } public static final class FixedAudioSystem { private final Context ctx; public FixedAudioSystem(Context ctx) { this.ctx = ctx; } public int getVolume() { return ctx.volume; } public String getLocale() { return ctx.locale; } } public static final class FixedDisplaySystem { private final Context ctx; public FixedDisplaySystem(Context ctx) { this.ctx = ctx; } public int getBrightness() { return ctx.brightness; } public String getLocale() { return ctx.locale; } } /** Reset GLOBAL between test cases so tests do not bleed into each other. */ public static void resetGlobal() { GLOBAL.volume = 50; GLOBAL.brightness = 100; GLOBAL.locale = "en"; } }