23. Decorators

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 ...

Get Cocoa Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.