|
|
|
||||||
|
||||||||
The target/action paradigm is about interacting with the control
elements in a graphical user interface (GUI). The target/action
paradigm isn't a TOM topic per se; it is already thoroughly used
in NextStep/OpenStep (I don't know of any prior art, but I can
imagine it exists). However, if I explain it now, I can build on
it in future TOM highlights, since in TOM we can do things with it
that Objective-C, which is the language of choice for OpenStep,
lacks.
The target/action paradigm is a way of dispatching GUI events. If we
first consider prior art, there are several ways by which events
are usually dispatched. We will skip inflexible non-OO solutions
like call-back function pointers with (or without) a
` Prior artA popular solution is for the button to invoke a method on itself,
e.g., One solution to this problem is to not invoke the
A solution to this second problem was thought of by the developers of
Java, who added the concept of inner classes to the
language. Now for each button target, you just define an inner
class that inherits the ButtonTarget and (only) implements
Target/actionThe target/action paradigm is a simple solution to a basically simple problem. The problem with the existing solutions is that it is always the same method that is invoked, irrespective of whether the button itself or its target are to implement that method. If the method to be invoked can be specified, the problem is solved. In a previous highlight (15 Feb 1998, The
selector type), the selector type has been introduced: a
selector is a name for behaviour that is not bound to a particular
object. (The selector type is present in languages like
Smalltalk, Objective-C, and TOM; it is lacking in, e.g., Java,
Eiffel, and C++.) With the target/action paradigm, each button
knows its target, which is the object to be informed of the user
clicking the button, and the action, which is the selector of the
method to be invoked on that target. Now the action can be
With the target/action paradigm, the target does not need to spend
memory space to references to all its buttons (though each button
spends space on the reference to its target). There are GUI
elements, however, that contain more information than just having
been clicked. For instance, lots of elements contain a current
value (numeric text fields, slider, volume knobs, etc). The
target must be able to retrieve that value. For this reason,
every action method has one argument: the GUI element that
triggered the invocation, normally called the sender.
Thus, the code used by a button to inform its target, given its
Another interesting application of the target/action paradigm is how it can interact with the responder chain. In OpenStep (and TAG), the GUI elements in a window are arranged in a tree: for example, a box contains a few buttons, another box some fields, etc., and the boxes are contained in a big box, which is part of the window. When the application is active, one of these elements will receive the key events. This element is called the first responder. Its parent in the hierarchy is the next responder, etc. Transitively, the first responder and its ancestors make up the responder chain, together with some global entities as the menu and Application object. The first responder is set by a mouse click; the responder chain is
used not only to dispatch key pressed events, but also to respond
to the user activating for instance the `Cut', `Copy', and `Paste'
menu items. How does this work? You don't want to have to fix
the targets of these menu items when the first responder is
changed. The solution is as elegant as it is simple: If the
target of a GUI element is set to The piece of code shown above to inform the target is of course invalid
given this responder paradigm (a
TOM?So far, the discussion on target/action has been conducted in a way that applies to both Objective-C/OpenStep and TOM/TAG. In a future highlight, we'll discuss how in TOM curriedInvocation objects can be used to provide extra
flexibility by passing more than one argument in an action method
to a target. Without having to modify the Button class, of
course.
Up: Highlights |
||||||||
|
|
|