M2 · Separating Object CreationSession 4-5CreationalKotlin replaces itOutline

Intent. Create new objects by cloning an existing instance.

In Kotlin. data class gives you copy() for the shallow case.

Copy an existing instance instead of constructing a new one. data class and copy() cover the shallow case outright, so the interesting question is what happens when the copy has to be deep.

Structure

classDiagram
    class Prototype {
        <<interface>>
        +clone() Prototype
    }
    class ConcreteA {
        -field
        +clone() Prototype
    }
    class ConcreteB {
        -field
        +clone() Prototype
    }
    Prototype <|.. ConcreteA
    Prototype <|.. ConcreteB