Observer Design Pattern is listed under Behavioral Design Patterns as it can alter the behavior of the program. In this article, we will understand this pattern with a simple example.
This pattern is chosen when we have one-to-many relationships between objects. Using this pattern will make sure that this relationship is not tightly coupled. When one object changes state, its dependent objects are notified automatically. In short when your application has x
- A one-to-many dependency between objects and you need to have these objects loosely coupled.
- You need to have communication between these objects so if the state of one objects is changed, then others are notified.
xHere are ALL other Java Design Patterns, explained in detail with examplesx
Observer Design Pattern by Example
The best example to understand this pattern is JMS Topic Subscribe and Publish process. The objects that observe on the state of another object is called Observer and the object that is being watched is called Subject. The subject may provide different methods to an observer.
In our example our Subscribers will be Observer and Topic will be Subject.
First, we will define our abstract class for Observer.
abstract class Observer { protected Topic topic; public abstract void update(); }
This abstract class will be extended by our topic subscribers. A subscriber is a simple class that has a name and it will attach itself to a particular topic when created.
public class Subscriber extends Observer { private String name; public Subscriber(String n, Topic t) { this.name = n; this.topic = t; this.topic.subscribe(this); } @Override public void update() { System.out.println(name + " new message arrived -x " + this.topic.getMessage()); } public String getName() { return name; } }
The subscriber extends Observer and overrides update method.
Topic
The topic class is our subject. It holds the list of observers/subscribers. When a message is published, all the subscribers are notified of it.
public class Topic { private ListxSubscriberx subscribers = new ArrayListxx(); private String topicName; private String message; public Topic(String name) { this.topicName = name; } public void subscribe(Subscriber s) { subscribers.add(s); } public void publish(String message) { this.message = message; notifySubscribers(); } public void notifySubscribers() { for (Subscriber sub : subscribers) { sub.update(); } } public String getTopicName() { return topicName; } public String getMessage() { return message; } }
Running the Example
public class ObserverDemo { public static void main(String[] args) { Topic t = new Topic("opencodez"); new Subscriber("Sub 1", t); new Subscriber("Sub 2", t); t.publish("Hello for topic Subscribers"); new Subscriber("Sub 3", t); t.publish("Hello again for topic Subscribers"); } }
Output
Sub 1 new message arrived -x Hello for topic Subscribers Sub 2 new message arrived -x Hello for topic Subscribers Sub 1 new message arrived -x Hello again for topic Subscribers Sub 2 new message arrived -x Hello again for topic Subscribers Sub 3 new message arrived -x Hello again for topic Subscribers
Advantages
- This pattern provides one-to-many relations between objects without tight coupling.
- This pattern provides very less dependency between Subject and Observer.
- Observers can be easily added or removed.
- But at the same time if Observer pattern not used carefully it can add unnecessary complexity to a program.
Conclusion
In this article, we understood Observer Design Patter with the help of a simple example. The source code is available in our Github repository.
Download Codex
xHere are ALL other Java Design Patterns, explained in detail with examplesx
x