Typically, events are handled by registering a callback function with the class that raises the event. When an event occurs, this class will call the callback function.
You will find many examples of swings. Here is an example of a non-rotation from a chat application that I did some time ago
It was a library that would allow the developer to embed chat features in their applications. The ChatClient class has a member of type IMessageListener
IMessageListener listener;
Afer, creating an object for the ChatClient class, the user will call setListener on the object. (Maybe addListerer for multiple listeners)
public void setListener(IMessageListener listener) { this.listener = listener; }
And in the library method, when the message is received, I would call the getMessage method for this listener object
This was a basic example. More complex libraries will use more complex methods, such as implementing event queues, threads, concurrency, etc.
Edit: Yes. it really is an observer pattern.
Midhat
source share