
//Prototype
class Shape implements Cloneable {
public int lineCount;
public Shape(int lineCount) {
this.lineCount = lineCount;
}
public Shape clone() {
try {
return (Shape) super.clone();
}
catch (CloneNotSupportedException e) {
return null;
}
}
}
//ConcretePrototype
class Square extends Shape {
public Square() {
super(4);
}
}
//Client
class ShapeMaker {
private Shape shape;
public ShapeMaker(Shape shape) {
this.shape = shape;
}
public Shape makeShape() {
return shape.clone();
}
}
var square = new Square();
var maker = new ShapeMaker(square);
var square1 = maker.makeShape();
var square2 = (Square)maker.makeShape();
System.out.println("lineCount1 is " +
square1.lineCount);
System.out.println("lineCount2 is " +
square2.lineCount);