Java Behavioral Design Patterns – Observer Design Pattern
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 –
- 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.
<Here are ALL other Java Design Patterns, explained in detail with examples>
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.
1 2 3 4 5 6 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 -> " + 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public class Topic { private List<Subscriber> subscribers = new ArrayList<>(); 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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
1 2 3 4 5 |
Sub 1 new message arrived -> Hello for topic Subscribers Sub 2 new message arrived -> Hello for topic Subscribers Sub 1 new message arrived -> Hello again for topic Subscribers Sub 2 new message arrived -> Hello again for topic Subscribers Sub 3 new message arrived -> 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 Code
<Here are ALL other Java Design Patterns, explained in detail with examples>