3.1 AppKit Design Patterns

Besides leveraging the design patterns and methodologies used with the Foundation framework, the Application Kit relies heavily on several others. Two that merit special attention are Model-View-Controller and Target/Action.

3.1.1 Model-View-Controller

The Model-View-Controller (MVC) pattern is the driving design pattern in the Application Kit.[1] The premise of this pattern is that code may be split up into logically distinct units that each perform a specific role:

[1] MVC traces its lineage to the first object-oriented programming language, Smalltalk, and has been important ever since.

  • The model is an object that encapsulates data and provides logic that manipulates that data.

  • The view is a separate object that only knows how to display data.

  • The controller is an arbiter between the model and the view. The controller's job is to take data from the model and pass it to the view where it can be displayed. If the view is interactiveable to accept user inputthen the controller will interpret those actions and instruct the data model to do something in response.

Many views in the MVC pattern may subscribe to the controller, giving the application flexibility to display data in different contexts and formats without heavy modification of the modeling and control logic. This idea is illustrated in Figure 3-1, which shows two views of the same data: a table view of the data and a chart view.

Figure 3-1. MVC used to display two different views of the same data

3.1.2 Target/Action

The Application Kit uses the target/action pattern to decouple UI widgets (controlstypically subclasses of NSControl) from the code that is executed when the user activates the control. In this pattern, a control keeps a reference to a target object that receives an action message. The action message is stored as a selector in an instance variable of the control object. When the user activates the control, the action message is sent to the target. Action messages and targets are generally established in Interface Builder; however, you can change a control's behavior by using the setAction: and setTarget: methods of NSControl. The current target and action of a control may be ascertained programmatically by using the methods target and action.

    Part II: API Quick Reference