X Tutup
Skip to content

Commit 4ae82e4

Browse files
committed
added prototype sample
1 parent d6bff9f commit 4ae82e4

File tree

15 files changed

+260
-0
lines changed

15 files changed

+260
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
<module>abstract-factory</module>
2222
<module>builder</module>
2323
<module>factory-method</module>
24+
<module>prototype</module>
2425
</modules>
2526
</project>

prototype/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>prototype</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>prototype</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
HeroFactory factory;
8+
Mage mage;
9+
Warlord warlord;
10+
Beast beast;
11+
12+
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
13+
mage = factory.createMage();
14+
warlord = factory.createWarlord();
15+
beast = factory.createBeast();
16+
System.out.println(mage);
17+
System.out.println(warlord);
18+
System.out.println(beast);
19+
20+
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
21+
mage = factory.createMage();
22+
warlord = factory.createWarlord();
23+
beast = factory.createBeast();
24+
System.out.println(mage);
25+
System.out.println(warlord);
26+
System.out.println(beast);
27+
}
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar;
2+
3+
public abstract class Beast extends Prototype {
4+
5+
@Override
6+
public abstract Beast clone() throws CloneNotSupportedException;
7+
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
public class ElfBeast extends Beast {
4+
5+
public ElfBeast() {
6+
}
7+
8+
public ElfBeast(ElfBeast beast) {
9+
}
10+
11+
@Override
12+
public Beast clone() throws CloneNotSupportedException {
13+
return new ElfBeast(this);
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Elven eagle";
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
public class ElfMage extends Mage {
4+
5+
public ElfMage() {
6+
}
7+
8+
public ElfMage(ElfMage mage) {
9+
}
10+
11+
@Override
12+
public Mage clone() throws CloneNotSupportedException {
13+
return new ElfMage(this);
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Elven mage";
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar;
2+
3+
public class ElfWarlord extends Warlord {
4+
5+
public ElfWarlord() {
6+
}
7+
8+
public ElfWarlord(ElfWarlord warlord) {
9+
}
10+
11+
@Override
12+
public Warlord clone() throws CloneNotSupportedException {
13+
return new ElfWarlord(this);
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Elven warlord";
19+
}
20+
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar;
2+
3+
public interface HeroFactory {
4+
5+
Mage createMage();
6+
7+
Warlord createWarlord();
8+
9+
Beast createBeast();
10+
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar;
2+
3+
public class HeroFactoryImpl implements HeroFactory {
4+
5+
private Mage mage;
6+
private Warlord warlord;
7+
private Beast beast;
8+
9+
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
10+
this.mage = mage;
11+
this.warlord = warlord;
12+
this.beast = beast;
13+
}
14+
15+
public Mage createMage() {
16+
try {
17+
return mage.clone();
18+
} catch (CloneNotSupportedException e) {
19+
return null;
20+
}
21+
}
22+
23+
public Warlord createWarlord() {
24+
try {
25+
return warlord.clone();
26+
} catch (CloneNotSupportedException e) {
27+
return null;
28+
}
29+
}
30+
31+
public Beast createBeast() {
32+
try {
33+
return beast.clone();
34+
} catch (CloneNotSupportedException e) {
35+
return null;
36+
}
37+
}
38+
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar;
2+
3+
public abstract class Mage extends Prototype {
4+
5+
@Override
6+
public abstract Mage clone() throws CloneNotSupportedException;
7+
8+
}

0 commit comments

Comments
 (0)
X Tutup