Mental Model: TUI Word Processor in Java
A word processor sounds big, but your terminal version is just “text editor plus a few document operations.”
Mental Model in Layers
Think about this tool in layers, not as one giant program.
- Document layer: list of lines and text operations.
- Metrics layer: derive stats like word count from current lines.
- CLI layer: map commands to document methods.
When stuck, ask: which layer is this bug in?
Document Is the Product
In Java, the Document class is your product core.
append,insert,edit,deleteare editing primitives.statsis read-only computation over current state.dirtytracks unsaved edits.
Everything else is just delivery plumbing.
Command Loop as a Router
The main loop should route commands, not do business logic.
- Parse command and line number safely.
- Delegate to
Document. - Print user feedback.
If the loop becomes huge, move logic down into methods.
Java-Specific Insight
Java nudges you toward explicit method boundaries. Use that to your advantage.
- Keep mutation methods tiny and predictable.
- Keep file I/O in
loadandsaveonly. - Keep input parsing (
parseLine) isolated.
This gives you code you can reason about with confidence.
Practical Build Sequence
- Start with
appendandshow. - Add
insert,edit, anddeletewith bounds checks. - Add
stats. - Add
save,load, and unsaved-quit warning.
That sequence mirrors how real text tools evolve.
Signs Your Model Is Working
- You can explain where data lives in one sentence.
- You can add a new command without touching everything.
- You can debug by tracing state before and after one command.
That’s a strong beginner architecture.