Motivation: Constructing certain objects is complex or resource-intensive, and the client needs copies of an existing object rather than building from scratch.
Intent: Provide a way to clone existing objects without depending on their concrete classes.
This in practice seems to be a way to clone things.
data class Shape(
val type: String,
val color: String,
val x: Int,
val y: Int,
)
// Usage: clone via copy() with optional changes
val original = Shape("circle", "red", 10, 20)
val cloned = original.copy(color = "blue")data class Canvas(val width: Int, val height: Int, val shapes: List<Shape>) : Cloneable {
override fun clone(): Canvas {
return Canvas(width, height, shapes.map { it.clone() })
}
}