Skip to content

Added the prototype pattern and its unit test #763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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";
}

}

34 changes: 34 additions & 0 deletions src/main/java/com/designpatterns/creational/prototype/Color.java
Original file line number Diff line number Diff line change
@@ -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. <p>
* 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 <a href="https://en.wikipedia.org/wiki/Prototype_pattern">Prototype Pattern</a>
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Color> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 1 addition & 5 deletions src/test/java/com/generation/SimplexNoiseTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package src.test.java.com.generation;


import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;


import org.junit.Assert;
import org.junit.Test;
import src.main.java.com.generation.SimplexNoise;
Expand All @@ -35,7 +31,7 @@ public void testGenerateHeightMap() {
Assert.assertEquals(HEIGHT, image.getHeight());

} catch (IOException | IllegalArgumentException exception) {

Assert.fail(exception.toString());
}

for (int x = 0; x < WIDTH; x++) {
Expand Down