CHAPTER 2 Subjects and Views

Subject/view basics.


Let's begin by building a very simple program called "simpleview" that uses a scrollbar to print numbers to standard output. A screen shot of the simpleview program is shown is figure 2-2 and its source code is shown in listing 2-1. When the user manipulates the scrollbar, floating point numbers between 0 and 50 are printed to standard output.





Figure 2-3 shows the object diagram for the simpleview program. We use a common notation throughout this tutorial for subjects and views. Subjects are depicted as squares and views as circles. Note that when the view is implemented as a Fresco viewer object, such as the scrollbar viewer shown in figure 2-3, we use a screen shot of the viewer rather than a circle.

Examining figure 2-3, you'll see that the SimpleView program has one subject (called a BoundedFloat) and two views--the scrollbar, which comes from the WidgetKit in line 27 of listing 2-1, and the SimpleView, which is defined on line 1. Let's first examine the subject--the BoundedFloat--and then look at the view behavior when the user manipulates the scrollbar.

The BoundedFloat derives from Fresco's Adjustment interface, shown in figure 2-4. Adjustment provides a set of operations for scrolling a current range within a bounding range.

The Adjustment interface is useful for implementing most scenarios where scrolling is involved. One example is a texteditor. In this case the bounding range is the entire text file, and the current range is what appears in the texteditor's window. In the case of the BoundedFloat, the current range does not have a span: it is simply a float value. In the simpleview program, when the user manipulates the scrollbar, the current value changes from its lower bound of zero to its upper bound of 50. You can see the BoundedFloat being created on line 25 of listing 2-1.

To understand the view behavior, it is helpful to look in detail at the chain of events that take place when the user manipulates the scrollbar. Refer to figure 2-3 and listing 2-1.

(1) First, during initialization, the SimpleView object attaches itself to the BoundedFloat (line 5 of listing 2-1) as does the scrollbar's slider.

(2) When the user presses on a stepper-button of the scrollbar, the stepper-button calls the scroll_forward() operation on the BoundedFloat changing its current value.

(3) The BoundedFloat responds by calling update() on all attached views.

(4) The SimpleView has defined update() to print the new value to standard output. This can be seen on line 10 of listing 2-1.

(5) The scrollbar's slider has defined update to change the position of its thumb-button.

Although it is not required, any Fresco object is capable of participating as a subject or view since the base FrescoObject interface defines operations that support subject-view interaction. The interface is shown here.

interface FrescoObject {
    Tag attach(in FrescoObject observer);
    void detach(in Tag attach_tag);
    void disconnect();                  
    void notify_observers();
    void update();
};

Attach() can be used to establish a subject-view relationship. As seen in figure 2-3, multiple views can be attached to a given subject (such as the BoundedFloat.) Thus subjects often maintain a list of attached views. Whenever a new view is attached, a "tag" is returned identifying the view within the list so that it may later be detached. (Pointers cannot by used for this type of identification because with distributed objects we cannot assume that all attached views are in the same address space.) Subjects typically implement notify_observers to traverse their list of attached views and call update() on each view. Thus, during the execution of scroll_forward in step (2) above, the BoundedFloat calls notify_observers() on itself. This causes update() to be called on each attached view in step (3).


Copyright (c) 1994 by Steve Churchill
Comments or questions? Contact Steve Churchill (stevec@faslab.com)

Generated with CERN WebMaker