Writing a delegate

Report a typo

We have a UserSettings interface, which defines two settings: its language and theme. The UserSettingsImpl class implements this interface and stores the settings.
You need to write an implementation of the LoggingUserSettingsDelegate class, which implements the UserSettings interface and delegates its functionality to the userSettings object. In this delegate, you must override the language and theme properties by adding message output to the console when you change settings.

println("Language changed from ${userSettings.language} to $value")

and

println("Theme changed from ${userSettings.theme} to $value")

Example output

Current language: English
Current theme: Light
Language changed from English to Spanish
Theme changed from Light to Dark
Write a program in Kotlin
interface UserSettings {
var language: String
var theme: String
}
class UserSettingsImpl : UserSettings {
override var language: String = "English"
override var theme: String = "Light"
}

class LoggingUserSettingsDelegate() {
// make your code here
}

class UserSettingsWithLogging(userSettings: UserSettings) : UserSettings by LoggingUserSettingsDelegate(userSettings)

Create a free account to access the full topic