Observer Pattern

Observer Pattern is a Design Pattern that defines one-to-many dependency between objects. As a result when one object is changes its state then all its dependents are notified and updated automatically. This design pattern is very help full for implementing a distributed event handling system system.

A very good example to describe the system can be Newspaper-Subscriber system. A client get a newspaper every morning is he/she subscribe to it. The newspaper company has no idea which people are their subscriber. The local agent send the copy to the client. When a client subscribe to a specific newspaper, he/she getting that from next day. And when they unsubscribe from that they don’t get that copy anymore. Now consider publishing a newspaper is like occurring an even. After publish all its client get a copy which is like get a notification. The basic functionality used run this pattern. Subscribe or attach, Unsubscribe or detach, Notify(),

Some Programming language like JAVA contains API for working with such sort of Pattern. java.util.Observable provides basic functionality for creating such Subject or event source. Along with this java.util.Observer provides same for an observer or client. One of the best example of this pattern is Java GUI implementation. Pressing a button introduce an action on which it is subscribe to. The addActionListener method subscribe to a task.

You can find a java implementation of this pattern in wikipedia

By: Md. Shahjalal

2 Responses to Observer Pattern

  1. [...] of a. Chain of responsibility b. Command c. Interpreter d. Iterator e. Mediator f. Memento g. Observer h. State i. Strategy j. Specification k. Template method l. Visitor m. Single-serving visitor n. [...]

  2. [...] Observer pattern strikes again, as I’m defining the relationship between the event generator and the event [...]

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.