Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vdlald committed Sep 4, 2020
1 parent e89042a commit bd48d6c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 79 deletions.
Expand Up @@ -23,12 +23,8 @@

package com.iluwatar.abstractfactory;

import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

@Setter
@Getter
private King king;

@Setter
@Getter
private Castle castle;

@Setter
@Getter
private Army army;

/**
* Creates kingdom.
*/
public void createKingdom(final KingdomFactory factory) {
setKing(factory.createKing());
setCastle(factory.createCastle());
setArmy(factory.createArmy());
}

/**
* The factory of kingdom factories.
*/
public static class FactoryMaker {

/**
* Enumeration for the different types of Kingdoms.
*/
public enum KingdomType {
ELF, ORC
}

/**
* The factory method to create KingdomFactory concrete objects.
*/
public static KingdomFactory makeFactory(KingdomType type) {
switch (type) {
case ELF:
return new ElfKingdomFactory();
case ORC:
return new OrcKingdomFactory();
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
}
}
private final Kingdom kingdom = new Kingdom();

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {

var app = new App();
}

@Override
public void run() {
log.info("Elf Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
log.info(app.getArmy().getDescription());
log.info(app.getCastle().getDescription());
log.info(app.getKing().getDescription());
createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF));
log.info(kingdom.getArmy().getDescription());
log.info(kingdom.getCastle().getDescription());
log.info(kingdom.getKing().getDescription());

log.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
log.info(app.getArmy().getDescription());
log.info(app.getCastle().getDescription());
log.info(app.getKing().getDescription());
createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC));
log.info(kingdom.getArmy().getDescription());
log.info(kingdom.getCastle().getDescription());
log.info(kingdom.getKing().getDescription());
}

/**
* Creates kingdom.
*/
public void createKingdom(final KingdomFactory factory) {
kingdom.setKing(factory.createKing());
kingdom.setCastle(factory.createCastle());
kingdom.setArmy(factory.createArmy());
}
}
@@ -0,0 +1,38 @@
package com.iluwatar.abstractfactory;

import lombok.Data;

@Data
public class Kingdom {

private King king;
private Castle castle;
private Army army;

/**
* The factory of kingdom factories.
*/
public static class FactoryMaker {

/**
* Enumeration for the different types of Kingdoms.
*/
public enum KingdomType {
ELF, ORC
}

/**
* The factory method to create KingdomFactory concrete objects.
*/
public static KingdomFactory makeFactory(KingdomType type) {
switch (type) {
case ELF:
return new ElfKingdomFactory();
case ORC:
return new OrcKingdomFactory();
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
}
}
}
Expand Up @@ -23,14 +23,13 @@

package com.iluwatar.abstractfactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.iluwatar.abstractfactory.App.FactoryMaker;
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
import lombok.val;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test for abstract factory.
*/
Expand All @@ -42,55 +41,63 @@ public class AbstractFactoryTest {

@BeforeEach
public void setUp() {
elfFactory = FactoryMaker.makeFactory(KingdomType.ELF);
orcFactory = FactoryMaker.makeFactory(KingdomType.ORC);
elfFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF);
orcFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC);
}

@Test
public void king() {
app.createKingdom(elfFactory);
final var elfKing = app.getKing();
val kingdom = app.getKingdom();

val elfKing = kingdom.getKing();
assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());

app.createKingdom(orcFactory);
final var orcKing = app.getKing();
val orcKing = kingdom.getKing();
assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
}

@Test
public void castle() {
app.createKingdom(elfFactory);
final var elfCastle = app.getCastle();
val kingdom = app.getKingdom();

val elfCastle = kingdom.getCastle();
assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());

app.createKingdom(orcFactory);
final var orcCastle = app.getCastle();
val orcCastle = kingdom.getCastle();
assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
}

@Test
public void army() {
app.createKingdom(elfFactory);
final var elfArmy = app.getArmy();
val kingdom = app.getKingdom();

val elfArmy = kingdom.getArmy();
assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());

app.createKingdom(orcFactory);
final var orcArmy = app.getArmy();
val orcArmy = kingdom.getArmy();
assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
}

@Test
public void createElfKingdom() {
app.createKingdom(elfFactory);
final var king = app.getKing();
final var castle = app.getCastle();
final var army = app.getArmy();
val kingdom = app.getKingdom();

val king = kingdom.getKing();
val castle = kingdom.getCastle();
val army = kingdom.getArmy();
assertTrue(king instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof ElfCastle);
Expand All @@ -102,9 +109,11 @@ public void createElfKingdom() {
@Test
public void createOrcKingdom() {
app.createKingdom(orcFactory);
final var king = app.getKing();
final var castle = app.getCastle();
final var army = app.getArmy();
val kingdom = app.getKingdom();

val king = kingdom.getKing();
val castle = kingdom.getCastle();
val army = kingdom.getArmy();
assertTrue(king instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof OrcCastle);
Expand Down

0 comments on commit bd48d6c

Please sign in to comment.