From aafe3d90df951ca6c609015479019cf9befde68a Mon Sep 17 00:00:00 2001 From: Vivek Balasubramaniam Date: Sat, 9 Nov 2019 18:41:22 +0530 Subject: [PATCH] BAEL-3091: The Prototype Pattern in Java - Changes based on comments --- .../java/com/baeldung/prototype/PineTree.java | 21 +++++++++++++ .../com/baeldung/prototype/PlasticTree.java | 21 +++++++++++++ .../java/com/baeldung/prototype/Tree.java | 14 ++------- .../com/baeldung/prototype/TreeCloneable.java | 6 ---- .../prototype/TreePrototypeUnitTest.java | 31 ++++++++++++++----- 5 files changed, 68 insertions(+), 25 deletions(-) create mode 100644 patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PineTree.java create mode 100644 patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PlasticTree.java delete mode 100644 patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/TreeCloneable.java diff --git a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PineTree.java b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PineTree.java new file mode 100644 index 0000000000..a8f79bc03e --- /dev/null +++ b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PineTree.java @@ -0,0 +1,21 @@ +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() { + return new PineTree(this.getMass(), this.getHeight()); + } + +} diff --git a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PlasticTree.java b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PlasticTree.java new file mode 100644 index 0000000000..854334c69b --- /dev/null +++ b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/PlasticTree.java @@ -0,0 +1,21 @@ +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() { + return new PlasticTree(this.getMass(), this.getHeight()); + } + +} diff --git a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java index 513f720885..dffb573057 100644 --- a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java +++ b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/Tree.java @@ -1,6 +1,6 @@ package com.baeldung.prototype; -public class Tree implements TreeCloneable { +public abstract class Tree { private double mass; private double height; @@ -40,15 +40,5 @@ public class Tree implements TreeCloneable { return "Tree [mass=" + mass + ", height=" + height + ", position=" + position + "]"; } - @Override - public TreeCloneable createA_Clone() { - Tree tree = null; - try { - tree = (Tree) super.clone(); - } catch (CloneNotSupportedException e) { - e.printStackTrace(); - } - return tree; - } - + public abstract Tree copy(); } diff --git a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/TreeCloneable.java b/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/TreeCloneable.java deleted file mode 100644 index 7767b8de73..0000000000 --- a/patterns/design-patterns-creational/src/main/java/com/baeldung/prototype/TreeCloneable.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.prototype; - -public interface TreeCloneable extends Cloneable { - - TreeCloneable createA_Clone(); -} diff --git a/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java b/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java index ef42d92899..cbb4425749 100644 --- a/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java +++ b/patterns/design-patterns-creational/src/test/java/com/baeldung/prototype/TreePrototypeUnitTest.java @@ -7,18 +7,35 @@ import org.junit.jupiter.api.Test; public class TreePrototypeUnitTest { @Test - public void givenATreePrototypeWhenClonedThenCreateA_Clone() { + public void givenAPlasticTreePrototypeWhenClonedThenCreateA_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); + PlasticTree plasticTree = new PlasticTree(mass, height); + plasticTree.setPosition(position); + PlasticTree anotherPlasticTree = (PlasticTree) plasticTree.copy(); + anotherPlasticTree.setPosition(otherPosition); - assertEquals(position, tree.getPosition()); - assertEquals(otherPosition, anotherTree.getPosition()); + assertEquals(position, plasticTree.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()); } }