DekGenius.com
[ Team LiB ] Previous Section Next Section

What Is Cocoa?

Cocoa is a complete set of classes and application programming interfaces (APIs) for building Mac OS X applications and tools. With over 240 classes, Cocoa is divided into two essential frameworks: the Foundation framework and the Application Kit.

The Foundation framework provides a fundamental set of tools for representing fundamental data types, accessing operating system services, threading, messaging, and more. The Application Kit provides the functionality to build graphical user interfaces (GUI) for Cocoa applications. It provides access to the standard Aqua interface components ranging from menus, buttons, and text fields—the building blocks of larger interfaces—to complete, prepackaged interfaces for print dialogs, file operation dialogs, and alert dialogs. The Application Kit also provides higher-level functionality to implement multiple document applications, text handling, and graphics.

Classes are not the only constituents of the Cocoa frameworks. Some programming tasks, such as sounding a system beep, are best accomplished with a simple C function. Cocoa includes a number of functions for accomplishing tasks such as manipulating byte orders and drawing simple graphics. Additionally, Cocoa defines a number of custom data types and constants to provide a higher degree of abstraction to many method parameters.

The Cocoa Development Environment

Project Builder and Interface Builder are the two most important applications used in Cocoa development. Project Builder is the interactive development environment (IDE) for Mac OS X used to manage and edit source files, libraries, frameworks, and resources. Additionally, it provides an interface to the Objective-C compiler, gcc, and the GNU debugger, gdb.

Interface Builder is used to create GUIs for Cocoa applications by allowing developers to manipulate UI components (such as windows and buttons) graphically using drag and drop. It provides assistance for laying out components by providing visual cues that conform to Apple's Aqua Human Interface Guidelines. From an inspector panel, the behavior and appearance of these components can be tweaked in almost every way the component supports. Interface Builder provides an intuitive way to connect objects by letting the user drag wires between objects. This way, you set up the initial network of objects in the interface. In addition, you can interface without having to compile a single bit of code.

Interface components are not the only objects that can be manipulated with Interface Builder. You can subclass any Cocoa class and create instances of the subclasses. More importantly, you can give these classes instance variables, known as outlets, and methods, called actions, and hook them up to user interface components. Interface Builder can then create source files for these subclasses, complete header files, and an implementation file including stubs for the action methods. There is much more to Interface Builder and Project Builder than we can cover in this book, but as you can begin to imagine, the tight integration of these two applications create a compelling application development environment.

Cocoa Design Patterns

Cocoa uses many design patterns. Design patterns are descriptions of common object-oriented programming practices. Effective application development requires that you know how and where to use patterns in Cocoa. Cocoa in a Nutshell discusses these patterns in the context in which they are used. Here is a brief list of the design patterns you will encounter in the book:

Delegation

In this pattern, one object, the delegate, acts on behalf of another object. Delegation is used to alter the behavior of an object that takes a delegate. The developer's job is to implement any number of methods that may be invoked in the delegate. Delegation minimizes the need to subclass objects to extend their functionality.

Singleton

This pattern ensures that only one object instance of a class exists in the system. A singleton method is an object constructor that creates an instance of the class and maintains a reference to that object. Subsequent invocations of the singleton constructor return the existing object, rather than create a new one.

Notification

Notifications allow decoupling of message senders from multiple message receivers. Cocoa implements this pattern in the notification system used throughout the frameworks. It is discussed in Chapter 2.

Model-View-Control

The Model-View-Controller (MVC) pattern is used extensively in the Application Kit to separate an application into logically distinct units: a model, which knows how to work with application data, the view, which is responsible for presenting the data to the user, and the controller, which handles interaction between the model and the view. Chapter 3 discusses MVC in more detail.

Target/action

The target/action pattern decouples user-interface components, such as buttons and menu items, with the objects (the targets) that implement their actions. In this pattern, an activated control sends an action message to its target. Chapter 3 discusses this topic further.

Responder chain

The responder chain pattern is used in the event handling system to give multiple objects a chance to respond to an event. This topic is discussed in Chapter 3.

Key-value coding

Key-value coding provides an interface for accessing an object's properties indirectly by name. Chapter 2 covers key-value coding more thoroughly.

Benefits

These days, application developers expect a lot from their tools, and users expect a lot from any application they use. Any application or application toolkit that neglects these needs is destined for failure. Cocoa comes through grandly by providing the features needed in applications now and in the future, including:

Framework-based development

Cocoa development is based on its frameworks: the Foundation framework and the Application Kit. With framework-based programming, the system takes a central role in the life of an application by calling out to code that you provide. This role allows the frameworks to take care of an application's behind-the-scene details and lets you focus on providing the functionality that makes your application unique.

"For free" features

Cocoa provides a lot of standard application functionality "for free" as part of the frameworks. These features not only include the large number of user-interface components, but larger application subsystems such as the text-handling system and the document-based application architecture. Because Apple has gone to great lengths to provide these features as a part of Cocoa, developers can spend less time doing the repetitive work that is common between all applications, and more time adding unique value to their application.

The development environment

As discussed earlier, Project Builder and Interface Builder provide a development environment that is highly integrated with the Cocoa frameworks. Interface Builder is used to quickly build user interfaces, which means less tedious work for the developer.

Cocoa's most important benefit is that it lets you develop applications dramatically faster than with other application frameworks.

Languages

Cocoa's native language is Objective-C. The Foundation and Application Kit frameworks are implemented in Objective-C, and using Objective-C provides access to all features of the frameworks. Chapter 1 covers Objective-C in depth.

Objective-C is not, however, the only language through which you can access the Cocoa frameworks. Through the Java Bridge, Apple provides a way to access the Cocoa frameworks using the Java language. The Java Bridge does not provide a complete solution since many of Cocoa's advanced features, such as the distributed objects system, are not available with Java. This book will not discuss Cocoa application development with Java.

Another option for working with Cocoa is AppleScript. AppleScript has traditionally been associated with simple scripting tasks, but with Mac OS X, Apple enabled AppleScript access to the Cocoa frameworks via AppleScript Studio. AppleScript Studio provides hooks into the Cocoa API so scripters can take their existing knowledge of AppleScript, write an application in Project Builder, and use Interface Builder to give their applications an Aqua interface—all without having to learn Objective-C. This exposes Cocoa to a completely new base of Macintosh developers, who know enough AppleScript to build simple task-driven applications for solving common problems. For more information about AppleScript Studio, see http://www.apple.com/applescript/studio.

    [ Team LiB ] Previous Section Next Section