OBSERVER DESIGN PATTERN

Himanshu verma
4 min readApr 21, 2023

--

What is Observer design pattern?

Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.

There are two things :-

- Observable -> Any object whose state may be of interest, and in whom another object may register an interest.

- Observer -> Any object that wishes to be notified when the state of another object changes.

In layman terms :-In Whenever there is any change in Observable, it will notify all the observer which are aligned to it, that my state has been changed, do your work accordingly.

Didn’t get it right? :(

Let me clear it with an example :)

Suppose we have a list, and many things are observing changes in that list, we need to notify that changes in the list has been done .

Let me explain it with code: —

/**
*
*
* Example
*
* Weather station (Observable) TV Display Observer
* Current temp Mobile Display observer
* Rain station (Observable)
* Water in cm
*
*/
interface Observable {
val objList: MutableList<DisplayObserver>
get() = mutableListOf()

/**
* Registration of the observable
*/
fun add(obj: DisplayObserver)

/**
* Removal of the observable
*/
fun remove(obj: DisplayObserver)

/**
* Notifying the members
*/
fun notifyAllMember()

/**
* Setting the data
*/
fun setData(temp: Int)

/**
* Getting the data
*/
fun getData(): Int

}

/**
* Observer interface
*/
interface DisplayObserver {
fun update()
}


class WeatherStationObservable : Observable {

private var currentTemp: Int = 0
override val objList: MutableList<DisplayObserver> = mutableListOf()

override fun add(obj: DisplayObserver) {
objList.add(obj)
}

override fun remove(obj: DisplayObserver) {
if (objList.size == 0) {
println("The weather station is empty")
}
objList.remove(obj)
}

override fun notifyAllMember() {
for (obj in objList) {
obj.update()
}
}

override fun setData(temp: Int) {
currentTemp = temp
notifyAllMember()
}

override fun getData(): Int {
return currentTemp
}

}

class RainStationObservable : Observable {

private var waterInCM: Int = 0
override val objList: MutableList<DisplayObserver> = mutableListOf()

override fun add(obj: DisplayObserver) {
objList.add(obj)
}

override fun remove(obj: DisplayObserver) {
if (objList.size == 0) {
println("The rain station is empty")
}
objList.remove(obj)
}

override fun notifyAllMember() {
for (obj in objList) {
obj.update()
}
}

override fun setData(amount: Int) {
waterInCM = amount
notifyAllMember()
}

override fun getData(): Int {
return waterInCM
}

}


/**
* Observer class which inherit observer interface
*/
class MobileDisplayObserver : DisplayObserver {

val obj: Observable

/**
*Constructor injection
*/
constructor(obj: Observable) {
this.obj = obj
}

override fun update() {
val amount = obj.getData()
println("The amount is $amount")
}
}

/**
* Observer class which inherit observer interface
*/
class TvDisplayObserver : DisplayObserver {

val obj: Observable

/**
*Constructor injection
*/
constructor(obj: Observable) {
this.obj = obj
}

override fun update() {
val amount = obj.getData()
println("The amount is $amount")
}
}

fun main() {
val rainStationObservable = RainStationObservable()
val weatherStationObservable = WeatherStationObservable()
val mobileDisplayObserver1 = MobileDisplayObserver(rainStationObservable)
val mobileDisplayObserver2 = MobileDisplayObserver(weatherStationObservable)

val tvDisplayObserver1 = TvDisplayObserver(rainStationObservable)
val tvDisplayObserver2 = TvDisplayObserver(weatherStationObservable)

rainStationObservable.add(mobileDisplayObserver1)
rainStationObservable.add(tvDisplayObserver1)

weatherStationObservable.add(mobileDisplayObserver2)
weatherStationObservable.add(tvDisplayObserver2)

rainStationObservable.setData(32)

weatherStationObservable.setData(temp = 45)
rainStationObservable.remove(tvDisplayObserver1)
rainStationObservable.remove(mobileDisplayObserver1)
rainStationObservable.notifyAllMember()
rainStationObservable.remove(mobileDisplayObserver1) // It will display "The rain station is empty"

}

In this example we can see that we had different type of Clients(Mobile and TV) and different type of server(Weather station and Rain stations)

We had to send an update on both type of Clients from each server.

So we have made use of this pattern here :-

HERE’s the explanation why

We have made an Observable Interface, which have different type of functions in it.

Question comes into mind ????

Why an Interface???

Answer is, as we had different type of Servers (Weather station and Rain station) and our clients(TV and Mobile) can have both type of servers, so we have to make them usable(we can’t bound it to any one whether it be Weather station or Rain station).

We have made DisplayObserver Interface

Question comes into mind ????

Why an Interface???

As we have different clients and these clients needs the observer on which they need to send update …..So these observers can be of different type as in our case we have two (TV and Mobile), so we have to make it generic that’s why we made an interface.

Weather station and Rain station Observable class

Now we have different servers (Weather station and Rain station) basically there are the information providers to all the clients which are observing them, whenever there is a change made they will notify every client.

We are adding different type of observers, whenever we have made any change we need to notify all the observers, that’s why made different functions for the same.

Now our Observers can be of different types:-
In our case we have two Mobile and Tv observers.

In these classes, we have injected Observable Object because, a Observer can have different type of Observable, so we are injecting that type of observable.

Power of Observer Design Pattern can be seen here :-

With a single of code, we were able to make the change available to all the classes (which were observing the observerable).

References :- https://www.tutorialspoint.com/design_pattern/observer_pattern.htm

In case of doubt , ASK IT OUT :-https://www.linkedin.com/in/himanshu-verma-318903171/

--

--