Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The Decorators pattern adds common reusable capabilities to objects via composition as an alternative to adding capabilities via subclassing. Decorators can be added or configured at runtime in contrast to defining subclasses at compile time. Cocoa’s NSScrollView class is a prominent example of the Decorator pattern. Scrolling is a technique that enables users to control which portion of an object is visible when the object is too large to see in its entirety. Rather than reimplement scrolling capability in every object that draws, Cocoa provides scrolling by decorating objects with an NSClipView instance, which in turn is decorated by an NSScrollView instance. The NSClipView hides portions of the view it decorates. The NSScrollView decorates the clip view and provides NSScroller instances as needed. NSScrollView coordinates the clip view and the scrollers to indicate and control the visible portion of object decorated by the clip view. Figure 23.1 illustrates the typical composition of objects used by Cocoa to enable graphical scrolling. The NSScrollView, NSClipView, and NSScroller instances decorate an NSImageView instance by providing a border, clipping, and scrollers that enable user-controlled scrolling.