Motivation: The application needs a very large number of similar objects (e.g., characters in a text editor, trees in a game world), and storing each one independently would consume too much memory.

Intent: Share the common (intrinsic) state among multiple objects, while keeping the varying (extrinsic) state external, to reduce memory usage. A factory is used to create and maintain shared objects with the same intrinsic state.

This is about sharing immutable repeated state. The goal here is to save memory.

class TreeType(val name: String, val color: String, val texture: String)
 
class TreeFactory {
    private val types = mutableMapOf<String, TreeType>()
 
    fun getTreeType(name: String, color: String, texture: String): TreeType =
        types.getOrPut("$name-$color-$texture") {
            TreeType(name, color, texture)
        }
}
 
class Tree(val type: TreeType) {
    fun draw(x: Double, y: Double) { /* ... */ }
}