
//Prototype
protocol Shape {
var color: String { get set }
func clone() -> Shape
}
//ConcretePrototype
class Square: Shape {
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() as! Square
print(square1.color)
print(square2.color)
| Specify the kinds of objects to create using a prototypical instance, and create new objectsby copying this prototype. |