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 00000000..b70a1829 --- /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 00000000..231103fc --- /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 00000000..9706c2f0 --- /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 00000000..fc6b5196
--- /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