TC Text Editor is a sleek, high-performance Java Swing application showcasing the Flyweight Design Pattern. Built with clean code and Maven, it delivers an intuitive UI and extreme memory efficiency—ideal for editing large documents with thousands of styled characters.

- Rich Text Editing: Bold, italic, color, font family & size controls
- Sample Content: Preloaded header, body, and footer showcasing multiple fonts
- Footer Branding: Customizable footer with your details
- Full-Screen Mode: Maximized window on startup for distraction-free writing
- Flyweight Efficiency: Shared formatting flyweights minimize memory overhead
-
Clone the repo
git clone https://github.com/Tharindu714/Flyweight-Text-Editor-Application.git cd Flyweight-Text-Editor-Application
-
Build the JAR
mvn clean package
In a large document, thousands of characters share identical styling. Without Flyweight, each character would carry its own formatting object, skyrocketing memory usage.
-
Intrinsic State (Flyweight)
-
Defined in
CharacterFormat
:fontFamily
,fontSize
,bold
,italic
,color
-
Shared: Only one instance per unique style combination.
-
-
Extrinsic State (Context)
-
Managed by
TextCharacter
:character
glyph,line
,column
position
-
Provided at runtime, kept separate from formatting.
-
-
Flyweight Factory
CharacterFormatFactory
maintains a cacheMap<String,CharacterFormat>
.- On
getFormat(...)
, it returns existing flyweight or creates a new one if needed.
-
Document Structure
Document
aggregatesTextCharacter
contexts.TextEditorUI
captures keystrokes, retrieves the currentStyledDocument
attributes, and adds characters to the model using shared formats.
By reusing CharacterFormat
objects, the application uses orders of magnitude less memory and reduces garbage collection overhead—perfect for real-world text editors handling enormous documents.
src/main/java/
├─ TextEditorUI.java # Swing GUI & user interaction
├─ TextEditorApp.java # (Optional CLI demo)
├─ CharacterFormat.java # Flyweight class (intrinsic state)
├─ CharacterFormatFactory.java # Flyweight factory & cache
├─ TextCharacter.java # Context class (extrinsic state)
└─ Document.java # Aggregator of TextCharacters
- Launch the editor and explore sample text.
- Select text to change fonts, sizes, colors, and styles.
- Observe memory savings by printing
formatFactory.getTotalFormats()
in your code.
Embrace the power of Flyweight—write more, waste less!
© 2025 Tharindu Chanaka | github.com/Tharindu714