Intent
Separate the mechanics of drawing from the act of drawing.
Motivation
In building an OO graphics system you must connect to an
existing host windowing system. The host windowing system
API usually contains operations for drawing graphics
primitives (lines, rectangles, arcs, etc.) in a window.
Mapping these API calls to a set of objects often relies
upon a basic conception of the way people draw.
Structure
(* Diagram to be supplied later -- send to ward*)
Participants
- Pen
- performs drawing commands for graphics primitives (lines, arcs, rectangles)
- maintains an internal state (color, line width, etc.)
- Paper
- represents (fronts for) the operating system surface being drawn on (bitmap, screen, window)
- often responsible for creating pens
- Person
- draws meaningful shapes and figures using the Pen
Collaborations
- The Person knows how to draw a specific visual (be it a bar chart, or a simple line of text). It performs this by sending drawing commands to the Pen. The Person is isolated from the connection to a particular type of Paper, allowing the same figures to be drawn on bitmaps, screens or printers.
- The act of drawing is separted from the mechanics of how the drawing is done.
Implementation
Pen will often directly wrapper the API calls necessary
to implement the drawing primitives. Most windowing
systems require a window handle to send these calls,
so it obtains that from Paper. Paper is often subclassed
to represent the different types of graphics mediums
(bitmaps, screens and printers). Person will be subclassed
to represent each specific type of visual object.
Known Uses
Smalltalk/V and
VisualWorks utilize this pattern in their
graphics systems. In Smalltalk/V, Pen is represented by
the class Pen, Paper is represented by the class
GraphicsMedium and its subclasses. The Person is
represented by subclasses of
SubPane. In
VisualWorks Pen
is the class
GraphicsContext. Paper is the class
DisplaySurface and its subclasses. The Person is
represented by the
VisualComponent hierarchy.
IBM Smalltalk has a similar concept, but it does not
map quite as well as Smalltalk/V and
VisualWorks.
Related Patterns
The Paper, Pen and Person pattern can be combined with
Decorator and Composite to form a graphics hierarchy of
visuals that can contain other visuals.
CategoryGui CategoryUserInterface