Design Patterns: A quick guide to Observer pattern in Java
23 Apr

Design Patterns: A quick guide to Observer pattern in Java

Mahipal Nehra

A quick guide to the observer design pattern in Java. Observer design pattern is a software design pattern that conceptualises the communication strategies between the objects and their dependents. In such a pattern we create a subject class that maintains a list of dependencies called observers that are notified of any change in state of the subject or whatever they are listening to. You might be well aware of event driven programming paradigm. The main focus of such paradigms is to implement event handling systems. “JavaScript”, for example is a popular event driven programming language. Even in Java, there are components that listen to events.

Design Patterns: A quick guide to Observer pattern in Java

Take for an example, web sockets, they help in creating full duplex communication between the client and servers. If you are trying to create a chat application , it is obvious that there will be user created events, http requests and responses. The observer design pattern is best suited to implement such systems. In such systems , the observer object that is waiting to get notified via a stream of events has no control over the emitted stream of events by the subject. The observer (user1 object) subscribes to a subject (‘that emits messages’) from another observer (user2 object). In this case user1 object has no control over what is emitted to it, only that it is notified of some state change. The design pattern of classes while using the observer design pattern is such that the subject does not know anything about them , it only is aware that it the observers implement the observer interface and how it can notify them about changes. We will see a simple example of observer design pattern in java, in a bit.

What is observer design pattern?

It is easier to understand concepts by using some analogy. So, here is one for you. Think of an email blog subscription.

  1. You visit this really cool site that writes articles on interstellar stuffs.

  2. You then subscribe to a particular topic , say space aliens. As long as you subscribe to that email list, you will get the articles.

  3. If you unsubscribe from that email list, you will not get emails from them.

  4. There will definitely be other subscribers and un-subscribers as well.

Read: "What does DevOps actually do?"

This is what the observer design pattern is all about. The publisher (email blog company) and the subscriber (you) together form the observer pattern. And in the lingo of software engineers, the publisher is the subject and the subscriber is the observer. So, basically the subject broadcasts any state change that happens and the observers listen to the updates and displays those changes. Okay, but why use observer design pattern? What is the point of using it?

Why use observer design pattern?

Lets get back to the chat application example we used in the introductory paragraph. You will want to use web sockets to create a full duplex communication between the client and server, otherwise no one would want to use a chat application that needs a page refresh to fetch new messages. Since, web sockets solve that problem and there are libraries that implement it , your problem has already been solved. However, it is necessary to know that the pattern most libraries follow is based on the observer design pattern. This awareness will help a lot when designing a system that requires a common interface to updates its dependencies in one go.  What we have achieved with webs sockets is asynchronous behaviour, updating dependencies when state change and that happens automatically. So, why use observer design pattern? What problems can be solved with it?

Read: "Factory Design Patterns in Java"

Lets see the bullet points below for the benefits of observer design pattern and that should clear your doubt on “why use observer design pattern”?

Benefits of Observer design pattern

  1. The observer design pattern provides an object design pattern where subjects and observers are loosely coupled. The subject does not need to know everything about the concrete class that the observer implements, it should just be aware of the implementation.

  2. Since, they are loosely coupled , we can add as many observers we like, and replace as many as we want, without having to modify a lot of things.

  3. The subject does not need to be modified to add other types of observers. Without the concept of observer design pattern or design patterns in general , we would have messed up the subject’s implementation.

  4. The subjects and observers can be reused independently of each other because of loose coupling.

  5. Changes to either the subject or an observer will not affect the other as long as they are not messed up completely. The observer design pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

  6. We can achieve asynchronous behaviour of our code with such pattern, and ultimately the end goal is to create a code base that is extensible, maintainable and reusable.

Why does observer design pattern fall in the category of behavioural design pattern?

Behavioural Design Patterns are those design patterns that deal with interaction between objects and how they communicate. As you can guess now that observer design pattern follows the same approach, the subject holds the state and communicates it to the observers whenever there is a change in state.

Example of observer design pattern in java

The common approach to this design pattern is to have the following things kept in mind:

  1. Subject – that keeps track of all observers and broadcasts any changes in the state.

  2. Observers- these  implement a common interface that subscribe to the subject for getting updates.

This way we can decouple the behaviour of objects from each other as well as the core class. So, how can you implement the observer design pattern in java?

1. Start by declaring a subject interface.

A quick guide to the observer design pattern in Java

Fig- Observer Design Pattern in Java – Subject Interface

This interface has 3 methods –

  1. registerObserver – As the name suggests, it registers the observers with the subject.

  2. removeObserver- It is used to unsubscribe from the subject.

  3. notifyObservers- It is used to update the observers for any state change.

2. Declare an Observer

A quick guide to the observer design pattern in Java

Fig- Observer Design Pattern in Java – Observer Interface

The update method is self explanatory, it updates the state of the observers.

3. Implement the subject interface

A quick guide to the observer design pattern in Java

Fig- Observer Design Pattern in Java – Subject Interface Implementation

The subject class has additional stuffs, beyond what the interface provides.

  1. Class properties -  List is used to store the observers and value is a simple integer that can be considered a state value.

  2. Methods- setValue method simply sets the value and notifies the observes for change.

  3. Constructor- The constructor simply initialises the array list object to keep track of different observers.

  4. Others are basically the implementation of the interface methods as already described.

4. Implement the observer interface

A quick guide to the observer design pattern in Java

Fig- Observer Design Pattern in Java – Observer Interface Implementation

This is just on instance of implementing the simple observer class. We can implement as many as we need. Apart from implementing the update method there are several additional stuffs, lets check them one by one.

  1. Class Properties- There are two variables declarations value and simpleSubject.

  2. Constructor – Now this is where the observer design pattern is playing a key role. The Subject class is being passed as a parameter and from within the Observer it is being registered. Notice that observers register themselves when needed, the subject only does that when asked to do it.

  3. There is an additional display method that prints out the value, or changed value.

5. Combining all these together   

A quick guide to the observer design pattern in Java

Fig- Observer Design Pattern in Java – Test Drive

See, how all these make sense now? The Subject class’s instance has everything that allows us to register the observer, update the value and notify the changes. The Observer class has the mechanism to register itself with the Subject and once the value is updated, the observer method is called inside of the Subject to broadcast the state change. The event driven systems may have the same approach to implement their version of “subject” and “observers”. Java’s Swing makes use of the Observer Pattern, as do many GUI frameworks, if you have ever used one, you may realise the pattern now.

Where else can observer design pattern be applied?

One final benefit of observer design pattern we did not mention rigorously is its use in frameworks and also standardising its approach to core programming languages’ standard library. Java has its own observer design pattern library and so does python. State management libraries like Redux have been built upon its principles. Web sockets , blockchains, distributed databases, or situations where data is not available to the CPU at start up.  

Read: "Singleton Design Pattern – A thoughtful dive into Object Structures and Creation"

Common misconception: Design Patterns do not remove dependencies completely, otherwise there will be no point of relating one class to the other. The point is to reduce it.

Conclusion

So, this was observer design pattern in java, you can use it any programming language of your choice. Now you can explain ‘why use observer design pattern?’ to anyone verbosely, because if you grasp the basics, explanation and implementation is easy. To summarise the reason, you can see that the code above is pretty much extensible. We can implement as many Subjects and Observers we like without having to distort the concrete classes and the interfaces. Also, every time some changes happen in the values (state) of the classes, it is taken care of by the subject, and we do not have to write messy code to update it using a bunch of conditions.

Read: "Top 5 Best Free Java Development IDE"

Posted by Mahipal Nehra | Posted at 23 Apr, 2020 Web