🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Definition
The Observer Design Pattern defines a dependency between objects such that when one object changes its state, all its dependents (observers) are notified and updated automatically.
2. Problem Statement
In a scenario where the state of one object (subject) is crucial to multiple other objects, there needs to be an efficient way to communicate state changes without tightly coupling these objects together.
3. Solution
The Observer pattern involves a subject and multiple observers. Observers register themselves to the subject to receive updates. When the subject's state changes, it broadcasts an update to all registered observers.
4. Real-World Use Cases
1. A news agency publishing news and subscribers receiving real-time updates.
2. GUI elements that respond to changes in data (like data binding).
3. Monitoring systems where a change in one component may affect others.
5. Implementation Steps
1. Define the Observer interface that outlines methods for being notified of updates.
2. Define the Subject interface that allows observers to register, unregister, and is responsible for notifying them.
3. Implement concrete classes for both Observer and Subject.
6. Implementation in Kotlin Programming
// Step 1: Observer interface
interface Observer {
fun update(message: String)
}
// Step 2: Subject interface
interface Subject {
fun registerObserver(o: Observer)
fun unregisterObserver(o: Observer)
fun notifyObservers()
}
// Step 3: Concrete implementations
class NewsAgency : Subject {
private val observers: MutableList<Observer> = mutableListOf()
private var news: String = ""
override fun registerObserver(o: Observer) {
observers.add(o)
}
override fun unregisterObserver(o: Observer) {
observers.remove(o)
}
override fun notifyObservers() {
for (observer in observers) {
observer.update(news)
}
}
fun postNews(newNews: String) {
news = newNews
notifyObservers()
}
}
class NewsSubscriber(private val name: String) : Observer {
override fun update(message: String) {
println("$name received the news: $message")
}
}
// Client Code
fun main() {
val newsAgency = NewsAgency()
val subscriber1 = NewsSubscriber("Subscriber 1")
val subscriber2 = NewsSubscriber("Subscriber 2")
newsAgency.registerObserver(subscriber1)
newsAgency.registerObserver(subscriber2)
newsAgency.postNews("Breaking News: Observer Pattern in Kotlin!")
}
Output:
Subscriber 1 received the news: Breaking News: Observer Pattern in Kotlin! Subscriber 2 received the news: Breaking News: Observer Pattern in Kotlin!
Explanation:
1. The Observer and Subject interfaces define a contract for the pattern.
2. NewsAgency, our concrete subject, maintains a list of observers and notifies them whenever new news is posted.
3. NewsSubscriber, our concrete observer, defines how each observer should react when they receive an update.
7. When to use?
The Observer pattern is useful when:
1. One-to-many dependency is established where a change in one object must be broadcasted to others.
2. You want to build a decoupled system where the subject and its observers can evolve independently.
3. Changes in the subject might affect other dependent objects and those changes need to be communicated automatically.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment