05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

496<br />

CHAPTER 26 MAC <strong>OS</strong> X DEVELOPMENT: OBJECTIVE-C<br />

Protocols<br />

Unlike C++, Objective-C uses a single inheritance model by which a class can inherit only from a<br />

single superclass. To overcome the restrictions posed by single inheritance while adding a different<br />

sort of flexibility, Objective-C classes may inherit additional interface by conforming to any<br />

number of protocols.<br />

A protocol is like a class, except it does not contain variables or provide any implementation.<br />

Rather, it simply defines an interface, which conforming classes are expected to implement.<br />

This same concept was later used in Java but renamed interfaces.<br />

Classes can then comply with a protocol, which tells the compiler they implement any<br />

required methods. For example, some objects can be copied by simply sending them the message<br />

copy. You don’t really care what class the object is; you just care that it implements the appropriate<br />

method. To this end, Foundation defines a protocol, NSCopying. If your BMPerson class<br />

implements the copy method, you can add the protocol to your class declaration:<br />

@interface BMPerson : NSObject <br />

Nothing else in the interface changes, but now you are expected to implement the copy<br />

method.<br />

NOTE Technically when you call - (id)copy, you are calling - (id)copyWithZone:(NSZone<br />

*)zone with a default value for zone. Zone is largely an obsolete concept, but the upshot is that<br />

conforming to NSCopying requires implementing copyWithZone: instead of copy.<br />

You can also define your own protocols. Say you wanted a general interface for providing a<br />

unique ID. You start by defining your protocol, much as you would a class:<br />

@protocol BMIdentifying<br />

- (NSString *)uniqueIDString;<br />

@end<br />

If BMPerson implements uniqueIDString, you can add that to the protocol list:<br />

@interface BMPerson : NSObject <br />

Later in your design, you might want to accept some object but need to make sure it complies<br />

with your BMIdentifying protocol. You can write method signatures that use protocols as<br />

types:<br />

- (void)addIdentifyingObject:(id )object;<br />

To call this method, you can similarly cast to the protocol:<br />

[listOfIdentifyingObjects addIdentifyingObject:(id )object];<br />

Cocoa uses formal and informal protocols for several class-agnostic patterns:<br />

Plug-ins: By creating a plug-in architecture, a programmer can open their application to<br />

third-party development. For such an architecture to work, the programmer must define<br />

an interface. By using protocols, plug-in writers are given total design freedom, as long as<br />

they implement the necessary methods.<br />

Delegates and data sources: To be reusable, an object’s behavior and content must be<br />

configurable. Languages such as Java rely heavily on subclassing to accomplish this.<br />

However, this is often too big a tool for the job. The delegate and data source patterns<br />

leave behavioral and content decisions to another object. By implementing the necessary<br />

methods, programmers can control these classes, without the trouble of subclassing.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!