https://kotlinlang.org logo
Title
a

Andreas Sinz

02/19/2018, 11:32 AM
@mplacona you could also use the decorator pattern to cache data. have a common interface and create one implementation that fetches the data and another one that just caches the data:
interface FooTags {
    fun getTags(): Map<String, Int>
}

class FooTagsRetriever : FooTags {
    override fun getTags(): Map<String, Int> {
        //Get Tags via HTTP
    }
}

class FooTagsCache(private val retriever: FooTags): FooTags {
    private lateinit var tagCache: Map<String, Int>

    override fun getTags(): Map<String, Int> {
        If(!::tagCache.isInitialized) {
            tagCache = retriever.getTags())
        }
        
       return tagCache
}
Then you have transparent caching and can easily test every part in isolation
m

mplacona

02/19/2018, 12:04 PM
That's awesome! Thank you!
wait.. what does
!::
do? I get "!" is NOT, but what does the
::
after that do?
a

Andreas Sinz

02/19/2018, 12:52 PM
@mplacona it gets a reference to the
tagCache
-property: https://kotlinlang.org/docs/reference/reflection.html#property-references
m

mplacona

02/19/2018, 12:53 PM
TIL! Thanks again