Video summary
Low Level Design of Text Editor like MS Word using Flyweight Design Pattern
Main summary
Key takeaways
Topic / Goal
The video presents a low-level design (LLD) of a text editor (similar to MS Word) focusing on implementing a text document that supports:
- Adding characters at arbitrary (row, column) positions with style attributes
- Getting style for a specific character
- Reading a full line/row as a string
- Deleting a character from a specific (row, column)
It frames this as CodeGym question #9 and uses Java inside CodeGym’s provided testing harness (no custom main needed).
Data Model & Style Parameters (Intrinsic Style)
Each inserted character has a style defined by only 4 parameters (simplified for the problem):
- Font name (e.g., Tahoma, Times New Roman, Algerian)
- Font size
- Bold or not
- Italic or not
The style is returned as a string by getStyle(row, col).
Required Operations / Methods to Implement
The Solution class must implement these core methods:
-
addCharacter(character, style, row, col)- Inserts the character at the given position.
- Shifts existing characters right if inserting inside an existing row/column.
- If the document doesn’t have enough rows/columns, it extends by appending new rows/expanding the row structure.
-
getStyle(row, col)- Returns the style string for the character at (row, col).
- If out of bounds or no character exists: returns an empty/MT string.
-
readLine(row)- Returns the entire row as a string (all characters in that row).
- If out of bounds or empty row: returns MT string.
-
deleteCharacter(row, col)- Deletes the character at (row, col) if present.
- Returns
trueif deletion happened, otherwisefalse. - After deletion, shifts remaining characters left.
Note: It also mentions a default “initializer/constructor” that shouldn’t be modified.
Key Design Choice: Flyweight Pattern to Reduce Memory
The major technical insight is using the Flyweight Design Pattern to exploit the constraint:
- The number of possible styles is relatively small (e.g., 100–200)
- The number of characters can be huge (e.g., up to 1 million)
Flyweight Split
- Intrinsic (shared, intrinsic) data: style + character identity (stored once per unique style)
- Extrinsic (varying) data: position-specific information (row/column placement stored separately via references)
Memory Benefit Reasoning (Approximate)
- Without flyweight: store style+character per character → large RAM usage
- With flyweight: store one shared style object per unique style, and keep references for each character position
The video gives an approximate large reduction example (on the order of ~100x) and analogizes it to Age of Empires, where many soldiers share the same underlying sprite/data.
Proposed Class Structure (LLD)
The implementation is organized into multiple conceptual classes:
-
Flyweight object (named like
Carr/ FlyWeight)- Holds intrinsic data: character + font name + font size + bold + italic
- Methods used:
getCharacter()(used byreadLine)getStyle()/style()(used bygetStyle)
-
Flyweight Factory (
Care FlyWeightFactory)- Maintains a map from a unique key (built from style parameters) → flyweight instance
createStyle(...):- Builds a key from font name, size, bold, italic
- If key exists, returns the existing shared flyweight
- Otherwise creates and stores a new one
-
TextRow- Represents one row as a wrapper around a list/array structure
- Stores flyweight objects in column order
addCharacterinTextRowperforms the “insert + shift right” behavior
-
Solutionclass- Maintains the whole document as a list of
TextRow - Implements the required CodeGym API methods by delegating to the factory + rows
- Maintains the whole document as a list of
Algorithmic Behavior Details
-
Insertion (
addCharacter)- Extends rows when needed.
- If inserting at an existing row,
TextRow.addCharacter()inserts atcoland shifts everything right. - If the row has fewer columns than the insertion column, it appends at the end.
-
Style retrieval (
getStyle)- Validates row/column bounds.
- Retrieves the flyweight from
TextRowat that column. - Returns style from flyweight’s
getStyle/style.
-
Read line (
readLine)- Collects the flyweights for the row.
- Calls
getCharacter()on each to build the output string.
-
Deletion (
deleteCharacter)- Validates bounds.
- Removes from the row list and shifts remaining elements left.
Testing / CodeGym Execution Approach
- Use the provided default CodeGym template.
- Fill in only the
Solutionmethods. - Comment/uncomment helper code carefully to avoid compilation errors locally.
The code is then tested via CodeGym’s testing engine:
- Shows a sample test passing quickly
- Indicates all required tests passed (claims up to “16 tests”)
Main Speakers / Sources
- Speaker: The video narrator/author presenting the solution walkthrough (name not provided in subtitles)
- Primary source/tutorial context: CodeGym (Question #9, Text Editor LLD using Flyweight) and its Java testing harness