Sitemap
Amit Dhiman

Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS

Observer Design Pattern

2 min readJul 5, 2023

This design pattern is very important. Let's discuss.

The Observer design pattern is a behavioral pattern that establishes a one-to-many dependency between objects, such that when one object changes its state, all its dependents are notified and updated automatically. It is useful in scenarios where multiple objects need to be notified about changes in another object’s state.

Here’s an example of implementing the Observer pattern in Kotlin:

// Subject interface
interface Subject {
fun attach(observer: Observer)
fun detach(observer: Observer)
fun notifyObservers()
}

// Concrete subject
class ConcreteSubject : Subject {
private val observers: MutableList<Observer> = mutableListOf()
private var state: Int = 0

fun setState(newState: Int) {
state = newState
notifyObservers()
}

override fun attach(observer: Observer) {
observers.add(observer)
}

override fun detach(observer: Observer) {
observers.remove(observer)
}

override fun notifyObservers() {
for (observer in observers) {
observer.update(state)
}
}
}

// Observer interface
interface Observer {
fun update(state: Int)
}

// Concrete observers
class ConcreteObserver(private val name: String) : Observer {
override fun update(state: Int) {
println("$name: Received update. New state is $state")
}
}

// Client code
fun main() {
val subject = ConcreteSubject()

val observer1 = ConcreteObserver("Observer 1")
val observer2 = ConcreteObserver("Observer 2")
val observer3 = ConcreteObserver("Observer 3")

subject.attach(observer1)
subject.attach(observer2)
subject.attach(observer3)

subject.setState(42)

subject.detach(observer2)

subject.setState(99)
}

In this example, we have a Subject interface that defines the contract for attaching, detaching, and notifying observers. The ConcreteSubject class implements this interface and maintains a list of observers. It has a setState method that updates its state and triggers the notification of observers.

Get Amit Dhiman’s stories in your inbox

Join Medium for free to get updates from this writer.

The Observer interface declares the update method that is called when the subject notifies the observer. The ConcreteObserver class implements this interface and provides the update method implementation to receive and handle updates from the subject.

In the main function, we create a ConcreteSubject instance and several ConcreteObserver instances. We attach the observers to the subject using the attach method. Then, we set the state of the subject using the setState method, which triggers the notification of observers.

Each observer receives the update and performs its own logic in the update method. In this example, we simply print a message stating the observer's name and the new state.

We can also detach observers using the detach method if we no longer want them to receive notifications.

When running the code, the output will demonstrate that the observers are notified and updated automatically when the subject’s state changes.

By using the Observer pattern, we achieve loose coupling between the subject and observers, as the subject doesn’t need to know the specific details of the observers. Observers can be added or removed dynamically, allowing for flexibility and extensibility in the system.

--

--

Amit Dhiman
Amit Dhiman

Written by Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS