Cocoa Fundamentals Guide

Page 139

CHAPTER 4

Cocoa Design Patterns

to communicate with them for specific purposes. For example, the Foundation framework includes the NSObject, NSCopying, and NSCoding protocols, which are all very important ones. Application Kit protocols include NSDraggingInfo, NSTextInput, and NSChangeSpelling. Formal protocols implicitly require the conforming class to implement all declared methods. However, they can mark single methods or groups of methods with the @optional directive, and the conforming class may choose to implement those. They are also fragile; once you define a protocol and make it available to other classes, future changes to it (except for additional optional methods) can break those classes. Further Reading: More more information on formal protocols, see “Protocols” in The Objective-C 2.0 Programming Language.

Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Each object either handles the request or passes it to the next object in the chain.

Responder Chain The application frameworks include an architecture known as the responder chain. This chain consists of a series of responder objects (that is, objects inheriting from NSResponder or, in UIKit, UIResponder) along which an event (for example, a mouse click) or action message is passed and (usually) eventually handled. If a given responder object doesn’t handle a particular message, it passes the message to the next responder in the chain. The order of responder objects in the chain is generally determined by the view hierarchy, with the progression from lower-level to higher-level responders in the hierarchy, culminating in the window object that manages the view hierarchy or the delegate of the window object or the global application object. The exact paths of events and action messages up the responder chain is different. An application can have as many responder chains as it has windows (or even local hierarchies of views); but only one responder chain can be active at a time—the one associated with the currently active window. iPhone OS Note: UIKit implements the responder chain differently than the Application Kit. If a view is managed by a UIViewController object, the view controller becomes the next responder in the chain (and from there the event or action message passes to the view’s superview). In addition, UIKit does not support a document architecture per se, and so there are no document objects or window-controller objects in the responder chain. There is also no error-handling responder chain in iPhone OS. The Application Kit also implements a similar chain of responders for error handling. The design of the view hierarchy, which is closely related to the responder chain, adapts the Composite pattern (“Composite” (page 141)). Action messages—messages originating from control objects—are based on the target-action mechanism, which is an instance of the Command pattern (“Command” (page 140)). Uses and Limitations When you construct a user interface for a program either by using Interface Builder or programmatically, you get one or more responder chains “for free.” The responder chain goes hand in hand with a view hierarchy, which you get automatically when you make a view object a subview of a window’s content view. If you have a custom view added to a view hierarchy, it becomes part of the responder chain; if you implement the

How Cocoa Adapts Design Patterns 2008-11-19 | © 2008 Apple Inc. All Rights Reserved.

139


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.