Created
July 17, 2023 16:29
-
-
Save NonCoderF/1b75836a7ae7929342e4ab3e912b6f13 to your computer and use it in GitHub Desktop.
Modified Observer with single JSON Object design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONObject | |
typealias JsonObserver = (jsonObject: JSONObject) -> Unit | |
class JSONSubject { | |
companion object { | |
fun instance() = JSONSubject() | |
} | |
private var observer: JsonObserver? = null | |
private val jsonObject: JSONObject = JSONObject() | |
fun registerObserver(observer: JsonObserver) { this.observer = observer } | |
fun updateKey(key: String, value: Any) { | |
jsonObject.put(key, value) | |
observer?.let { it(jsonObject) } | |
} | |
fun removeKey(key: String){ | |
jsonObject.remove(key) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment