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");
}
}
Comments
Post a Comment