Observer pattern for unpredictable observation time

Posted by JoJo on Programmers See other posts from Programmers or by JoJo
Published on 2012-06-23T05:27:43Z Indexed on 2012/06/23 9:25 UTC
Read the original article Hit count: 217

Filed under:

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.

© Programmers or respective owner

Related posts about observer-pattern