Mental Model: Tiny Text Editor in Java

You already know if, for, and methods. The missing piece is usually this: “How do I think about the whole program?”

Here’s the mental model for this editor.

Think in Three Boxes

  1. Document state: the lines in memory (List<String>) plus a dirty flag.
  2. Command loop: read one command, run one action, print result.
  3. Persistence boundary: load once at startup, save when asked.

That’s it. Most beginner tools are this pattern.

Core Data Model

In Java terms, your EditorState object is the source of truth.

  • lines is the document.
  • dirty answers: “Do I have unsaved changes?”
  • file is where state gets persisted.

If you keep these in one class, your brain has one place to look.

Control Flow You Can Trace

A clean way to reason about each iteration is:

  • read input
  • validate input
  • mutate state (or not)
  • show feedback

That loop is your app “heartbeat.” If something feels broken, step through one heartbeat at a time.

Java-Specific Insight

Java helps you separate responsibilities with small methods:

  • append, edit, and delete mutate data
  • show renders output
  • load and save cross the file I/O boundary

That separation is more important than fancy architecture.

Build Order That Keeps You Sane

Build in this order:

  1. show + append
  2. edit + delete
  3. save + load
  4. dirty protection on quit

Each step gives a working program, not a giant half-finished one.

Common Beginner Traps

  • Mixing parsing with business logic in one giant switch branch.
  • Updating state in multiple places instead of one state class.
  • Skipping bounds checks on line numbers.

If you avoid these three, your editor stays understandable.

What to Practice Next

Add one feature at a time: search, replace, or undo (single-level). When you can add a feature without breaking the loop mental model, you’re thinking like a builder.