Skip to content

Commit d8bfa59

Browse files
authored
Merge pull request #763 from abhijay94/Development
Added the prototype pattern and its unit test
2 parents 04f6924 + 132060f commit d8bfa59

File tree

7 files changed

+123
-5
lines changed

7 files changed

+123
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src.main.java.com.designpatterns.creational.prototype;
2+
3+
class BlackColor extends Color {
4+
5+
BlackColor() {
6+
this.colorName = "black";
7+
}
8+
9+
@Override
10+
public String addColor() {
11+
return "Black color added";
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package src.main.java.com.designpatterns.creational.prototype;
2+
3+
class BlueColor extends Color {
4+
5+
BlueColor() {
6+
this.colorName = "blue";
7+
}
8+
9+
@Override
10+
public String addColor() {
11+
return "Blue color added";
12+
}
13+
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package src.main.java.com.designpatterns.creational.prototype;
2+
3+
/**
4+
* The prototype pattern is used when the type of objects to create is determined by a prototypical instance, which
5+
* is cloned to produce new objects. <p>
6+
* This pattern is used to:
7+
* 1. avoid subclasses of an object creator in the client application, like the factory method pattern does.
8+
* 2. avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is
9+
* prohibitively expensive for a given application.
10+
*
11+
* @see <a href="https://en.wikipedia.org/wiki/Prototype_pattern">Prototype Pattern</a>
12+
*/
13+
public abstract class Color implements Cloneable {
14+
15+
String colorName;
16+
17+
public abstract String addColor();
18+
19+
/**
20+
* This method should be called from the client instead of writing code that invokes the "new" operator on a
21+
* hard-coded class name.
22+
*
23+
* @return a clone for the object
24+
*/
25+
public Object clone() {
26+
Object clone = null;
27+
try {
28+
clone = super.clone();
29+
} catch (CloneNotSupportedException e) {
30+
e.printStackTrace();
31+
}
32+
return clone;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package src.main.java.com.designpatterns.creational.prototype;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ColorStore {
7+
private static Map<String, Color> colorMap = new HashMap<>();
8+
9+
static {
10+
colorMap.put("blue", new BlueColor());
11+
colorMap.put("black", new BlackColor());
12+
colorMap.put("red", new RedColor());
13+
}
14+
15+
public static Color getColor(String colorName) {
16+
return (Color) colorMap.get(colorName).clone();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src.main.java.com.designpatterns.creational.prototype;
2+
3+
class RedColor extends Color {
4+
5+
RedColor() {
6+
this.colorName = "red";
7+
}
8+
9+
@Override
10+
public String addColor() {
11+
return "Red color added";
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package src.test.java.com.designpatterns.creational.prototype;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.designpatterns.creational.prototype.ColorStore;
6+
7+
public class PrototypeTest {
8+
@Test
9+
public void testPrototype() {
10+
String testFailReason = "";
11+
String testOne = ColorStore.getColor("blue").addColor();
12+
if (!"Blue color added".equals(testOne)) {
13+
testFailReason += "TC 1 Failed: Blue couldn't be added\n";
14+
}
15+
String testTwo = ColorStore.getColor("black").addColor();
16+
if (!"Black color added".equals(testTwo)) {
17+
testFailReason += "TC 2 Failed: Black couldn't be added\n";
18+
}
19+
String testThree = ColorStore.getColor("red").addColor();
20+
if (!"Red color added".equals(testThree)) {
21+
testFailReason += "TC 3 Failed: Red couldn't be added\n";
22+
}
23+
String testFour = ColorStore.getColor("blue").addColor();
24+
if (!"Blue color added".equals(testFour)) {
25+
testFailReason += "TC 4 Failed: Blue couldn't be added\n";
26+
}
27+
Assert.assertEquals(testFailReason, "", testFailReason);
28+
}
29+
}

src/test/java/com/generation/SimplexNoiseTest.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package src.test.java.com.generation;
22

3-
43
import java.awt.Color;
54
import java.awt.image.BufferedImage;
65
import java.io.IOException;
76
import java.io.InputStream;
8-
97
import javax.imageio.ImageIO;
10-
11-
128
import org.junit.Assert;
139
import org.junit.Test;
1410
import src.main.java.com.generation.SimplexNoise;
@@ -35,7 +31,7 @@ public void testGenerateHeightMap() {
3531
Assert.assertEquals(HEIGHT, image.getHeight());
3632

3733
} catch (IOException | IllegalArgumentException exception) {
38-
34+
Assert.fail(exception.toString());
3935
}
4036

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

0 commit comments

Comments
 (0)