
//Prototype
class Shape {
public lineCount = 0
constructor(lineCount: number) {
this.lineCount = lineCount
}
clone() {
return new Shape(this.lineCount)
}
}
//ConcretePrototype
class Square extends Shape {
constructor() {
super(4)
}
}
//Client
class ShapeMaker {
private _shape: Shape
constructor(shape: Shape) {
this._shape = shape;
}
makeShape() {
return this._shape.clone()
}
}
let square = new Square()
let maker = new ShapeMaker(square)
let square1 = maker.makeShape()
let square2 = maker.makeShape()
console.log("square1.lineCount is",
square1.lineCount)
console.log("square2.lineCount is",
square2.lineCount)
| Описывает виды создаваемых объектов с помощью прототипа и создает новые объекты путем его копирования. |