Skip to main content

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.


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 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 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 lines = new ArrayList<>();

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

    public List 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 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 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 undoStack = new Stack<>();
    private Stack 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 class Main {

    public static void main(String[] args) {

        Document doc = new Document("D1", "My Document");

        Paragraph p1 = new Paragraph();
        Line line = new Line();

        line.addCharacter(new Character('H'));
        line.addCharacter(new Character('i'));

        p1.addLine(line);
        doc.addParagraph(p1);

        EditorService editor = new EditorService();

        Operation op1 = new Operation(OperationType.INSERT, "Hi");
        editor.executeOperation(op1);

        System.out.println("Document created with undo-redo support");
    }
}

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

Comments

Popular posts from this blog

Designing a Parking Lot - Low Level Design

Problem Statement Design a parking lot that can handle vehicles entering and leaving while managing parking across multiple floors. Each vehicle should be assigned a suitable parking spot based on its type, and the spot should be freed once the vehicle exits. The design should also support generating a ticket at entry and optionally calculating the parking fee based on the duration of stay. Asked In Companies Amazon Google Microsoft Uber Walmart Flipkart Meta PayPal Oracle Salesforce Adobe Apple Intuit LinkedIn Atlassian Functional Requirements The design should support multiple vehicle types such as bikes, cars, and trucks A vehicle must be assigned a parking spot compatible with its type A parking spot cannot be assigned to more than one vehicle at a time The parking lot should support multiple levels (floors) The design should search and allocate an availa...

Most Frequently Asked Low Level Design(LLD) Interview Questions

Below are the curated list of most commonly asked Low Level Design (LLD) interview problems. Each problem includes a short description and a link to the complete solution with code and class diagrams. Design Parking Lot System The system should handle parking for different vehicle types such as bikes, cars, and trucks. It should manage slot allocation, availability tracking, and entry/exit flow. The design also ensures efficient usage of parking space under varying load conditions. View Solution Design Elevator / Lift System The system should support multiple elevators operating across floors with request handling logic. It focuses on scheduling algorithms to minimize wait time and optimize movement. It also manages direction control and concurrent floor requests. View Solution Design Movie Ticket Booking System The system should allow users to browse movies, select shows, and book seats. It handles seat ...

Software Design Patterns for LLD Interviews: A Complete Guide

Software Design Patterns for LLD Interviews: A Complete Guide In Software Development Engineer (SDE) interviews—especially for mid-level and senior roles—low-level design (LLD) rounds assess your ability to write clean, reusable, maintainable, and extensible code. The foundation of resolving these architectural challenges lies in the standard Gang of Four (GoF) Design Patterns. Rather than memorizing theoretical definitions, interviewers expect you to apply these patterns to real-world scenarios, identifying the trade-offs of each. Below is a comprehensive guide to the 12 most frequently asked design patterns in LLD interviews, categorized by their classification (Creational, Structural, and Behavioral). Each pattern contains a concrete, real-world Java implementation and a detailed breakdown of design decisions. Creational Design Patterns Creational design patterns deal with object creation mechanisms. They abstract the instantiation process, making a system independent of how...