Skip to content

Commit 070ff68

Browse files
authored
Java Design_Pattern
1 parent 6b8caf3 commit 070ff68

18 files changed

Lines changed: 437 additions & 0 deletions

File tree

abstract-factory/etc/diagram1.png

56.7 KB
Loading

abstract-factory/etc/diagram2.png

25.7 KB
Loading

abstract-factory/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
3+
<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"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.khan</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.20.0-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>abstract-factory</artifactId>
12+
<dependencies>
13+
<dependency>
14+
<groupId>org.junit.jupiter</groupId>
15+
<artifactId>junit-jupiter-api</artifactId>
16+
<scope>test</scope>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter-engine</artifactId>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
</project>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import com.khan.abstractfactory.App.FactoryMaker.KingdomType;
8+
9+
public class App {
10+
11+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
12+
13+
private King king;
14+
private Castle castle;
15+
private Army army;
16+
17+
/**
18+
* Creates kingdom
19+
*/
20+
public void createKingdom(final KingdomFactory factory) {
21+
setKing(factory.createKing());
22+
setCastle(factory.createCastle());
23+
setArmy(factory.createArmy());
24+
}
25+
26+
King getKing(final KingdomFactory factory) {
27+
return factory.createKing();
28+
}
29+
30+
public King getKing() {
31+
return king;
32+
}
33+
34+
private void setKing(final King king) {
35+
this.king = king;
36+
}
37+
38+
Castle getCastle(final KingdomFactory factory) {
39+
return factory.createCastle();
40+
}
41+
42+
public Castle getCastle() {
43+
return castle;
44+
}
45+
46+
private void setCastle(final Castle castle) {
47+
this.castle = castle;
48+
}
49+
50+
Army getArmy(final KingdomFactory factory) {
51+
return factory.createArmy();
52+
}
53+
54+
public Army getArmy() {
55+
return army;
56+
}
57+
58+
private void setArmy(final Army army) {
59+
this.army = army;
60+
}
61+
62+
/**
63+
* The factory of kingdom factories.
64+
*/
65+
public static class FactoryMaker {
66+
67+
/**
68+
* Enumeration for the different types of Kingdoms.
69+
*/
70+
public enum KingdomType {
71+
ELF, ORC
72+
}
73+
74+
/**
75+
* The factory method to create KingdomFactory concrete objects.
76+
*/
77+
public static KingdomFactory makeFactory(KingdomType type) {
78+
switch (type) {
79+
case ELF:
80+
return new ElfKingdomFactory();
81+
case ORC:
82+
return new OrcKingdomFactory();
83+
default:
84+
throw new IllegalArgumentException("KingdomType not supported.");
85+
}
86+
}
87+
}
88+
89+
/**
90+
* Program entry point.
91+
*
92+
* @param args
93+
* command line args
94+
*/
95+
public static void main(String[] args) {
96+
97+
App app = new App();
98+
99+
LOGGER.info("Elf Kingdom");
100+
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
101+
LOGGER.info(app.getArmy().getDescription());
102+
LOGGER.info(app.getCastle().getDescription());
103+
LOGGER.info(app.getKing().getDescription());
104+
105+
LOGGER.info("Orc Kingdom");
106+
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
107+
LOGGER.info(app.getArmy().getDescription());
108+
LOGGER.info(app.getCastle().getDescription());
109+
LOGGER.info(app.getKing().getDescription());
110+
}
111+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
/**
5+
*
6+
* Army interface
7+
*
8+
*/
9+
public interface Army {
10+
11+
String getDescription();
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
/**
5+
*
6+
* Castle interface
7+
*
8+
*/
9+
public interface Castle {
10+
11+
String getDescription();
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
/**
5+
*
6+
* ElfArmy
7+
*
8+
*/
9+
public class ElfArmy implements Army {
10+
11+
static final String DESCRIPTION = "This is the Elven Army!";
12+
13+
@Override
14+
public String getDescription() {
15+
return DESCRIPTION;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
/**
5+
*
6+
* ElfCastle
7+
*
8+
*/
9+
public class ElfCastle implements Castle {
10+
11+
static final String DESCRIPTION = "This is the Elven castle!";
12+
13+
@Override
14+
public String getDescription() {
15+
return DESCRIPTION;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
/**
5+
*
6+
* ElfKing
7+
*
8+
*/
9+
public class ElfKing implements King {
10+
11+
static final String DESCRIPTION = "This is the Elven king!";
12+
13+
@Override
14+
public String getDescription() {
15+
return DESCRIPTION;
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
package com.khan.abstractfactory;
3+
4+
/**
5+
*
6+
* ElfKingdomFactory concrete factory.
7+
*
8+
*/
9+
public class ElfKingdomFactory implements KingdomFactory {
10+
11+
public Castle createCastle() {
12+
return new ElfCastle();
13+
}
14+
15+
public King createKing() {
16+
return new ElfKing();
17+
}
18+
19+
public Army createArmy() {
20+
return new ElfArmy();
21+
}
22+
23+
}

0 commit comments

Comments
 (0)