CategoryPattern: ----- '''Pattern Name''' Self-Addressed Stamped Envelope (wiki has problems with hyphens) '''Intent''' Define a context-free way to notify an object of the occurrence of an event. '''Also Known as''' SASE (CategoryAcronym), Callback '''Motivation''' Sometimes you want to arrange ahead of time to have a message sent to an object whenever some 'event' occurs without having the object that triggered the 'event' know about the receiver of the message or the details of the message. For instance, in a windowing system you will often want to have an application respond to button presses or mouse clicks by having the application register a message with the button or view so that when the event occurs, the message is sent. The pattern is called SASE because of the analogy to sending a self-addressed, stamped envelope to a recipient with the understanding that the recipient will send back the envelope whenever an event occurs. For example, you may want to send a SASE to contest promoters with the understanding they will return it when the winners of the contest are known. '''Applicability''' Use SASE when * you want objects to respond to the occurrence of an event in a way they define * you don't want to force receivers of a message to conform to a specific protocol * you don't want to waste time sorting through useless notifications. (as sometimes happens in classic changed/update) '''Participants''' * '''receiver''' registers the message with the sender object. * '''sender''' sends back the SASE whenever the event occurs. The sender may also trigger the event in response to a message send or state change. Often a sender will keep track of many SASE's for many different events and send along the appropriate SASE for each event. * '''message''' may not actually be a first class object. Any pairing of a receiver and a message selector will work as long as the language supports ''perform:'' semantics. '''Collaborations''' * the receiver notifies the sender to notify it by sending itself, along with a message selector and the event name to the sender. * whenever the sender receives a trigger message for a particular event, it sends the messages associated with that event. '''Known Uses''' * The Smalltalk/V Event system is an implementation of SASE. In Smalltalk/V, a class registers the events it can trigger, while receivers inform the instances of that class to notify them whenever an event occurs with the #when:send:to: message. * Callbacks in IBM Smalltalk are also implementations of SASE. * onChangeSend:to: in VisualWorks is an implementation of SASE. '''Related Patterns''' SASE is very closely related to CommandPattern. AmericanCulturalAssumption: In the pre-internet days you could request information from a business by sending them an empty envelope with a stamp and your address on it. They would stuff the envelope with their crud and mail it back to you. ''Why is this an AmericanCulturalAssumption? In the pre-internet days I have sent SASE's (with international postage no less) from the US to the UK for similar offers.'' ''In the UK it was normally written SAE - Stamped, Addressed Envelope.''