Design a Text Editor/Word Processor like Microsoft Word

May 26, 2026
lldjava

On this page

Design a Text Editor/Word Processor like Microsoft Word

Problem Statement

Design a text editor / word processor system similar to Microsoft Word or Google Docs that allows users to create, edit, and format documents. The system should support real-time editing structure, document versioning, and basic collaborative-ready architecture design.

Asked In Companies

Microsoft Google Adobe Atlassian Amazon Meta Apple Salesforce Oracle Cisco Intuit LinkedIn


Functional Requirements

  • Users should be able to create and edit documents
  • Support text insertion, deletion, and formatting
  • Maintain document structure (paragraphs, lines, characters)
  • Support undo/redo operations
  • Maintain document version history
  • Support multiple users editing the same document (design-ready)

Objects Required

  • Document
  • Paragraph
  • Line
  • Character
  • User
  • EditorService
  • Operation
  • Version

Core Enums

DocumentStatus Enum

public enum DocumentStatus {
    ACTIVE,
    ARCHIVED,
    DELETED
}

This enum defines the lifecycle state of a document. It helps manage whether a document is currently editable, stored for history, or removed from active usage.


OperationType Enum

public enum OperationType {
    INSERT,
    DELETE,
    FORMAT
}

This enum defines the type of editing operation performed on the document. It is the backbone of undo/redo and version tracking systems.


Character Class

public class Character {

    private char value;

    public Character(char value) {
        this.value = value;
    }

    public char getValue() {
        return value;
    }
}

Represents the smallest unit of a document. Every text in the editor is broken down into characters to allow fine-grained editing operations.

Method explanation:

  • Character(char value) – Constructor that initializes a character node with a single character value.
  • getValue() – Returns the stored character value for rendering or processing.

Line Class

import java.util.*;

public class Line {

    private List<Character> characters = new ArrayList<>();

    public void addCharacter(Character ch) {
        characters.add(ch);
    }

    public void deleteCharacter(int index) {
        if (index >= 0 && index < characters.size()) {
            characters.remove(index);
        }
    }

    public List<Character> getCharacters() {
        return characters;
    }
}

A line is a collection of characters. It supports insertion and deletion operations at character level, forming the foundation of text editing.

Method explanation:

  • addCharacter(Character ch) – Appends a character at the end of the line.
  • deleteCharacter(int index) – Removes a character from a specific position if index is valid.
  • getCharacters() – Returns the full list of characters in the line.

Paragraph Class

import java.util.*;

public class Paragraph {

    private List<Line> lines = new ArrayList<>();

    public void addLine(Line line) {
        lines.add(line);
    }

    public List<Line> getLines() {
        return lines;
    }
}

A paragraph is a collection of lines. It helps structure the document into readable blocks similar to real-world word processors.

Method explanation:

  • addLine(Line line) – Adds a new line to the paragraph.
  • getLines() – Returns all lines present in the paragraph.

Document Class

import java.util.*;

public class Document {

    private String documentId;
    private String title;
    private List<Paragraph> paragraphs;
    private DocumentStatus status;

    public Document(String documentId, String title) {
        this.documentId = documentId;
        this.title = title;
        this.paragraphs = new ArrayList<>();
        this.status = DocumentStatus.ACTIVE;
    }

    public void addParagraph(Paragraph paragraph) {
        paragraphs.add(paragraph);
    }

    public List<Paragraph> getParagraphs() {
        return paragraphs;
    }

    public DocumentStatus getStatus() {
        return status;
    }

    public void setStatus(DocumentStatus status) {
        this.status = status;
    }
}

The Document class is the root entity that holds all content. It manages paragraphs and maintains document state using DocumentStatus.

Method explanation:

  • Document(String documentId, String title) – Initializes a new document with ID, title, empty paragraphs list, and ACTIVE status.
  • addParagraph(Paragraph paragraph) – Appends a paragraph to the document.
  • getParagraphs() – Returns all paragraphs in the document.
  • getStatus() – Returns current document state.
  • setStatus(DocumentStatus status) – Updates document lifecycle state.

Operation Class

public class Operation {

    private OperationType type;
    private String data;

    public Operation(OperationType type, String data) {
        this.type = type;
        this.data = data;
    }

    public OperationType getType() {
        return type;
    }

    public String getData() {
        return data;
    }
}

Represents a single user action such as insert, delete, or format. This enables undo/redo functionality and version tracking.

Method explanation:

  • Operation(OperationType type, String data) – Creates an operation with type and associated payload.
  • getType() – Returns the operation type (INSERT/DELETE/FORMAT).
  • getData() – Returns the content related to the operation.

EditorService Class

import java.util.*;

public class EditorService {

    private Stack<Operation> undoStack = new Stack<>();
    private Stack<Operation> redoStack = new Stack<>();

    public void executeOperation(Operation op) {
        undoStack.push(op);
        redoStack.clear();
    }

    public void undo() {
        if (!undoStack.isEmpty()) {
            Operation op = undoStack.pop();
            redoStack.push(op);
        }
    }

    public void redo() {
        if (!redoStack.isEmpty()) {
            Operation op = redoStack.pop();
            undoStack.push(op);
        }
    }
}

EditorService manages all editing operations. It implements undo/redo using two stacks.

Method explanation:

  • executeOperation(Operation op) – Executes an operation and pushes it to undo stack while clearing redo history.
  • undo() – Reverts the last operation by moving it from undo stack to redo stack.
  • redo() – Re-applies the last undone operation.

User Class

public class User {

    private String userId;
    private String name;

    public User(String userId, String name) {
        this.userId = userId;
        this.name = name;
    }
}

Represents a user interacting with the document. In a collaborative system, multiple users can edit the same document simultaneously.

Method explanation:

  • User(String userId, String name) – Initializes a user with identity and name.

Version Class

public class Version {

    private int versionNumber;
    private String snapshot;

    public Version(int versionNumber, String snapshot) {
        this.versionNumber = versionNumber;
        this.snapshot = snapshot;
    }

    public int getVersionNumber() {
        return versionNumber;
    }
}

Represents a saved snapshot of the document at a specific time. It enables version history and rollback functionality.

Method explanation:

  • Version(int versionNumber, String snapshot) – Creates a version snapshot.
  • getVersionNumber() – Returns version sequence number.

Main Class

public enum OperationType {
    INSERT,
    DELETE,
    FORMAT
}

0


Class Diagram

Characterchar valueCharacter(char value)char getValue()LineList<Character> charactersvoid addCharacter(Character ch)void deleteCharacter(int index)List<Character> getCharacters()ParagraphList<Line> linesvoid addLine(Line line)List<Line> getLines()DocumentString documentIdString titleList<Paragraph> paragraphsDocumentStatus statusList<Version> versionsDocument(String documentId, String title)void addParagraph(Paragraph paragraph)void addVersion(Version version)List<Version> getVersions()List<Paragraph> getParagraphs()DocumentStatus getStatus()void setStatus(DocumentStatus status)Versionint versionNumberString snapshotString createdByUserIdVersion(int versionNumber, String snapshot, String createdByUserId)int getVersionNumber()UserString userIdString nameUser(String userId, String name)OperationOperationType typeString dataOperation(OperationType type, String data)OperationType getType()String getData()EditorServiceStack<Operation> undoStackStack<Operation> redoStackvoid executeOperation(Operation op)void undo()void redo()Mainstatic void main(String[] args)DocumentStatusACTIVEARCHIVEDDELETEDOperationTypeINSERTDELETEFORMATcreatesedits

Also See