[Next] [Previous] [Up] [Top] [Contents] [Index]
2.4 Predicate Objects
Fields may be associated with a predicate object. This has the effect of reserving persistent space for the field in any object that might be classified as a descendant of the predicate object. The value stored in the field persists even when the field is inaccessible. At object-creation time, an initial value may be provided for fields potentially inherited from predicate objects, even if those fields may not be visible in the newly-created object. The semantics of accessing a field attached to a predicate object is governed by the semantics of accessing its corresponding accessor methods.
The following example exploits this semantics to implement a graphical window object that can be either expanded or iconified. Each of the two important states of the window remembers its own screen location (using a field named position
in both cases), plus some other mode-specific information such as the text in the window and the bitmap of the icon, and this data persists across openings and closings of the window:
object window isa interactive_graphical_object;
var field iconified(@window) := false;
method display(w@window) {
-- draw window using w.position
... }
method erase(w@window) {
-- clear space where window is
... }
method move(w@window, new_position) {
-- works for both expanded and iconified windows!
w.erase; w.position := new_position; w.display; }
predicate expanded_window isa window when not(window.iconified);
var field position(@expanded_window) := upper_left;
field text(@expanded_window);
method iconify(w@expanded_window) {
w.erase; w.iconified := true; w.display; }
predicate iconified_window isa window when window.iconified;
var field position(@iconfied_window) := lower_right;
field icon(@iconified_window);
method open(w@iconified_window) {
w.erase; w.iconified := false; w.display; }
method create_window(open_position, iconified_position,
text, icon) {
object isa window {
iconified := false,
position@open_window := open_position,
position@iconified_window := iconified_position,
text := text, icon := icon } }
A window object has two position
fields, but only one is visible at a time. This allows the display
, erase
, and move
routines to send the message position
as part of their implementation, without needing to know whether the window is open or closed. The create_window
method initializes both position
fields when the window is created, even though the position of the icon is not visible initially. The position@object
notation used in the field initialization resolves the ambiguity between the two position
fields.
[Next] [Previous] [Up] [Top] [Contents] [Index]
Generated with Harlequin WebMaker