2.4 Predicate Objects
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:
A window object has twoobject
windowisa
interactive_graphical_object;var field
iconified(@window) := false;method
display(w@window) { -- draw window usingw.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_windowisa
windowwhen
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_windowisa
windowwhen
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 } }
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.
Generated with Harlequin WebMaker