Observer Pattern

ObserverPattern

So, I’ve been looking at Design Patterns lately, and I’ve decided to document a couple, starting with the Observer Pattern.

The basic gist of it is that you have an “observable” class/interface (in this case Subject implemented as MyTopic) which has, amongst others, the methods register(Observer obj) and notifyObservers().

You then pass in concrete instances of the interface Observer (in this case MyTopicSubscriber) via the subject.register() method, and then – any time the Subject is altered or otherwise interacted with, it can then call notifyObservers() and then – via calls to each of the Observer object’s update() method, everyone in that list of Observers can be informed.

That’s about it, really. Seems simple!

 

TRP

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s