Unformatted text preview: Pattern Composite pattern (cont
3. Overgenerality d) Composite pattern (cont d) How do components avoid deGining methods that don t make sense for some leafs? (Especially methods that make sense only for composites, e.g., format, group, ungroup?) Possibly view leafs as components with no children 4. Where do you deGine child management methods? In root: transparent, but leafs may do silly things In composites: safer because leafs do not respond to method, but loss of uniformity 39 5. Should components implement list of components? e.g., as an instance variable? probably yes, except that leafs would use this variable 6. Child ordering Sometimes an issue (e.g., a row of Lexi text) 7. Caching to improve performance Save I/O data to avoid recomputation 8. Who should delete components? Probably the responsibility of the composite 9. Choice of a data structure to store children 40 Case study Pattern Formatting Lexi Documents Handle justiGication, indentation, margins, line breaking, etc. Many good algorithms exist; different tradeoffs between quality and speed of formatting Design decision: implement different algorithms, decide at run- time what algorithm to use Design goal: maintain independence (orthogonality) between formatting code and
representation code (glyphs) DeGine root class that supports many algorithms, each algorithm implemented in a subclass 41 Compositor & Composition Classes Relevant design decisions: 1. Compositor: class of objects that incorporate a formatting algorithm 2. Pass objects to be formatted as parameters to compositor methods 3. Parameters are instances of a Glyph subclass called Composition provide a uniform interface to all formattable objects to compositor algorithms 42 Pattern Compositor & Composition Classes Relevant design decisions (cont d): 4. Each Composition instance has reference (e.g., instance variable) to compositor object When composition needs to format itself, it sends appropriate message to its compositor instance Diagram of Composite Pat...
View Full Document
- Spring '12
- Object-Oriented Programming, Design pattern, Subroutine