I have a class which behaves like a Singleton. I w...
# android
l
I have a class which behaves like a Singleton. I want to set its state in the data layer and want to use it as read-only object in the presenter. The state will be just 2 or 3 observable flags, like MutableStateFlow. How can I make the state of this object mutable in data layer but immutable in presentation layer?
s
use it as a backing field
l
What is a backing field? Can you give me some pseudo code please? Edit: ok I checked backing fields, but don't understand how this helps me
The most obvious solution I'm aware of is to use internal modifier which implies the class file relies in data layer, so I can do:
Copy code
class SharedState {
  internal val _myState: MutableStateFlow<Boolean> = MutableStateFlow(false)

  val myState: StateFlow = _myState
}
But what options do I have if SharedState class doesn't rely in data layer, so that internal modifier is not possible?
s
kind of hard to give suggestion without understanding your package structure or "data layer" but seems to me if your "data layer" is what is hydrating your state data, why not just have your singleton live there?
l
Nothing wrong with that. Just looking for alternatives to restrict access to class memebers from different callers. Another solution would be interfaces
Basically I'm looking for package-private visibility or
friend
class behavior like in C#