Skip to content

Commit 5607a49

Browse files
authored
Merge pull request iluwatar#1517 from vdlald/847
847
2 parents ef326ee + bab48ef commit 5607a49

File tree

3 files changed

+124
-114
lines changed

3 files changed

+124
-114
lines changed

abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java

Lines changed: 30 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
package com.iluwatar.abstractfactory;
2525

26-
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
2726
import org.slf4j.Logger;
2827
import org.slf4j.LoggerFactory;
2928

@@ -41,84 +40,14 @@
4140
* and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses
4241
* both concrete implementations to create a king, a castle and an army.
4342
*/
44-
public class App {
43+
public class App implements Runnable {
4544

46-
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
45+
private static Logger log = LoggerFactory.getLogger(App.class);
4746

48-
private King king;
49-
private Castle castle;
50-
private Army army;
47+
private final Kingdom kingdom = new Kingdom();
5148

52-
/**
53-
* Creates kingdom.
54-
*/
55-
public void createKingdom(final KingdomFactory factory) {
56-
setKing(factory.createKing());
57-
setCastle(factory.createCastle());
58-
setArmy(factory.createArmy());
59-
}
60-
61-
King getKing(final KingdomFactory factory) {
62-
return factory.createKing();
63-
}
64-
65-
public King getKing() {
66-
return king;
67-
}
68-
69-
private void setKing(final King king) {
70-
this.king = king;
71-
}
72-
73-
Castle getCastle(final KingdomFactory factory) {
74-
return factory.createCastle();
75-
}
76-
77-
public Castle getCastle() {
78-
return castle;
79-
}
80-
81-
private void setCastle(final Castle castle) {
82-
this.castle = castle;
83-
}
84-
85-
Army getArmy(final KingdomFactory factory) {
86-
return factory.createArmy();
87-
}
88-
89-
public Army getArmy() {
90-
return army;
91-
}
92-
93-
private void setArmy(final Army army) {
94-
this.army = army;
95-
}
96-
97-
/**
98-
* The factory of kingdom factories.
99-
*/
100-
public static class FactoryMaker {
101-
102-
/**
103-
* Enumeration for the different types of Kingdoms.
104-
*/
105-
public enum KingdomType {
106-
ELF, ORC
107-
}
108-
109-
/**
110-
* The factory method to create KingdomFactory concrete objects.
111-
*/
112-
public static KingdomFactory makeFactory(KingdomType type) {
113-
switch (type) {
114-
case ELF:
115-
return new ElfKingdomFactory();
116-
case ORC:
117-
return new OrcKingdomFactory();
118-
default:
119-
throw new IllegalArgumentException("KingdomType not supported.");
120-
}
121-
}
49+
public Kingdom getKingdom() {
50+
return kingdom;
12251
}
12352

12453
/**
@@ -127,19 +56,33 @@ public static KingdomFactory makeFactory(KingdomType type) {
12756
* @param args command line args
12857
*/
12958
public static void main(String[] args) {
130-
13159
var app = new App();
60+
app.run();
61+
}
13262

133-
LOGGER.info("Elf Kingdom");
134-
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
135-
LOGGER.info(app.getArmy().getDescription());
136-
LOGGER.info(app.getCastle().getDescription());
137-
LOGGER.info(app.getKing().getDescription());
63+
@Override
64+
public void run() {
65+
log.info("Elf Kingdom");
66+
createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
67+
log.info(kingdom.getArmy().getDescription());
68+
log.info(kingdom.getCastle().getDescription());
69+
log.info(kingdom.getKing().getDescription());
70+
71+
log.info("Orc Kingdom");
72+
createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
73+
log.info(kingdom.getArmy().getDescription());
74+
log.info(kingdom.getCastle().getDescription());
75+
log.info(kingdom.getKing().getDescription());
76+
}
13877

139-
LOGGER.info("Orc Kingdom");
140-
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
141-
LOGGER.info(app.getArmy().getDescription());
142-
LOGGER.info(app.getCastle().getDescription());
143-
LOGGER.info(app.getKing().getDescription());
78+
/**
79+
* Creates kingdom.
80+
* @param kingdomType type of Kingdom
81+
*/
82+
public void createKingdom(final Kingdom.FactoryMaker.KingdomType kingdomType) {
83+
final KingdomFactory kingdomFactory = Kingdom.FactoryMaker.makeFactory(kingdomType);
84+
kingdom.setKing(kingdomFactory.createKing());
85+
kingdom.setCastle(kingdomFactory.createCastle());
86+
kingdom.setArmy(kingdomFactory.createArmy());
14487
}
14588
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.iluwatar.abstractfactory;
2+
3+
public class Kingdom {
4+
5+
private King king;
6+
private Castle castle;
7+
private Army army;
8+
9+
public King getKing() {
10+
return king;
11+
}
12+
13+
public Castle getCastle() {
14+
return castle;
15+
}
16+
17+
public Army getArmy() {
18+
return army;
19+
}
20+
21+
public void setKing(King king) {
22+
this.king = king;
23+
}
24+
25+
public void setCastle(Castle castle) {
26+
this.castle = castle;
27+
}
28+
29+
public void setArmy(Army army) {
30+
this.army = army;
31+
}
32+
33+
/**
34+
* The factory of kingdom factories.
35+
*/
36+
public static class FactoryMaker {
37+
38+
/**
39+
* Enumeration for the different types of Kingdoms.
40+
*/
41+
public enum KingdomType {
42+
ELF, ORC
43+
}
44+
45+
/**
46+
* The factory method to create KingdomFactory concrete objects.
47+
*/
48+
public static KingdomFactory makeFactory(KingdomType type) {
49+
switch (type) {
50+
case ELF:
51+
return new ElfKingdomFactory();
52+
case ORC:
53+
return new OrcKingdomFactory();
54+
default:
55+
throw new IllegalArgumentException("KingdomType not supported.");
56+
}
57+
}
58+
}
59+
}

abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,71 @@
2323

2424
package com.iluwatar.abstractfactory;
2525

26+
import org.junit.jupiter.api.Test;
27+
2628
import static org.junit.jupiter.api.Assertions.assertEquals;
2729
import static org.junit.jupiter.api.Assertions.assertTrue;
2830

29-
import com.iluwatar.abstractfactory.App.FactoryMaker;
30-
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
31-
import org.junit.jupiter.api.BeforeEach;
32-
import org.junit.jupiter.api.Test;
33-
3431
/**
3532
* Test for abstract factory.
3633
*/
3734
public class AbstractFactoryTest {
3835

3936
private final App app = new App();
40-
private KingdomFactory elfFactory;
41-
private KingdomFactory orcFactory;
42-
43-
@BeforeEach
44-
public void setUp() {
45-
elfFactory = FactoryMaker.makeFactory(KingdomType.ELF);
46-
orcFactory = FactoryMaker.makeFactory(KingdomType.ORC);
47-
}
4837

4938
@Test
5039
public void king() {
51-
final var elfKing = app.getKing(elfFactory);
40+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
41+
final var kingdom = app.getKingdom();
42+
43+
final var elfKing = kingdom.getKing();
5244
assertTrue(elfKing instanceof ElfKing);
5345
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
54-
final var orcKing = app.getKing(orcFactory);
46+
47+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
48+
final var orcKing = kingdom.getKing();
5549
assertTrue(orcKing instanceof OrcKing);
5650
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
5751
}
5852

5953
@Test
6054
public void castle() {
61-
final var elfCastle = app.getCastle(elfFactory);
55+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
56+
final var kingdom = app.getKingdom();
57+
58+
final var elfCastle = kingdom.getCastle();
6259
assertTrue(elfCastle instanceof ElfCastle);
6360
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
64-
final var orcCastle = app.getCastle(orcFactory);
61+
62+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
63+
final var orcCastle = kingdom.getCastle();
6564
assertTrue(orcCastle instanceof OrcCastle);
6665
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
6766
}
6867

6968
@Test
7069
public void army() {
71-
final var elfArmy = app.getArmy(elfFactory);
70+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
71+
final var kingdom = app.getKingdom();
72+
73+
final var elfArmy = kingdom.getArmy();
7274
assertTrue(elfArmy instanceof ElfArmy);
7375
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
74-
final var orcArmy = app.getArmy(orcFactory);
76+
77+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
78+
final var orcArmy = kingdom.getArmy();
7579
assertTrue(orcArmy instanceof OrcArmy);
7680
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
7781
}
7882

7983
@Test
8084
public void createElfKingdom() {
81-
app.createKingdom(elfFactory);
82-
final var king = app.getKing();
83-
final var castle = app.getCastle();
84-
final var army = app.getArmy();
85+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
86+
final var kingdom = app.getKingdom();
87+
88+
final var king = kingdom.getKing();
89+
final var castle = kingdom.getCastle();
90+
final var army = kingdom.getArmy();
8591
assertTrue(king instanceof ElfKing);
8692
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
8793
assertTrue(castle instanceof ElfCastle);
@@ -92,10 +98,12 @@ public void createElfKingdom() {
9298

9399
@Test
94100
public void createOrcKingdom() {
95-
app.createKingdom(orcFactory);
96-
final var king = app.getKing();
97-
final var castle = app.getCastle();
98-
final var army = app.getArmy();
101+
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
102+
final var kingdom = app.getKingdom();
103+
104+
final var king = kingdom.getKing();
105+
final var castle = kingdom.getCastle();
106+
final var army = kingdom.getArmy();
99107
assertTrue(king instanceof OrcKing);
100108
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
101109
assertTrue(castle instanceof OrcCastle);

0 commit comments

Comments
 (0)