added factory method sample

This commit is contained in:
Ilkka Seppala 2014-08-10 10:07:38 +03:00
parent fbb9ebd46c
commit a9de898072
10 changed files with 128 additions and 0 deletions

23
factory-method/pom.xml Normal file
View 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>factory-method</artifactId>
<version>1.0-SNAPSHOT</version>
<name>factory-method</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>

View File

@ -0,0 +1,22 @@
package com.iluwatar;
public class App
{
public static void main( String[] args )
{
Blacksmith blacksmith;
Weapon weapon;
blacksmith = new OrcBlacksmith();
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
System.out.println(weapon);
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
System.out.println(weapon);
blacksmith = new ElfBlacksmith();
weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
System.out.println(weapon);
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
System.out.println(weapon);
}
}

View File

@ -0,0 +1,7 @@
package com.iluwatar;
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class ElfBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new ElfWeapon(weaponType);
}
}

View File

@ -0,0 +1,16 @@
package com.iluwatar;
public class ElfWeapon implements Weapon {
private WeaponType weaponType;
public ElfWeapon(WeaponType weaponType) {
this.weaponType = weaponType;
}
@Override
public String toString() {
return "Elven " + weaponType;
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class OrcBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new OrcWeapon(weaponType);
}
}

View File

@ -0,0 +1,16 @@
package com.iluwatar;
public class OrcWeapon implements Weapon {
private WeaponType weaponType;
public OrcWeapon(WeaponType weaponType) {
this.weaponType = weaponType;
}
@Override
public String toString() {
return "Orcish " + weaponType;
}
}

View File

@ -0,0 +1,5 @@
package com.iluwatar;
public interface Weapon {
}

View File

@ -0,0 +1,19 @@
package com.iluwatar;
public enum WeaponType {
SHORT_SWORD, SPEAR, AXE;
@Override
public String toString() {
String s = "";
switch(this) {
case SHORT_SWORD: s = "short sword"; break;
case SPEAR: s = "spear"; break;
case AXE: s = "axe"; break;
}
return s;
}
}

View File

@ -20,5 +20,7 @@
<modules>
<module>abstract-factory</module>
<module>builder</module>
<module>factory-method</module>
<module>shared</module>
</modules>
</project>