Merge pull request #8105 from vimde/master
BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)
This commit is contained in:
commit
b305d9b362
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.prototype;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.prototype;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.prototype;
|
package com.baeldung.prototype;
|
||||||
|
|
||||||
public class Tree implements Cloneable {
|
public abstract class Tree {
|
||||||
|
|
||||||
private double mass;
|
private double mass;
|
||||||
private double height;
|
private double height;
|
||||||
|
@ -35,20 +35,10 @@ public class Tree implements Cloneable {
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Tree clone() {
|
|
||||||
Tree tree = null;
|
|
||||||
try {
|
|
||||||
tree = (Tree) super.clone();
|
|
||||||
} catch (CloneNotSupportedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return tree;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Tree [mass=" + mass + ", height=" + height + ", position=" + position + "]";
|
return "Tree [mass=" + mass + ", height=" + height + ", position=" + position + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract Tree copy();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,23 +2,67 @@ package com.baeldung.prototype;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class TreePrototypeUnitTest {
|
public class TreePrototypeUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenATreePrototypeWhenClonedThenCreateA_Clone() {
|
public void givenAPlasticTreePrototypeWhenClonedThenCreateA_Clone() {
|
||||||
double mass = 10.0;
|
double mass = 10.0;
|
||||||
double height = 3.7;
|
double height = 3.7;
|
||||||
Position position = new Position(3, 7);
|
Position position = new Position(3, 7);
|
||||||
Position otherPosition = new Position(4, 8);
|
Position otherPosition = new Position(4, 8);
|
||||||
|
|
||||||
Tree tree = new Tree(mass, height);
|
PlasticTree plasticTree = new PlasticTree(mass, height);
|
||||||
tree.setPosition(position);
|
plasticTree.setPosition(position);
|
||||||
Tree anotherTree = tree.clone();
|
PlasticTree anotherPlasticTree = (PlasticTree) plasticTree.copy();
|
||||||
anotherTree.setPosition(otherPosition);
|
anotherPlasticTree.setPosition(otherPosition);
|
||||||
|
|
||||||
assertEquals(position, tree.getPosition());
|
assertEquals(position, plasticTree.getPosition());
|
||||||
assertEquals(otherPosition, anotherTree.getPosition());
|
assertEquals(otherPosition, anotherPlasticTree.getPosition());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAPineTreePrototypeWhenClonedThenCreateA_Clone() {
|
||||||
|
double mass = 10.0;
|
||||||
|
double height = 3.7;
|
||||||
|
Position position = new Position(3, 7);
|
||||||
|
Position otherPosition = new Position(4, 8);
|
||||||
|
|
||||||
|
PineTree pineTree = new PineTree(mass, height);
|
||||||
|
pineTree.setPosition(position);
|
||||||
|
PineTree anotherPineTree = (PineTree) pineTree.copy();
|
||||||
|
anotherPineTree.setPosition(otherPosition);
|
||||||
|
|
||||||
|
assertEquals(position, pineTree.getPosition());
|
||||||
|
assertEquals(otherPosition, anotherPineTree.getPosition());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenA_ListOfTreesWhenClonedThenCreateListOfClones() {
|
||||||
|
double mass = 10.0;
|
||||||
|
double height = 3.7;
|
||||||
|
Position position = new Position(3, 7);
|
||||||
|
Position otherPosition = new Position(4, 8);
|
||||||
|
|
||||||
|
PlasticTree plasticTree = new PlasticTree(mass, height);
|
||||||
|
plasticTree.setPosition(position);
|
||||||
|
PineTree pineTree = new PineTree(mass, height);
|
||||||
|
pineTree.setPosition(otherPosition);
|
||||||
|
|
||||||
|
List<Tree> trees = Arrays.asList(plasticTree, pineTree);
|
||||||
|
|
||||||
|
List<Tree> treeClones = trees.stream().map(Tree::copy).collect(toList());
|
||||||
|
|
||||||
|
Tree plasticTreeClone = treeClones.get(0);
|
||||||
|
|
||||||
|
assertEquals(mass, plasticTreeClone.getMass());
|
||||||
|
assertEquals(height, plasticTreeClone.getHeight());
|
||||||
|
assertEquals(position, plasticTreeClone.getPosition());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue