Using custom interests

At times, it is useful to define interests that specify more than the sender and event. For example, you might have a view on only a portion of some data. Rather than receive (and mostly ignore) notifications pertaining to any part of the data, you would rather the view only receive notifications pertaining to that portion of the data it displays.

You can do this by having the sender provide protocol for creating derived classes of TInterest. Receivers can then use this protocol to specify interest in events involving only part of the sender's data.

This section extends the example to define TShapeInterest, which is an interest in a particular shape, and TSpecialShapeView, which is a view on a single shape in a TShapeList.

The definition of TShapeInterest::IsEqual is very important, because it determines the cases in which interests are sent. TInterests, like TNotifications, must be capable of streaming across task boundaries, and so need to be able to copy their contents. Therefore, TShapeInterests cannot contain pointers to shapes and implement IsEqual to compare the pointers.

In addition, it is desirable to be able to distinguish different shapes that happen to have the same geometry and bundle, and it is also desirable to be able to specify interests in a single shape independently of changes to its geometry or bundles. But MGraphic defines equality using the geometry and bundle, so TShapeInterest also cannot own copies of the shapes and compare them (as was done with TShapeNotification).

For these reasons, the example in this section introduces a third new class, TShape, that wraps an MGraphic and provides a unique ID that it uses to define equality. The code presented in previous examples is unchanged, except that now it uses TShape rather than MGraphic. With this class, TShapeInterest can implement IsEqual in a way that meets these requirements.

NOTE The TShape class is specific to this example and is not part of the Taligent Application Environment.

While some receivers might be interested in a change only when it applies to a particular shape, others might still want to find out about that change applied to any shape. Senders provide for this by sending notifications multiple times, using a different interest each time. This technique is common when using specialized interests.

MNotifier::Notify defines protocol for sending a notification using an interest that differs from the one the notification owns. The same notification instance can be used more than once as long as it can be cast to the class the sender has defined for each interest used to send the notification.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker