From "Design Patterns"
🎧 Listen to Summary
Free 10-min PreviewDocument Structure and Glyph Hierarchy
Key Insight
Lexi, a 'What-You-See-Is-What-You-Get' (WYSIWYG) document editor, must effectively represent its internal document structure, allowing free mixing of text and graphics and direct manipulation of substructures like diagrams or tables as single units. The internal representation aligns with the document's physical structure, facilitating visual generation, presentation, and mapping display positions to internal elements. Goals include maintaining the physical arrangement of text and graphics into lines, columns, and tables, alongside enabling visual display and user interaction.
Key constraints for this design involve uniformly treating text and graphics, preventing redundant formatting mechanisms. The system must also uniformly handle both single elements, like a character, and groups of elements, such as a complex diagram, supporting documents of arbitrary complexity. This is achieved through recursive composition, a technique where increasingly complex elements are built from simpler ones. An abstract 'Glyph' class serves as the base for all document structure objects, with subclasses defining both primitive graphical elements (like characters and images) and structural elements (like rows and columns).
The 'Glyph' interface mandates three core responsibilities: knowing 'how to draw themselves' through the 'Draw' operation, defining 'what space they occupy' using the 'Bounds' operation, and managing 'their children and parent' with 'Insert', 'Remove', 'Child', and 'Parent' operations. For instance, a 'Rectangle' glyph redefines 'Draw' to render its shape and 'Intersects' to determine point intersection. This object-oriented approach, which captures the essence of recursive composition and allows for flexible, fine-grained control over document elements, embodies the Composite pattern.
📚 Continue Your Learning Journey — No Payment Required
Access the complete Design Patterns summary with audio narration, key takeaways, and actionable insights from Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.