Observer
I subscribe to newspapers/TvChannels
Newspapers/TVchannels subscribe to celebriities.
So we know of events.
Whenever I want time I check my watch but when I am sleeping, I subscribe to an alarm.
Best possible I pull info but (if it is inefficient for an event ) I prefer to be informed.
In general observer pattern is story of a passive observer
that dont want to observe. "Let me know if it changes" attitude.
oh sorry, If too many components need to observe samething?
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Publishers will inform the subscribers when something changes.
So the publisher will have a list of subscribers in a list.
Whenever the publishers property changes...he will inform all subscribers
class Publisher
{
ArrayList mysubscribers;
public MyProperty
{
get {return myProp;}
set
{
myProp = value;
foreach (subscriber s in mysubscribers)
{
inform(s);
}
}
}
}
More than software engineering, this pattern is very well used in real life.
In software Engineering, this is a most used pattern.
1) Even triggers in DB are meant for same purpose.
2) "SQL notification services" is a product to demonstrate the need of this pattern.
3) Do you use gadgets/browse webpages/ subscribe to RSS feeds? -- This is not observer pattern. If you subscribe to an event to get an email, that is observer pattern.