From ba900596249d37476eddb9cccdc16b5a08911ecb Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Tue, 21 May 2019 14:14:43 +0530 Subject: [PATCH 1/2] Added the prototype pattern and its unit test --- .../creational/prototype/BlackColor.java | 13 +++++++ .../creational/prototype/BlueColor.java | 15 ++++++++ .../creational/prototype/Color.java | 34 +++++++++++++++++++ .../creational/prototype/ColorStore.java | 18 ++++++++++ .../creational/prototype/RedColor.java | 13 +++++++ .../creational/prototype/PrototypeTest.java | 29 ++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 src/main/java/com/designpatterns/creational/prototype/BlackColor.java create mode 100644 src/main/java/com/designpatterns/creational/prototype/BlueColor.java create mode 100644 src/main/java/com/designpatterns/creational/prototype/Color.java create mode 100644 src/main/java/com/designpatterns/creational/prototype/ColorStore.java create mode 100644 src/main/java/com/designpatterns/creational/prototype/RedColor.java create mode 100644 src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java diff --git a/src/main/java/com/designpatterns/creational/prototype/BlackColor.java b/src/main/java/com/designpatterns/creational/prototype/BlackColor.java new file mode 100644 index 000000000000..b70a18296f46 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/prototype/BlackColor.java @@ -0,0 +1,13 @@ +package src.main.java.com.designpatterns.creational.prototype; + +class BlackColor extends Color { + + BlackColor() { + this.colorName = "black"; + } + + @Override + public String addColor() { + return "Black color added"; + } +} \ No newline at end of file diff --git a/src/main/java/com/designpatterns/creational/prototype/BlueColor.java b/src/main/java/com/designpatterns/creational/prototype/BlueColor.java new file mode 100644 index 000000000000..231103fc0769 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/prototype/BlueColor.java @@ -0,0 +1,15 @@ +package src.main.java.com.designpatterns.creational.prototype; + +class BlueColor extends Color { + + BlueColor() { + this.colorName = "blue"; + } + + @Override + public String addColor() { + return "Blue color added"; + } + +} + diff --git a/src/main/java/com/designpatterns/creational/prototype/Color.java b/src/main/java/com/designpatterns/creational/prototype/Color.java new file mode 100644 index 000000000000..9706c2f00cb8 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/prototype/Color.java @@ -0,0 +1,34 @@ +package src.main.java.com.designpatterns.creational.prototype; + +/** + * The prototype pattern is used when the type of objects to create is determined by a prototypical instance, which + * is cloned to produce new objects.

+ * This pattern is used to: + * 1. avoid subclasses of an object creator in the client application, like the factory method pattern does. + * 2. avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is + * prohibitively expensive for a given application. + * + * @see Prototype Pattern + */ +public abstract class Color implements Cloneable { + + String colorName; + + public abstract String addColor(); + + /** + * This method should be called from the client instead of writing code that invokes the "new" operator on a + * hard-coded class name. + * + * @return a clone for the object + */ + public Object clone() { + Object clone = null; + try { + clone = super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return clone; + } +} \ No newline at end of file diff --git a/src/main/java/com/designpatterns/creational/prototype/ColorStore.java b/src/main/java/com/designpatterns/creational/prototype/ColorStore.java new file mode 100644 index 000000000000..fc6b51961c70 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/prototype/ColorStore.java @@ -0,0 +1,18 @@ +package src.main.java.com.designpatterns.creational.prototype; + +import java.util.HashMap; +import java.util.Map; + +public class ColorStore { + private static Map colorMap = new HashMap<>(); + + static { + colorMap.put("blue", new BlueColor()); + colorMap.put("black", new BlackColor()); + colorMap.put("red", new RedColor()); + } + + public static Color getColor(String colorName) { + return (Color) colorMap.get(colorName).clone(); + } +} diff --git a/src/main/java/com/designpatterns/creational/prototype/RedColor.java b/src/main/java/com/designpatterns/creational/prototype/RedColor.java new file mode 100644 index 000000000000..96392c432787 --- /dev/null +++ b/src/main/java/com/designpatterns/creational/prototype/RedColor.java @@ -0,0 +1,13 @@ +package src.main.java.com.designpatterns.creational.prototype; + +class RedColor extends Color { + + RedColor() { + this.colorName = "red"; + } + + @Override + public String addColor() { + return "Red color added"; + } +} diff --git a/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java b/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java new file mode 100644 index 000000000000..e1a2c7a6f23e --- /dev/null +++ b/src/test/java/com/designpatterns/creational/prototype/PrototypeTest.java @@ -0,0 +1,29 @@ +package src.test.java.com.designpatterns.creational.prototype; + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.designpatterns.creational.prototype.ColorStore; + +public class PrototypeTest { + @Test + public void testPrototype() { + String testFailReason = ""; + String testOne = ColorStore.getColor("blue").addColor(); + if (!"Blue color added".equals(testOne)) { + testFailReason += "TC 1 Failed: Blue couldn't be added\n"; + } + String testTwo = ColorStore.getColor("black").addColor(); + if (!"Black color added".equals(testTwo)) { + testFailReason += "TC 2 Failed: Black couldn't be added\n"; + } + String testThree = ColorStore.getColor("red").addColor(); + if (!"Red color added".equals(testThree)) { + testFailReason += "TC 3 Failed: Red couldn't be added\n"; + } + String testFour = ColorStore.getColor("blue").addColor(); + if (!"Blue color added".equals(testFour)) { + testFailReason += "TC 4 Failed: Blue couldn't be added\n"; + } + Assert.assertEquals(testFailReason, "", testFailReason); + } +} From 4fd7698fd216e88d8228096fe2945c7aac428145 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Tue, 21 May 2019 14:21:25 +0530 Subject: [PATCH 2/2] Updated SimpleNoise test to use org.juint.Assert instead of jupiter --- .../java/com/generation/SimplexNoiseTest.java | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/src/test/java/com/generation/SimplexNoiseTest.java b/src/test/java/com/generation/SimplexNoiseTest.java index a977e8e76f77..d3ca2ae6a641 100644 --- a/src/test/java/com/generation/SimplexNoiseTest.java +++ b/src/test/java/com/generation/SimplexNoiseTest.java @@ -1,7 +1,5 @@ package src.test.java.com.generation; -import static org.junit.jupiter.api.Assertions.*; - import java.awt.Color; import java.awt.image.BufferedImage; import java.io.IOException; @@ -9,42 +7,43 @@ import javax.imageio.ImageIO; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import src.main.java.com.generation.SimplexNoise; public class SimplexNoiseTest { - @Test - public void testGenerateHeightMap() { - - final int WIDTH = 256; - final int HEIGHT = 256; - final int X = 0; - final int Y = 0; - final String RESOURCE_NAME = "src/test/java/com/generation/expected-result.png"; - - float[][] heightmap = new SimplexNoise(50, 0.3F, 1111111111111111L).generateHeightMap(X, Y, WIDTH, HEIGHT); - BufferedImage image = null; - - try(InputStream in = this.getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) { - - image = ImageIO.read(in); - - assertEquals(WIDTH, image.getWidth()); - assertEquals(HEIGHT, image.getHeight()); - - } catch(IOException | IllegalArgumentException exception) { - - fail(exception); - } - - for(int x = 0; x < WIDTH; x++) { - - for(int y = 0; y < HEIGHT; y++) { - - assertEquals(new Color(image.getRGB(x, y)).getRed(), (int)(heightmap[x][y] * 255)); - } - } - } + @Test + public void testGenerateHeightMap() { + + final int WIDTH = 256; + final int HEIGHT = 256; + final int X = 0; + final int Y = 0; + final String RESOURCE_NAME = "src/test/java/com/generation/expected-result.png"; + + float[][] heightmap = new SimplexNoise(50, 0.3F, 1111111111111111L).generateHeightMap(X, Y, WIDTH, HEIGHT); + BufferedImage image = null; + + try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) { + + image = ImageIO.read(in); + + Assert.assertEquals(WIDTH, image.getWidth()); + Assert.assertEquals(HEIGHT, image.getHeight()); + + } catch (IOException | IllegalArgumentException exception) { + + Assert.fail(exception.toString()); + } + + for (int x = 0; x < WIDTH; x++) { + + for (int y = 0; y < HEIGHT; y++) { + + Assert.assertEquals(new Color(image.getRGB(x, y)).getRed(), (int) (heightmap[x][y] * 255)); + } + } + } }