If you're waiting for room in a fully booked hotel, it's easier to ask the hotel manager to tell you when a vacancy opens up than it is to ask every guest in the hotel to tell you when they leave. Likewise, when handling event dispatches, it's often more efficient to register event listeners with a display object container than it is to register with each of its descendants.
For example, suppose we're building a simple checkbox control, comprised of the following two classes:
CheckBox, a Sprite subclass that acts as a container for the entire control
CheckBoxIcon, a Sprite subclass that represents the checkbox's graphical icon
At runtime, each CheckBox
instance creates two child objects: a CheckBoxIcon instance for the checkbox's
icon and a TextField instance for
the checkbox's text label. For reference, let's call the main
container and its two children
label. Figure 21-3 shows our checkbox
Figure 21-3. Objects in the Checkbox control
We want our checkbox to be easy to use, so we design it to
toggle on or off when the user clicks either the checkbox icon or the
checkbox label. Accordingly, in our implementation, we must detect
mouse-click events targeted at both
label. To do so, we could register a separate mouse-click listener with each of those objects. However, registering two event listeners would ...