Kotlin observable pattern: Implementing subscription events
Kotlin OOP Program: Exercise-6 with Solution
Write a Kotlin object-oriented program that creates an interface Observable with methods subscribe and unsubscribe. Implement it in a class Publisher to allow objects to subscribe and unsubscribe from events.
Sample Solution:
Kotlin Code:
interface Observable {
fun subscribe(observer: Observer)
fun unsubscribe(observer: Observer)
}
interface Observer {
fun update(message: String)
}
class Publisher : Observable {
private val observers: MutableList<Observer> = mutableListOf()
override fun subscribe(observer: Observer) {
observers.add(observer)
println("Observer ${observer.javaClass.simpleName} subscribed.")
}
override fun unsubscribe(observer: Observer) {
observers.remove(observer)
println("Observer ${observer.javaClass.simpleName} unsubscribed.")
}
fun publishMessage(message: String) {
println("Publishing message: $message")
observers.forEach { observer ->
observer.update(message)
}
}
}
class EmailNotification : Observer {
override fun update(message: String) {
println("Email notification received: $message")
// Logic to send an email notification
}
}
class SMSNotification : Observer {
override fun update(message: String) {
println("SMS notification received: $message")
// Logic to send an SMS notification
}
}
fun main() {
val publisher = Publisher()
val emailNotification = EmailNotification()
val smsNotification = SMSNotification()
publisher.subscribe(emailNotification)
publisher.subscribe(smsNotification)
publisher.publishMessage("Hello, subscribers!")
publisher.unsubscribe(emailNotification)
publisher.publishMessage("Goodbye, subscribers!")
}
Sample Output:
Observer EmailNotification subscribed. Observer SMSNotification subscribed. Publishing message: Hello, subscribers! Email notification received: Hello, subscribers! SMS notification received: Hello, subscribers! Observer EmailNotification unsubscribed. Publishing message: Goodbye, subscribers! SMS notification received: Goodbye, subscribers!
Explanation:
In the above exercise we have an Observable interface that defines the methods subscribe and unsubscribe. These methods allow objects to subscribe and unsubscribe from events.
The Observer interface defines the update method called when an event occurs.
Publisher Class: The Publisher class implements the Observable interface. It maintains a list of observers and provides subscribe and unsubscribe methods to manage them. The publishMessage method publishes a message to all subscribed observers. It calls the update method on each observer.
EmailNotification and SMSNotification classes: The EmailNotification and SMSNotification classes implement the Observer interface and define their own behavior in the update method.
In the "main()" function, we create an instance of the Publisher class. We also create instances of the EmailNotification and SMSNotification classes.
Subscribe: We then subscribe the emailNotification and smsNotification objects to the publisher by calling the subscribe method. After that, we publish a message using the publishMessage method of the publisher.
Unsubscribe: Next, we unsubscribe the emailNotification object from the publisher by calling the unsubscribe method. Finally, we publish another message using the publishMessage method.
Kotlin Editor:
Previous: Creating animal instances.
Next: Building hierarchical structures.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/kotlin-exercises/oop/kotlin-oop-exercise-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics