User interfaces for object-oriented systems, Part 6: The RPN calculator

The RPN-calculator application demonstrates object-oriented UI principles

1 2 3 4 5 6 Page 5
Page 5 of 6

The Viewer keeps a reference to the current view in view (Listing 5, line 291), which is a reference to a Viewer_ui -- the common interface supported by the two adapters. Consequently, it doesn't need to know which concrete class it's talking to. The Viewer's constructor (Listing 5, line 293) installs an AncestorListener that initializes Menu_site to point at the main frame when the proxy is installed into the frame. The constructor also installs an instance of the tape-style view by calling use_tape() (Listing 5, line 372). Additional construction happens in addNotify() (Listing 5, line 484), which is called when the Viewer's Panel component is realized on the screen. The make_menu() method (Listing 5, line 492) creates the "Interface" menu, and sets things up so that use_tape() is called when the user requests a tape view and use_keypad() (Listing 5, line 392) is called when the user wants a keypad view. The matching removeNotify() method (Listing 5, line 525), called when the JPanel is destroyed, removes the "Interface" menu.

The use_tape() method (Listing 5, line 372) first tries to install a tape-style view by calling replaced_view_with(...) (Listing 5, line 337). This method is called whenever the user requests (by selecting the appropriate menu item) that the view change. It's passed the Class object for the requested view, and if an object of that class isn't being used as the current view, an object is created and installed, replacing the earlier view. Any menus installed by the previous view are also destroyed at this time. The replaced_view_with() returns false if the view wasn't replaced, in which case use_tape() does nothing. Otherwise, it modifies the menu bar to hold items appropriate to the tape-style view. The use_keypad() (Listing 5, line 392) method does essentially the same thing, though the menus it creates are different from the ones created by use_tape().

Moving over to the left side of Figure 13, we see that the Tape and Calculator keypad both notify listeners when they have input by sending them an actionPerformed() message. The mediator object (Listing 5, line 319) (an ActionListener) is notified when text is available, and this mediator relays it to the Parser's parse(...) method (Listing 5, line 110), which does the actual work. The Viewer installs the mediator into a view (the Tape or Calculator_keypad object) every time a new view is installed (on line 352). So, when the current Viewer's view has text for the parser, it calls parse(...), which does a brute-force parse of the input string and makes appropriate calls to the Math_stack that was specified in the constructor.

1 2 3 4 5 6 Page 5
Page 5 of 6