Skip to content

Commit ce5700c

Browse files
committed
added abstract-factory sample
1 parent 54d5409 commit ce5700c

File tree

15 files changed

+184
-0
lines changed

15 files changed

+184
-0
lines changed

abstract-factory/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>abstract-factory</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>abstract-factory</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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
createKingdom(new ElfKingdomFactory());
8+
createKingdom(new OrcKingdomFactory());
9+
}
10+
11+
public static void createKingdom(KingdomFactory factory) {
12+
King king = factory.createKing();
13+
Castle castle = factory.createCastle();
14+
Army army = factory.createArmy();
15+
System.out.println("The kingdom was created.");
16+
System.out.println(king);
17+
System.out.println(castle);
18+
System.out.println(army);
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.iluwatar;
2+
3+
public interface Army {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.iluwatar;
2+
3+
public interface Castle {
4+
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public class ElfArmy implements Army {
4+
5+
@Override
6+
public String toString() {
7+
return "This is the Elven Army!";
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public class ElfCastle implements Castle {
4+
5+
@Override
6+
public String toString() {
7+
return "This is the Elven castle!";
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public class ElfKing implements King {
4+
5+
@Override
6+
public String toString() {
7+
return "This is the Elven king!";
8+
}
9+
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar;
2+
3+
public class ElfKingdomFactory implements KingdomFactory {
4+
5+
public Castle createCastle() {
6+
return new ElfCastle();
7+
}
8+
9+
public King createKing() {
10+
return new ElfKing();
11+
}
12+
13+
public Army createArmy() {
14+
return new ElfArmy();
15+
}
16+
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.iluwatar;
2+
3+
public interface King {
4+
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.iluwatar;
2+
3+
public interface KingdomFactory {
4+
5+
Castle createCastle();
6+
King createKing();
7+
Army createArmy();
8+
9+
}

0 commit comments

Comments
 (0)