Do you think about learning events not only in Spring Boot? Have you ever thought about what events and observer pattern have in common? Independently, if you say yes or no – that’s great. This article helps you learn or recall the answers to these and more questions :). In the meantime, if you are interested in design patterns, you can find more handy information about design pattern: Adapter . Additionally, if you would like to read the article in Polish, it is here: Event-y a wzorzec obserwator.
1. The Observer patternIt is one of the behavioural design patterns in which an object can observe the state of another object and react as business logic wants. Changes of state are our events. In other words, the Observer pattern is a widespread, simple-to-use messaging method where the subject knows the observers. Observer pattern – general overview. Resource: https://refactoring.guru/design-patterns/observerThe publisher maintains a list of subscribers and knows in which object (magazine, YT channel) they are interested. Subscribers can leave the list at any time when they wish to stop the publisher from sending notifications about the chosen object to them. In our case study observer knows subscribers: Karol and Iwona. Karol is interested in „Classic Rock Music”, and Iwona is interested in Joga with Kasandra YT channel. Therefore, a new joga video notification receive only Iwona. Karol will receive notification only for classic rock music. Moreover, observers can start and stop subscribe channel at anytime they want. To visualize it in Java, please see below how the communication can work.