add factory of factories
This commit is contained in:
parent
390c33e36e
commit
d37922bf82
@ -25,6 +25,8 @@ package com.iluwatar.abstractfactory;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme
|
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme
|
||||||
@ -93,6 +95,31 @@ public class App {
|
|||||||
this.army = army;
|
this.army = army;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The factory of kingdom factories.
|
||||||
|
*/
|
||||||
|
public static class FactoryMaker {
|
||||||
|
|
||||||
|
private FactoryMaker() {}
|
||||||
|
|
||||||
|
public enum KingdomType {
|
||||||
|
ELF,
|
||||||
|
ORC
|
||||||
|
}
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
*
|
*
|
||||||
@ -104,17 +131,15 @@ public class App {
|
|||||||
App app = new App();
|
App app = new App();
|
||||||
|
|
||||||
LOGGER.info("Elf Kingdom");
|
LOGGER.info("Elf Kingdom");
|
||||||
app.createKingdom(new ElfKingdomFactory());
|
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
||||||
LOGGER.info(app.getArmy().getDescription());
|
LOGGER.info(app.getArmy().getDescription());
|
||||||
LOGGER.info(app.getCastle().getDescription());
|
LOGGER.info(app.getCastle().getDescription());
|
||||||
LOGGER.info(app.getKing().getDescription());
|
LOGGER.info(app.getKing().getDescription());
|
||||||
|
|
||||||
LOGGER.info("Orc Kingdom");
|
LOGGER.info("Orc Kingdom");
|
||||||
app.createKingdom(new OrcKingdomFactory());
|
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
|
||||||
LOGGER.info(app.getArmy().getDescription());
|
LOGGER.info(app.getArmy().getDescription());
|
||||||
LOGGER.info(app.getCastle().getDescription());
|
LOGGER.info(app.getCastle().getDescription());
|
||||||
LOGGER.info(app.getKing().getDescription());
|
LOGGER.info(app.getKing().getDescription());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -28,6 +28,9 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractfactory.App.FactoryMaker;
|
||||||
|
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for abstract factory
|
* Test for abstract factory
|
||||||
*/
|
*/
|
||||||
@ -39,8 +42,8 @@ public class AbstractFactoryTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
elfFactory = new ElfKingdomFactory();
|
elfFactory = FactoryMaker.makeFactory(KingdomType.ELF);
|
||||||
orcFactory = new OrcKingdomFactory();
|
orcFactory = FactoryMaker.makeFactory(KingdomType.ORC);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user