added prototype sample
This commit is contained in:
parent
d6bff9fd60
commit
4ae82e4302
1
pom.xml
1
pom.xml
@ -21,5 +21,6 @@
|
||||
<module>abstract-factory</module>
|
||||
<module>builder</module>
|
||||
<module>factory-method</module>
|
||||
<module>prototype</module>
|
||||
</modules>
|
||||
</project>
|
23
prototype/pom.xml
Normal file
23
prototype/pom.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0"?>
|
||||
<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"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>prototype</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>prototype</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
28
prototype/src/main/java/com/iluwatar/App.java
Normal file
28
prototype/src/main/java/com/iluwatar/App.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
HeroFactory factory;
|
||||
Mage mage;
|
||||
Warlord warlord;
|
||||
Beast beast;
|
||||
|
||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
|
||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
}
|
||||
}
|
8
prototype/src/main/java/com/iluwatar/Beast.java
Normal file
8
prototype/src/main/java/com/iluwatar/Beast.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public abstract class Beast extends Prototype {
|
||||
|
||||
@Override
|
||||
public abstract Beast clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
21
prototype/src/main/java/com/iluwatar/ElfBeast.java
Normal file
21
prototype/src/main/java/com/iluwatar/ElfBeast.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class ElfBeast extends Beast {
|
||||
|
||||
public ElfBeast() {
|
||||
}
|
||||
|
||||
public ElfBeast(ElfBeast beast) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Beast clone() throws CloneNotSupportedException {
|
||||
return new ElfBeast(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven eagle";
|
||||
}
|
||||
|
||||
}
|
21
prototype/src/main/java/com/iluwatar/ElfMage.java
Normal file
21
prototype/src/main/java/com/iluwatar/ElfMage.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class ElfMage extends Mage {
|
||||
|
||||
public ElfMage() {
|
||||
}
|
||||
|
||||
public ElfMage(ElfMage mage) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mage clone() throws CloneNotSupportedException {
|
||||
return new ElfMage(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven mage";
|
||||
}
|
||||
|
||||
}
|
21
prototype/src/main/java/com/iluwatar/ElfWarlord.java
Normal file
21
prototype/src/main/java/com/iluwatar/ElfWarlord.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class ElfWarlord extends Warlord {
|
||||
|
||||
public ElfWarlord() {
|
||||
}
|
||||
|
||||
public ElfWarlord(ElfWarlord warlord) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Warlord clone() throws CloneNotSupportedException {
|
||||
return new ElfWarlord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven warlord";
|
||||
}
|
||||
|
||||
}
|
11
prototype/src/main/java/com/iluwatar/HeroFactory.java
Normal file
11
prototype/src/main/java/com/iluwatar/HeroFactory.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public interface HeroFactory {
|
||||
|
||||
Mage createMage();
|
||||
|
||||
Warlord createWarlord();
|
||||
|
||||
Beast createBeast();
|
||||
|
||||
}
|
39
prototype/src/main/java/com/iluwatar/HeroFactoryImpl.java
Normal file
39
prototype/src/main/java/com/iluwatar/HeroFactoryImpl.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class HeroFactoryImpl implements HeroFactory {
|
||||
|
||||
private Mage mage;
|
||||
private Warlord warlord;
|
||||
private Beast beast;
|
||||
|
||||
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
|
||||
this.mage = mage;
|
||||
this.warlord = warlord;
|
||||
this.beast = beast;
|
||||
}
|
||||
|
||||
public Mage createMage() {
|
||||
try {
|
||||
return mage.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Warlord createWarlord() {
|
||||
try {
|
||||
return warlord.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Beast createBeast() {
|
||||
try {
|
||||
return beast.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
8
prototype/src/main/java/com/iluwatar/Mage.java
Normal file
8
prototype/src/main/java/com/iluwatar/Mage.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public abstract class Mage extends Prototype {
|
||||
|
||||
@Override
|
||||
public abstract Mage clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
21
prototype/src/main/java/com/iluwatar/OrcBeast.java
Normal file
21
prototype/src/main/java/com/iluwatar/OrcBeast.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class OrcBeast extends Beast {
|
||||
|
||||
public OrcBeast() {
|
||||
}
|
||||
|
||||
public OrcBeast(OrcBeast beast) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Beast clone() throws CloneNotSupportedException {
|
||||
return new OrcBeast(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish wolf";
|
||||
}
|
||||
|
||||
}
|
21
prototype/src/main/java/com/iluwatar/OrcMage.java
Normal file
21
prototype/src/main/java/com/iluwatar/OrcMage.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class OrcMage extends Mage {
|
||||
|
||||
public OrcMage() {
|
||||
}
|
||||
|
||||
public OrcMage(OrcMage mage) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mage clone() throws CloneNotSupportedException {
|
||||
return new OrcMage(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish mage";
|
||||
}
|
||||
|
||||
}
|
21
prototype/src/main/java/com/iluwatar/OrcWarlord.java
Normal file
21
prototype/src/main/java/com/iluwatar/OrcWarlord.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class OrcWarlord extends Warlord {
|
||||
|
||||
public OrcWarlord() {
|
||||
}
|
||||
|
||||
public OrcWarlord(OrcWarlord warlord) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Warlord clone() throws CloneNotSupportedException {
|
||||
return new OrcWarlord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish warlord";
|
||||
}
|
||||
|
||||
}
|
8
prototype/src/main/java/com/iluwatar/Prototype.java
Normal file
8
prototype/src/main/java/com/iluwatar/Prototype.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public abstract class Prototype implements Cloneable {
|
||||
|
||||
@Override
|
||||
public abstract Object clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
8
prototype/src/main/java/com/iluwatar/Warlord.java
Normal file
8
prototype/src/main/java/com/iluwatar/Warlord.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public abstract class Warlord extends Prototype {
|
||||
|
||||
@Override
|
||||
public abstract Warlord clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user