< Previous PageNext Page >

Document Architecture

Many applications let users create and edit documents: unique aggregations of data presented in windows with identical user interfaces. Word processors, photo-image editors, and web browsers are examples of document-based applications. These applications have similar features. They enable users to create new documents, save those documents to files, and then open those documents later. Document-based applications also validate menu items, monitor each document's edited status, manage document windows, and respond appropriately to application-wide events (such as termination). Often they can have different internal representations of document data.

Cocoa offers developers an architecture that reduces the effort required to implement a document-based application with features such as these. The essential component of this architecture includes three Application Kit classes: NSDocument, NSDocumentController, and NSWindowController. In a document-based Cocoa application, objects of these three classes have distinct spheres of responsibility and a cascading series of relationships with each other based on ownership and management (Figure 7-1).


Figure 7-1  Ownership relationships among the document classes

Ownership relationships among the document classes

A Cocoa application that is based on the document architecture has a single NSDocumentController object. This object owns and manages one or more NSDocument objects. Each of these NSDocument objects, in turn, creates and manages one or more NSWindowController objects. And each NSWindowController object is associated with a window of the document. (A document can have multiple windows.) An NSWindowController object manages the presentation of the document.

Each of the three kinds of objects in the Cocoa document architecture has a specific responsibility which is dictated by its MVC role:

Xcode provides a project template for Cocoa applications based on the document architecture. When you create a project using this template, you get an NSDocument subclass (named MyDocument) with stub implementations of the required method overrides. You also get a document nib file with File's Owner set to MyDocument.

Further Reading: For a complete description of the document architecture, see Document-Based Applications Overview.



< Previous PageNext Page >


© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-12-20)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.