Chapter 10. MathPaper and Cocoa’sDocument-Based Architecture

In this chapter and in Chapter 11 through Chapter 14, we are going to start over and build a new, more sophisticated calculator-like application called MathPaper . MathPaper will manage multiple windows, use fonts to convey information, and use interprocess communication to make a request to another (back end) program to do the actual calculation. In writing MathPaper, we will learn a lot more about Cocoa’s architecture for creating applications that handle documents in multiple windows.

The MathPaper Application

When we’re done, MathPaper will be a scratchpad mathematics application that looks like a text editor: it will display a little text window into which you can type mathematical expressions. The neat thing about MathPaper is that when you hit the Return key, the application will automatically calculate the value of the mathematical expression that you’ve typed and display the result. Figure 10-1 contains an example of MathPaper running with three windows open.

MathPaper window with mathematical expressions and results

Figure 10-1. MathPaper window with mathematical expressions and results

MathPaper can handle multiple windows: typing Command-N will give you another “piece” of “math paper.” In later chapters of this book, we’ll use part of MathPaper to graph equations as well. MathPaper has four main parts:

Document-based architecture

Cocoa’s powerful document-based architecture manages the application’s windows and some of the menus, and it handles files that need to be opened, saved, printed, and so on. The principal AppKit classes in this architecture are NSDocumentController, NSDocument, and NSWindowController.

MathDocument

MathDocument is a subclass of the NSDocument class that manages the actual opening and saving of MathPaper document files. There is a separate MathDocument object for each piece of math paper (i.e., one for each window like the ones shown in Figure 10-1).

PaperController

PaperController is a subclass of the NSWindowController class that asks for the mathematical calculations to be performed and displays the results. As with MathDocument, there is a separate PaperController object for each piece of math paper.

Evaluator

Evaluator is a separate program that can evaluate arbitrary algebraic expressions. It communicates with a PaperController object using an NSTask object and two NSPipe objects. Evaluator is the back end (computational) part of our MathPaper application.

The Cocoa document-based architecture is part of the Application Kit framework. The document architecture manages many aspects of an application that can open multiple documents at a time, including the File Open, Save, Save As, Save To, and Close menu options. To use this architecture, you create subclasses of the NSDocument class (and optionally the NSWindowController class). The classes contain the common functionality for managing windows; your subclasses contain the application-specific routines.

In addition to handling multiple windows, MathPaper handles multiple processes: each piece of math paper is attached to its own copy of the Evaluator program, which performs calculations solely for that window. Together, the MathDocument and PaperController classes comprise the MathPaper application’s front end, while the Evaluator makes up the application’s back end. This technique of having separate front and back ends is a common approach used by many applications. It is also a common technique for structuring web sites. One of the advantages of this technique is that it lets you subdivide your programming efforts, concentrating on the math-solving or database part of the program in the back end and the user-interface part of the program in the front end. Another advantage is that the equation-solving back end can be used by more than one application, as long as its interface is well documented. If you are careful with your design, you can also create a portable back end that will be easy to move from one operating system to another.

In this chapter, we’ll build two of the three major modules of MathPaper: the Evaluator back end, which we’ll test in a Terminal (Unix) window, and the front-end interface, which we’ll test on the desktop. In Chapter 11, we’ll hook these two modules together with the third major module in our application, a controller. The controller will use an object of type NSTask to manage the Evaluator (Unix) subprocesses. In Chapter 12, we’ll improve our MathPaper output using our own Rich Text Format (RTF) class. Then, in Chapter 13, we’ll arrange for each piece of math paper to save its data into a file and then read the data back from the file when the data-file icon is opened.

The big picture of the MathPaper application, with three open math paper windows (as in Figure 10-1), is shown in Figure 10-2. It indicates that the MathPaper application creates three MathDocument objects and three PaperController objects, one for each window. Each PaperController object creates its own NSTask object to manage a copy of the Evaluator back end.

The big picture of the MathPaper application

Figure 10-2. The big picture of the MathPaper application

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.116.126