Writing the sender

Follow these steps when writing a sender:

  1. Determine which events might be of interest to receivers.
  2. Define the protocol for creating interests for these events.
  3. Determine and document the notification classes associated with each interest.
  4. Select a mechanism that the sender uses to issue the notifications.
  5. Issue the notifications at the proper places in the sender code.
The code sample below shows the interface for TShapeList. The two events about which it notifies are "adding a shape to the list" and "removing a shape from the list."

Receivers create interests for these events occurring to an instance of TShapelist by calling its GetAddedShapeInterest and GetRemovedShapeInterest member functions.

Because there is no documentation on the classes of notifications sent for these events, receivers assume they are TNotifications.

TShapeList inherits from TNotifier, which provides a notifier implementation that performs synchronous notification.

      class TShapeList : public TNotifier {
      public:
          MCollectibleDeclarationsMacro(TShapeList);
      
                                  TShapeList();
                                   TShapeList(const TShapeList&);
          virtual                 ~TShapeList();
      
          virtual void            AdoptShape(MGraphic*);
          virtual MGraphic*       OrphanShape(const MGraphic&);
      
          virtual TIteratorOver<MGraphic>* CreateShapeIterator() const;
      
          TInterest               GetAddedShapeInterest();
          TInterest               GetRemovedShapeInterest();
      
      private:
          TDequeOf<MGraphic>     fShapes;
      
          enum { kOriginalVersion };
      };
All that remains is to define the member functions that create the interests and issue the notifications in the code.

GetAddedShapeInterest returns a standard TInterest, initialized with the notifier (this instance) and a token that names the interest. GetRemovedShapeInterest is exactly parallel, so code for it is omitted.

      TInterest TShapeList::GetAddedShapeInterest()
      {
          static const TToken kAddedShape("AddedShape");
          return TInterest(this, kAddedShape);
      }
AdoptShape uses the Notify protocol of the parent TNotifier to issue notifications when the shape list changes. A standard TNotification is instantiated using the interest returned from GetAddedShapeInterest. OrphanShape is parallel, with the exception that it only issues notification if the shape that it was requested to remove was actually in the list.

      void TShapeList::AdoptShape(MGraphic* shape)
      {
          fShapes.Add(shape);
          Notify(TNotification(GetAddedShapeInterest()));
      }
      MGraphic* TShapeList::OrphanShape(const MGraphic& shape)
      {
          MGraphic* removedShape = fShapes.Remove(shape);
          if (removedShape) {
              Notify(TNotification(GetRemovedShapeInterest()));
          }
      
          return removedShape;
      }

[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