BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
public class Tree implements Cloneable {
|
||||
public class Tree implements TreeCloneable {
|
||||
|
||||
private double mass;
|
||||
private double height;
|
||||
@@ -36,7 +36,12 @@ public class Tree implements Cloneable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tree clone() {
|
||||
public String toString() {
|
||||
return "Tree [mass=" + mass + ", height=" + height + ", position=" + position + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeCloneable createA_Clone() {
|
||||
Tree tree = null;
|
||||
try {
|
||||
tree = (Tree) super.clone();
|
||||
@@ -46,9 +51,4 @@ public class Tree implements Cloneable {
|
||||
return tree;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tree [mass=" + mass + ", height=" + height + ", position=" + position + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
public interface TreeCloneable extends Cloneable {
|
||||
|
||||
TreeCloneable createA_Clone();
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class TreePrototypeUnitTest {
|
||||
|
||||
Tree tree = new Tree(mass, height);
|
||||
tree.setPosition(position);
|
||||
Tree anotherTree = tree.clone();
|
||||
Tree anotherTree = (Tree) tree.createA_Clone();
|
||||
anotherTree.setPosition(otherPosition);
|
||||
|
||||
assertEquals(position, tree.getPosition());
|
||||
|
||||
Reference in New Issue
Block a user