코드 출처 : Baeldung - Prototype Pattern in Java
public abstract class Tree {
private double mass;
private double height;
private Position position;
public Tree(double mass, double height) {
this.mass = mass;
this.height = height;
}
public double getMass() {
return mass;
}
public void setMass(double mass) {
this.mass = mass;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
@Override
public String toString() {
return "Tree{" +
"mass=" + mass +
", height=" + height +
", position=" + position +
'}';
}
public abstract Tree copy();
}
public final class Position {
private final int x;
private final int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Position other = (Position) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 13;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public String toString() {
return "Position{" +
"x=" + x +
", y=" + y +
'}';
}
}
public class PineTree extends Tree{
private String type;
public PineTree(double mass, double height) {
super(mass, height);
this.type = "Pine";
}
public String getType() {
return type;
}
@Override
public Tree copy() {
PineTree pineTreeClone = new PineTree(this.getMass(), this.getHeight());
pineTreeClone.setPosition(this.getPosition());
return pineTreeClone;
}
}
public class PlasticTree extends Tree{
private String name;
public PlasticTree(double mass, double height) {
super(mass, height);
this.name = "PlasticTree";
}
public String getName() {
return name;
}
@Override
public Tree copy() {
PlasticTree plasticTreeClone = new PlasticTree(this.getMass(), this.getHeight());
plasticTreeClone.setPosition(this.getPosition());
return plasticTreeClone;
}
}