Summary: Use renderers to show sub-views.
Context
- You’ve separated the model of a complex object from its view (as in Observer or Model-View-Controller).
- The bulk of the view doesn’t change much.
- Variable items (“cells”) in the model may require a specialized sub-view.
Solution
Divide the view into two parts. Let the main view display the “frame” (the parts that tend not to vary); let the main view also maintain a reference to a “renderer” that can display each repeated (variable) part.
The renderer typically has a small interface (usually just one method), that takes as its arguments all information needed to display one cell.
Typically, each view will have a default renderer, that takes the object to display, calls toString() on it, and displays that. This way, the default view is often good enough for a first cut.
Uses
- This is a special case of the Observer or Model-View-Controller pattern.
- The Swing libraries for Java use this approach for lists, tables, and trees.
- The Swing libraries use a related idea (“editor”) for editing cells of these objects.