Observer pattern for unpredictable observation time
- by JoJo
I have a situation where objects are created at unpredictable times. Some of these objects are created before an important event, some after. If the event already happened, I make the object execute stuff right away. If the event is forthcoming, I make the object observe the event. When the event triggers, the object is notified and executes the same code.
if (subject.eventAlreadyHappened()) {
observer.executeStuff();
} else {
subject.subscribe(observer);
}
Is there another design pattern to wrap or even replace this observer pattern? I think it looks a little dirty to me.