Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Programmer's Guide to MacApp / Part 2 - Working With MacApp
Chapter 26 - Working With Dependencies


Overview

MacApp's dependency mechanism allows an object to be registered as a dependent of another object and to be notified of changes in the object on which it depends. A notifier is an object that has one or more dependents--when the notifier changes, it informs its dependent objects of the change. An object can have multiple dependents and multiple notifiers, and one object may be both a notifier and a dependent.

A dependency space stores dependency relationships between objects and encapsulates a mechanism for notifying dependent objects when an object changes. MacApp defines TDependencySpace as an abstract superclass for dependency spaces and defines two subclasses, TSimpleDependencySpace and TStandardDependencySpace.

By default, a MacApp application has one dependency space, referenced by the global variable gMacAppDependencies. This instance is created in the TApplication method DoMakeDependencySpace and defaults to an object of type TStandardDependencySpace. The default dependency space can be accessed using the method TObject::GetDependencySpace.

The section "Dependencies", in Chapter 3, "Core Technologies," covers the following topics:

This chapter provides additional information about changes and updates, and also supplies recipes for performing specific operations with dependencies in your application. Figure 26-1 shows the classes and methods used to provide dependency support.

Figure 26-1 Dependency classes and methods

Changes and Updates

Your application informs an object that it has changed by calling the object's Changed method.

void TObject::Changed(ChangeID theChange,
                  TObject* changedBy);
You call an object's Changed method so that it can perform any operations it needs to and notify its dependents that it has changed.

The parameter theChange is equivalent to a long integer value. A command object, notifying its dependents when the command is committed, sends a command number for theChange. A control object that synchronizes two or more controls in a dialog box might send its fEventNumber value to notify its dependent controls. Table 19-1 on page 462 lists event number and change notification constants defined by MacApp.

The changedBy parameter represents the object that caused the change. It can be a command object, NULL, or the caller of Changed.

In the TObject class, the Changed method calls the NotifierChanged method of the object's default dependency space. The NotifierChanged method contains the updating algorithm for a particular dependency space class, which ensures that the DoUpdate method of any dependent objects will be called. DoUpdate is declared as follows:

void TObject::DoUpdate(ChangeID  theChange,
                  TObject*       changedObject,
                  TObject*       changedBy,
                  TDependencySpace*dependencySpace);
The DoUpdate method does nothing in TObject. You override DoUpdate to let your object respond to a change in one of its notifiers. The parameters to the DoUpdate method describe the change, the changed object, the changed-by object, and the dependency space in which the change occurred. A DoUpdate method normally checks the change ID before taking any action. This is particularly important for dependencies that exist in a dependency space defined by TStandardDependencySpace, because DoUpdate is called even if the object is only an indirect dependent of the object that changed.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
25 JUL 1996