File tree Expand file tree Collapse file tree 10 files changed +128
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 10 files changed +128
-0
lines changed Original file line number Diff line number Diff line change 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 >factory-method</artifactId >
12+ <version >1.0-SNAPSHOT</version >
13+ <name >factory-method</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 >
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class App
4+ {
5+ public static void main ( String [] args )
6+ {
7+ Blacksmith blacksmith ;
8+ Weapon weapon ;
9+
10+ blacksmith = new OrcBlacksmith ();
11+ weapon = blacksmith .manufactureWeapon (WeaponType .SPEAR );
12+ System .out .println (weapon );
13+ weapon = blacksmith .manufactureWeapon (WeaponType .AXE );
14+ System .out .println (weapon );
15+
16+ blacksmith = new ElfBlacksmith ();
17+ weapon = blacksmith .manufactureWeapon (WeaponType .SHORT_SWORD );
18+ System .out .println (weapon );
19+ weapon = blacksmith .manufactureWeapon (WeaponType .SPEAR );
20+ System .out .println (weapon );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface Blacksmith {
4+
5+ Weapon manufactureWeapon (WeaponType weaponType );
6+
7+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class ElfBlacksmith implements Blacksmith {
4+
5+ public Weapon manufactureWeapon (WeaponType weaponType ) {
6+ return new ElfWeapon (weaponType );
7+ }
8+
9+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class ElfWeapon implements Weapon {
4+
5+ private WeaponType weaponType ;
6+
7+ public ElfWeapon (WeaponType weaponType ) {
8+ this .weaponType = weaponType ;
9+ }
10+
11+ @ Override
12+ public String toString () {
13+ return "Elven " + weaponType ;
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class OrcBlacksmith implements Blacksmith {
4+
5+ public Weapon manufactureWeapon (WeaponType weaponType ) {
6+ return new OrcWeapon (weaponType );
7+ }
8+
9+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public class OrcWeapon implements Weapon {
4+
5+ private WeaponType weaponType ;
6+
7+ public OrcWeapon (WeaponType weaponType ) {
8+ this .weaponType = weaponType ;
9+ }
10+
11+ @ Override
12+ public String toString () {
13+ return "Orcish " + weaponType ;
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public interface Weapon {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package com .iluwatar ;
2+
3+ public enum WeaponType {
4+
5+ SHORT_SWORD , SPEAR , AXE ;
6+
7+ @ Override
8+ public String toString () {
9+ String s = "" ;
10+ switch (this ) {
11+ case SHORT_SWORD : s = "short sword" ; break ;
12+ case SPEAR : s = "spear" ; break ;
13+ case AXE : s = "axe" ; break ;
14+ }
15+ return s ;
16+ }
17+
18+
19+ }
Original file line number Diff line number Diff line change 2020 <modules >
2121 <module >abstract-factory</module >
2222 <module >builder</module >
23+ <module >factory-method</module >
24+ <module >shared</module >
2325 </modules >
2426</project >
You can’t perform that action at this time.
0 commit comments