Skip to content

Commit bd48d6c

Browse files
committed
refactor
1 parent e89042a commit bd48d6c

3 files changed

Lines changed: 88 additions & 79 deletions

File tree

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

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@
2323

2424
package com.iluwatar.abstractfactory;
2525

26-
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
2726
import lombok.Getter;
28-
import lombok.Setter;
2927
import lombok.extern.slf4j.Slf4j;
30-
import org.slf4j.Logger;
31-
import org.slf4j.LoggerFactory;
3228

3329
/**
3430
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
@@ -45,75 +41,41 @@
4541
* both concrete implementations to create a king, a castle and an army.
4642
*/
4743
@Slf4j
48-
public class App {
44+
public class App implements Runnable {
4945

50-
@Setter
5146
@Getter
52-
private King king;
53-
54-
@Setter
55-
@Getter
56-
private Castle castle;
57-
58-
@Setter
59-
@Getter
60-
private Army army;
61-
62-
/**
63-
* Creates kingdom.
64-
*/
65-
public void createKingdom(final KingdomFactory factory) {
66-
setKing(factory.createKing());
67-
setCastle(factory.createCastle());
68-
setArmy(factory.createArmy());
69-
}
70-
71-
/**
72-
* The factory of kingdom factories.
73-
*/
74-
public static class FactoryMaker {
75-
76-
/**
77-
* Enumeration for the different types of Kingdoms.
78-
*/
79-
public enum KingdomType {
80-
ELF, ORC
81-
}
82-
83-
/**
84-
* The factory method to create KingdomFactory concrete objects.
85-
*/
86-
public static KingdomFactory makeFactory(KingdomType type) {
87-
switch (type) {
88-
case ELF:
89-
return new ElfKingdomFactory();
90-
case ORC:
91-
return new OrcKingdomFactory();
92-
default:
93-
throw new IllegalArgumentException("KingdomType not supported.");
94-
}
95-
}
96-
}
47+
private final Kingdom kingdom = new Kingdom();
9748

9849
/**
9950
* Program entry point.
10051
*
10152
* @param args command line args
10253
*/
10354
public static void main(String[] args) {
104-
10555
var app = new App();
56+
}
10657

58+
@Override
59+
public void run() {
10760
log.info("Elf Kingdom");
108-
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
109-
log.info(app.getArmy().getDescription());
110-
log.info(app.getCastle().getDescription());
111-
log.info(app.getKing().getDescription());
61+
createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF));
62+
log.info(kingdom.getArmy().getDescription());
63+
log.info(kingdom.getCastle().getDescription());
64+
log.info(kingdom.getKing().getDescription());
11265

11366
log.info("Orc Kingdom");
114-
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
115-
log.info(app.getArmy().getDescription());
116-
log.info(app.getCastle().getDescription());
117-
log.info(app.getKing().getDescription());
67+
createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC));
68+
log.info(kingdom.getArmy().getDescription());
69+
log.info(kingdom.getCastle().getDescription());
70+
log.info(kingdom.getKing().getDescription());
71+
}
72+
73+
/**
74+
* Creates kingdom.
75+
*/
76+
public void createKingdom(final KingdomFactory factory) {
77+
kingdom.setKing(factory.createKing());
78+
kingdom.setCastle(factory.createCastle());
79+
kingdom.setArmy(factory.createArmy());
11880
}
11981
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.abstractfactory;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Kingdom {
7+
8+
private King king;
9+
private Castle castle;
10+
private Army army;
11+
12+
/**
13+
* The factory of kingdom factories.
14+
*/
15+
public static class FactoryMaker {
16+
17+
/**
18+
* Enumeration for the different types of Kingdoms.
19+
*/
20+
public enum KingdomType {
21+
ELF, ORC
22+
}
23+
24+
/**
25+
* The factory method to create KingdomFactory concrete objects.
26+
*/
27+
public static KingdomFactory makeFactory(KingdomType type) {
28+
switch (type) {
29+
case ELF:
30+
return new ElfKingdomFactory();
31+
case ORC:
32+
return new OrcKingdomFactory();
33+
default:
34+
throw new IllegalArgumentException("KingdomType not supported.");
35+
}
36+
}
37+
}
38+
}

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323

2424
package com.iluwatar.abstractfactory;
2525

26-
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
import static org.junit.jupiter.api.Assertions.assertTrue;
28-
29-
import com.iluwatar.abstractfactory.App.FactoryMaker;
30-
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
26+
import lombok.val;
3127
import org.junit.jupiter.api.BeforeEach;
3228
import org.junit.jupiter.api.Test;
3329

30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
3433
/**
3534
* Test for abstract factory.
3635
*/
@@ -42,55 +41,63 @@ public class AbstractFactoryTest {
4241

4342
@BeforeEach
4443
public void setUp() {
45-
elfFactory = FactoryMaker.makeFactory(KingdomType.ELF);
46-
orcFactory = FactoryMaker.makeFactory(KingdomType.ORC);
44+
elfFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF);
45+
orcFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC);
4746
}
4847

4948
@Test
5049
public void king() {
5150
app.createKingdom(elfFactory);
52-
final var elfKing = app.getKing();
51+
val kingdom = app.getKingdom();
52+
53+
val elfKing = kingdom.getKing();
5354
assertTrue(elfKing instanceof ElfKing);
5455
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
5556

5657
app.createKingdom(orcFactory);
57-
final var orcKing = app.getKing();
58+
val orcKing = kingdom.getKing();
5859
assertTrue(orcKing instanceof OrcKing);
5960
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
6061
}
6162

6263
@Test
6364
public void castle() {
6465
app.createKingdom(elfFactory);
65-
final var elfCastle = app.getCastle();
66+
val kingdom = app.getKingdom();
67+
68+
val elfCastle = kingdom.getCastle();
6669
assertTrue(elfCastle instanceof ElfCastle);
6770
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
6871

6972
app.createKingdom(orcFactory);
70-
final var orcCastle = app.getCastle();
73+
val orcCastle = kingdom.getCastle();
7174
assertTrue(orcCastle instanceof OrcCastle);
7275
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
7376
}
7477

7578
@Test
7679
public void army() {
7780
app.createKingdom(elfFactory);
78-
final var elfArmy = app.getArmy();
81+
val kingdom = app.getKingdom();
82+
83+
val elfArmy = kingdom.getArmy();
7984
assertTrue(elfArmy instanceof ElfArmy);
8085
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
8186

8287
app.createKingdom(orcFactory);
83-
final var orcArmy = app.getArmy();
88+
val orcArmy = kingdom.getArmy();
8489
assertTrue(orcArmy instanceof OrcArmy);
8590
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
8691
}
8792

8893
@Test
8994
public void createElfKingdom() {
9095
app.createKingdom(elfFactory);
91-
final var king = app.getKing();
92-
final var castle = app.getCastle();
93-
final var army = app.getArmy();
96+
val kingdom = app.getKingdom();
97+
98+
val king = kingdom.getKing();
99+
val castle = kingdom.getCastle();
100+
val army = kingdom.getArmy();
94101
assertTrue(king instanceof ElfKing);
95102
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
96103
assertTrue(castle instanceof ElfCastle);
@@ -102,9 +109,11 @@ public void createElfKingdom() {
102109
@Test
103110
public void createOrcKingdom() {
104111
app.createKingdom(orcFactory);
105-
final var king = app.getKing();
106-
final var castle = app.getCastle();
107-
final var army = app.getArmy();
112+
val kingdom = app.getKingdom();
113+
114+
val king = kingdom.getKing();
115+
val castle = kingdom.getCastle();
116+
val army = kingdom.getArmy();
108117
assertTrue(king instanceof OrcKing);
109118
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
110119
assertTrue(castle instanceof OrcCastle);

0 commit comments

Comments
 (0)