https://kotlinlang.org logo
#android
Title
# android
m

Mohamed Ibrahim

10/16/2019, 10:41 AM
int for example the
ApiManager.INSTANCE.doSomething()
, can we override the INSTANCE getter function?
g

gildor

10/16/2019, 10:45 AM
What is your use case for this?
m

Mohamed Ibrahim

10/16/2019, 11:12 AM
I want to check if a variable is initialized inside that class
s

Sergio C.

10/16/2019, 11:16 AM
::something.isInitialized
g

gildor

10/16/2019, 12:17 PM
You cannot override
object
instance directly. Check it in doSomething() Or alternatively expose this object using own property that would check a variable
But in general this approach smells, same as most of usages of Singleton pattern with initialization
m

Mohamed Ibrahim

10/16/2019, 1:16 PM
yes but Object is a
Singleton
which may depend on something that is not there yet
g

gildor

10/16/2019, 1:18 PM
Yes, I got your case, and saying that you can do check using special getter function, just abstract actual object, but Singleton with initialization sounds as antipattern and you probably should use standard class with constructor instead
👍 1
k

Kris Wong

10/16/2019, 1:26 PM
sounds like a job for DI or service locator
👍 4
6 Views