BAEL-3091: The Prototype Pattern in Java
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
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 int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + x;
|
||||
result = prime * result + y;
|
||||
return result;
|
||||
}
|
||||
|
||||
@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 String toString() {
|
||||
return "Position [x=" + x + ", y=" + y + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
public class Tree implements TreeCloneable {
|
||||
|
||||
private double mass;
|
||||
private double height;
|
||||
private Position position;
|
||||
|
||||
public Tree(double mass, double height) {
|
||||
this.mass = mass;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public void setMass(double mass) {
|
||||
this.mass = mass;
|
||||
}
|
||||
|
||||
public void setHeight(double height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public void setPosition(Position position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public double getMass() {
|
||||
return mass;
|
||||
}
|
||||
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeCloneable createA_Clone() {
|
||||
TreeCloneable tree = null;
|
||||
try {
|
||||
tree = (TreeCloneable) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SomeTree [mass=" + mass + ", height=" + height + ", position=" + position + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
public interface TreeCloneable extends Cloneable {
|
||||
|
||||
public TreeCloneable createA_Clone();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TreePrototypeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenATreePrototypeWhenClonedThenCreateA_Clone() {
|
||||
double mass = 10.0;
|
||||
double height = 3.7;
|
||||
Position position = new Position(3, 7);
|
||||
Position otherPosition = new Position(4, 8);
|
||||
|
||||
Tree tree = new Tree(mass, height);
|
||||
tree.setPosition(position);
|
||||
Tree anotherTree = (Tree) tree.createA_Clone();
|
||||
anotherTree.setPosition(otherPosition);
|
||||
|
||||
assertEquals(position, tree.getPosition());
|
||||
assertEquals(otherPosition, anotherTree.getPosition());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user