13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

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.

CHAPTER 2: Object-Oriented Programming 27it and <strong>Objective</strong>-C are referred to as message-oriented languages. Henceforth, when you read“message” you can usually consider that term interchangeable with “method.”It is worth noting that in <strong>Objective</strong>-C a class is also a type of object; the compiler creates exactlyone object for each class at compile-time. By convention, class names begin with an uppercaseletter, such as MyObject, while the names of instances begin with a lowercase letter, such asmyObject. Class objects also have their own type, Class, and their own zero value, Nil. Note thatthese names also follow the capitalization convention mentioned previously.So if you have an instance of a class called String, you can send messages to it. You can alsosend messages directly to the String class, rather than a particular instance. It is common in<strong>Objective</strong>-C to implement factory methods on classes, which return an initialized instance ofthat class. From the point of view of the runtime library, there is no difference between sendinga message to an object instance and sending a message to a class. The only difference to youas the programmer is that each class exists only once, as itself, and is accessed through itsname—in this case String—rather than through a typed variable.<strong>Objective</strong>-C programs usually conform to some specific programming paradigms designed toassist with the encapsulation of data and logic. All of these will be explored in more detail in laterchapters of this book, but for now here’s a brief introduction to each of the patterns you’re likelyto encounter and later use when writing <strong>Objective</strong>-C code.• Delegation: The Delegation pattern specifies that one logical unit (commonlyan object) can pass off some decisions about its behavior to another objectand this object is referred to as a delegate. It implements a number ofmethods corresponding to the decisions in which it wishes to partake.• Observation: This pattern allows any interested object to receive andrespond to status updates from any other part of the system. In particular,<strong>Objective</strong>-C’s dynamic binding allows this to happen in a completelydecoupled way: it allows for the handling of discrete events without needingto explicitly forge a connection between the observer and the source of theevent.• Model-View-Controller: The <strong>Objective</strong>-C UI frameworks (AppKit on theMac, UIKit on iOS) use this pattern extensively. It provides for a separationbetween the management of data (the Model) and the presentation of thatdata (the View). In between sits the Controller and its job is to provide thebridge between the two; here is where the application’s logic resides.This pattern helps prevent inter-dependencies between presentationand data code and modules, allowing for much greater re-use of thosecomponents.• Proxying: <strong>Objective</strong>-C’s dynamic handling of messages and types atruntime allows for the use of proxy objects as intermediaries. These proxiesforward messages on to a “real” object internally, which the caller can’totherwise access. Proxies can be implemented in order to enforce additionalinterface alterations upon existing objects, such as implementing a formof synchronization over shared resources, or to provide a reference to anobject present within another system (you will see this when you learn aboutXPC later in this book). A system API might return a proxy for an internalobject that implements a restricted interface, for example.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!