
//Prototype
class Shape {
constructor(lineCount) {
this.lineCount = lineCount;
}
clone() {
return new Shape(this.lineCount);
}
}
//ConcretePrototype
class Square extends Shape {
constructor(lineCount) {
super(4);
}
}
//Client
class ShapeMaker {
constructor(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);
| Описывает виды создаваемых объектов с помощью прототипа и создает новые объекты путем его копирования. |