Observer Design Pattern Java example
Observer Design Pattern is a behavioral design pattern which is used to notify multiple objects about any change in Subject state using loose coupling. There will be a Subject object which maintains the list of its subsribers, whenever the subject state changes, it will notify the list of subscribers. The subscriber can be updated using a push or pull mechanism.
In JDK Java Swing make heavy use of Observer Design Pattern. Observer Design Pattern is also used a lot in many GUI frameworks and event based applications. In Spring framework any bean which implments ApplicationListener
interface can receive an ApplicationEvent
published using Observer Design Pattern.
One can also implement Observer Design Pattern using java.util.Observable
class & java.util.Observer
interface. However the disadvantage is that Observable
is a class and not interface, so you need to extend the Observable
class which makes our class tightly coupled.
Below example shows how to implement Observer Design Pattern in Java
Suppose there are mutiple platforms e.g. TV, mobile, on which users watch movies. Now using a Subject interface which acts as Observable
, we want to notify these platforms about movies update like when is this movie going to be removed from the OTT platform. We will add registerPlatform(), removePlatform() methods to register/unregister any platform. notifyPlatforms() is used to notify platforms. getMovieName() & getMovieLastDate() are used to provide movie name and till what date it will be available.
public interface Subject { void registerPlatform(Platform platform); void removePlatform(Platform platform); void notifyPlatforms(); String getMovieName(); String getMovieLastDate(); }
2) Implement Movie Subject
import java.util.ArrayList; import java.util.List; public class Movie implements Subject { private List<Platform> platforms = new ArrayList<>(); private String movieName; private String lastDate; @Override public void registerPlatform(Platform platform) { platforms.add(platform); } @Override public void removePlatform(Platform platform) { platforms.remove(platform); } @Override public void notifyPlatforms() { for (Platform platform : platforms) { platform.update(this); } } @Override public String getMovieName() { return movieName; } @Override public String getMovieLastDate() { return lastDate; } public void updateMovie(String movieName, String lastDate) { this.movieName = movieName; this.lastDate = lastDate; notifyPlatforms(); } }
3) Add Platform interface and its implementations which acts as Observer
public interface Platform { void update(Subject subject); }
public class TV implements Platform { @Override public void update(Subject subject) { System.out.println("Got update for movie " + subject.getMovieName()); System.out.println("Movie last date on this platform is " + subject.getMovieLastDate()); } }
public class Mobile implements Platform { @Override public void update(Subject subject) { System.out.println("Got update for movie " + subject.getMovieName()); System.out.println("Movie last date on this platform is " + subject.getMovieLastDate()); } }
4) Lets run ObserverTest which will first register subscribers (TV, Mobile) and then update all the suscribers using updateMovie() method in Movie Subject/Observable.
public class ObserverTest { public static void main(String[] args) { Movie movie = new Movie(); movie.registerPlatform(new TV()); movie.registerPlatform(new Mobile()); movie.updateMovie("Evil Dead", "11/11/2025"); //movie.removePlatform(platform); } }
Console Output :
Got update for movie Evil Dead Movie last date on this platform is 11/11/2025 Got update for movie Evil Dead Movie last date on this platform is 11/11/2025