Паттерны / Порождающие паттерны
image

//Prototype
protocol Shape {
    var color: String { get set }

    func clone() -> Shape
}

//ConcretePrototype
class SquareShape {
    var color: String

    init(_ color: String) {
        self.color = color
    }

    func clone() -> Shape {
        return Square(color)
    }
}

//Client
class ShapeMaker {
    private var shape: Shape

    init(_ shape: Shape) {
        self.shape = shape
    }

    func makeShape() -> Shape {
        return shape.clone()
    }
}

let square = Square("Red")
let maker = ShapeMaker(square)

let square1 = maker.makeShape()
let square2 = maker.makeShape() asSquare

print(square1.color)
print(square2.color)


Описывает виды создаваемых объектов с помощью прототипа и создает новые объекты путем его копирования.