File tree Expand file tree Collapse file tree 6 files changed +122
-0
lines changed
main/java/com/designpatterns/creational/prototype
test/java/com/designpatterns/creational/prototype Expand file tree Collapse file tree 6 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments