Skip to content

Commit d2fbe02

Browse files
dhananjay-ngDhananjay Nagargoje
authored andcommitted
#DP : Prototype Prototyped :)
1 parent 6f148a5 commit d2fbe02

File tree

7 files changed

+303
-131
lines changed

7 files changed

+303
-131
lines changed

.idea/workspace.xml

Lines changed: 179 additions & 131 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package designpatterns.creational.prototype;
2+
3+
public class Album implements PrototypeCapable {
4+
private String name = null;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public Album clone() throws CloneNotSupportedException {
16+
System.out.println("Cloning Album object..");
17+
return (Album) super.clone();
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Album";
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package designpatterns.creational.prototype;
2+
3+
4+
public class Movie implements PrototypeCapable {
5+
private String name = null;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
@Override
16+
public Movie clone() throws CloneNotSupportedException {
17+
System.out.println("Cloning Movie object..");
18+
return (Movie) super.clone();
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package designpatterns.creational.prototype;
2+
3+
public interface PrototypeCapable extends Cloneable {
4+
public PrototypeCapable clone() throws CloneNotSupportedException;
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package designpatterns.creational.prototype;
2+
3+
4+
public class PrototypeFactory {
5+
public static class ModelType {
6+
public static final String MOVIE = "movie";
7+
public static final String ALBUM = "album";
8+
public static final String SHOW = "show";
9+
}
10+
11+
private static java.util.Map<String, PrototypeCapable> prototypes = new java.util.HashMap<String, PrototypeCapable>();
12+
13+
static {
14+
prototypes.put(ModelType.MOVIE, new Movie());
15+
prototypes.put(ModelType.ALBUM, new Album());
16+
prototypes.put(ModelType.SHOW, new Show());
17+
}
18+
19+
public static PrototypeCapable getInstance(final String s) throws CloneNotSupportedException {
20+
return ((PrototypeCapable) prototypes.get(s)).clone();
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package designpatterns.creational.prototype;
2+
3+
public class Show implements PrototypeCapable {
4+
private String name = null;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public Show clone() throws CloneNotSupportedException {
16+
System.out.println("Cloning Show object..");
17+
return (Show) super.clone();
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Show";
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package designpatterns.creational.prototype;
2+
3+
public class TestPrototypePattern {
4+
public static void main(String[] args) {
5+
try {
6+
Movie moviePrototype = (Movie) PrototypeFactory.getInstance(PrototypeFactory.ModelType.MOVIE);
7+
System.out.println(moviePrototype);
8+
9+
String albumPrototype = PrototypeFactory.getInstance(PrototypeFactory.ModelType.ALBUM).toString();
10+
System.out.println(albumPrototype);
11+
12+
String showPrototype = PrototypeFactory.getInstance(PrototypeFactory.ModelType.SHOW).toString();
13+
System.out.println(showPrototype);
14+
15+
Movie moviePrototype2 = (Movie) PrototypeFactory.getInstance(PrototypeFactory.ModelType.MOVIE);
16+
System.out.println(moviePrototype2);
17+
18+
19+
String albumPrototype2= PrototypeFactory.getInstance(PrototypeFactory.ModelType.ALBUM).toString();
20+
System.out.println(albumPrototype);
21+
22+
String showPrototype2 = PrototypeFactory.getInstance(PrototypeFactory.ModelType.SHOW).toString();
23+
System.out.println(showPrototype);
24+
25+
} catch (CloneNotSupportedException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)