Reformat Abstract Factory - Issue #224
This commit is contained in:
parent
44b7b94da6
commit
c0c21ebd91
@ -3,80 +3,78 @@ package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual
|
||||
* factories that have a common theme without specifying their concrete classes. In
|
||||
* normal usage, the client software creates a concrete implementation of the abstract
|
||||
* factory and then uses the generic interface of the factory to create the concrete
|
||||
* objects that are part of the theme. The client does not know (or care) which
|
||||
* concrete objects it gets from each of these internal factories, since it uses only
|
||||
* the generic interfaces of their products. This pattern separates the details of
|
||||
* implementation of a set of objects from their general usage and relies on object
|
||||
* composition, as object creation is implemented in methods exposed in the factory
|
||||
* interface.
|
||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
|
||||
* have a common theme without specifying their concrete classes. In normal usage, the client
|
||||
* software creates a concrete implementation of the abstract factory and then uses the generic
|
||||
* interface of the factory to create the concrete objects that are part of the theme. The client
|
||||
* does not know (or care) which concrete objects it gets from each of these internal factories,
|
||||
* since it uses only the generic interfaces of their products. This pattern separates the details
|
||||
* of implementation of a set of objects from their general usage and relies on object composition,
|
||||
* as object creation is implemented in methods exposed in the factory interface.
|
||||
* <p>
|
||||
* The essence of the Abstract Factory pattern is a factory interface
|
||||
* ({@link KingdomFactory}) and its implementations ({@link ElfKingdomFactory},
|
||||
* {@link OrcKingdomFactory}). The example uses both concrete implementations to
|
||||
* create a king, a castle and an army.
|
||||
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and
|
||||
* its implementations ({@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both
|
||||
* concrete implementations to create a king, a castle and an army.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private King king;
|
||||
private Castle castle;
|
||||
private Army army;
|
||||
private King king;
|
||||
private Castle castle;
|
||||
private Army army;
|
||||
|
||||
/**
|
||||
* Creates kingdom
|
||||
* @param factory
|
||||
*/
|
||||
public void createKingdom(final KingdomFactory factory) {
|
||||
setKing(factory.createKing());
|
||||
setCastle(factory.createCastle());
|
||||
setArmy(factory.createArmy());
|
||||
}
|
||||
/**
|
||||
* Creates kingdom
|
||||
*
|
||||
* @param factory
|
||||
*/
|
||||
public void createKingdom(final KingdomFactory factory) {
|
||||
setKing(factory.createKing());
|
||||
setCastle(factory.createCastle());
|
||||
setArmy(factory.createArmy());
|
||||
}
|
||||
|
||||
ElfKingdomFactory getElfKingdomFactory() {
|
||||
return new ElfKingdomFactory();
|
||||
}
|
||||
ElfKingdomFactory getElfKingdomFactory() {
|
||||
return new ElfKingdomFactory();
|
||||
}
|
||||
|
||||
OrcKingdomFactory getOrcKingdomFactory() {
|
||||
return new OrcKingdomFactory();
|
||||
}
|
||||
OrcKingdomFactory getOrcKingdomFactory() {
|
||||
return new OrcKingdomFactory();
|
||||
}
|
||||
|
||||
King getKing(final KingdomFactory factory) {
|
||||
return factory.createKing();
|
||||
}
|
||||
King getKing(final KingdomFactory factory) {
|
||||
return factory.createKing();
|
||||
}
|
||||
|
||||
Castle getCastle(final KingdomFactory factory) {
|
||||
return factory.createCastle();
|
||||
}
|
||||
Castle getCastle(final KingdomFactory factory) {
|
||||
return factory.createCastle();
|
||||
}
|
||||
|
||||
Army getArmy(final KingdomFactory factory) {
|
||||
return factory.createArmy();
|
||||
}
|
||||
Army getArmy(final KingdomFactory factory) {
|
||||
return factory.createArmy();
|
||||
}
|
||||
|
||||
public King getKing() {
|
||||
return king;
|
||||
}
|
||||
public King getKing() {
|
||||
return king;
|
||||
}
|
||||
|
||||
private void setKing(final King king) {
|
||||
this.king = king;
|
||||
}
|
||||
private void setKing(final King king) {
|
||||
this.king = king;
|
||||
}
|
||||
|
||||
public Castle getCastle() {
|
||||
return castle;
|
||||
}
|
||||
public Castle getCastle() {
|
||||
return castle;
|
||||
}
|
||||
|
||||
private void setCastle(final Castle castle) {
|
||||
this.castle = castle;
|
||||
}
|
||||
private void setCastle(final Castle castle) {
|
||||
this.castle = castle;
|
||||
}
|
||||
|
||||
public Army getArmy() {
|
||||
return army;
|
||||
}
|
||||
public Army getArmy() {
|
||||
return army;
|
||||
}
|
||||
|
||||
private void setArmy(final Army army) {
|
||||
this.army = army;
|
||||
}
|
||||
private void setArmy(final Army army) {
|
||||
this.army = army;
|
||||
}
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public interface Army {
|
||||
|
||||
String getDescription();
|
||||
String getDescription();
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public interface Castle {
|
||||
|
||||
String getDescription();
|
||||
String getDescription();
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class ElfArmy implements Army {
|
||||
|
||||
static final String DESCRIPTION = "This is the Elven Army!";
|
||||
static final String DESCRIPTION = "This is the Elven Army!";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class ElfCastle implements Castle {
|
||||
|
||||
static final String DESCRIPTION = "This is the Elven castle!";
|
||||
static final String DESCRIPTION = "This is the Elven castle!";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class ElfKing implements King {
|
||||
|
||||
static final String DESCRIPTION = "This is the Elven king!";
|
||||
static final String DESCRIPTION = "This is the Elven king!";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class ElfKingdomFactory implements KingdomFactory {
|
||||
|
||||
public Castle createCastle() {
|
||||
return new ElfCastle();
|
||||
}
|
||||
public Castle createCastle() {
|
||||
return new ElfCastle();
|
||||
}
|
||||
|
||||
public King createKing() {
|
||||
return new ElfKing();
|
||||
}
|
||||
public King createKing() {
|
||||
return new ElfKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new ElfArmy();
|
||||
}
|
||||
public Army createArmy() {
|
||||
return new ElfArmy();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public interface King {
|
||||
|
||||
String getDescription();
|
||||
String getDescription();
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public interface KingdomFactory {
|
||||
|
||||
Castle createCastle();
|
||||
Castle createCastle();
|
||||
|
||||
King createKing();
|
||||
King createKing();
|
||||
|
||||
Army createArmy();
|
||||
Army createArmy();
|
||||
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class OrcArmy implements Army {
|
||||
|
||||
static final String DESCRIPTION = "This is the Orc Army!";
|
||||
static final String DESCRIPTION = "This is the Orc Army!";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class OrcCastle implements Castle {
|
||||
|
||||
static final String DESCRIPTION = "This is the Orc castle!";
|
||||
static final String DESCRIPTION = "This is the Orc castle!";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class OrcKing implements King {
|
||||
|
||||
static final String DESCRIPTION = "This is the Orc king!";
|
||||
static final String DESCRIPTION = "This is the Orc king!";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,15 @@ package com.iluwatar.abstractfactory;
|
||||
*/
|
||||
public class OrcKingdomFactory implements KingdomFactory {
|
||||
|
||||
public Castle createCastle() {
|
||||
return new OrcCastle();
|
||||
}
|
||||
public Castle createCastle() {
|
||||
return new OrcCastle();
|
||||
}
|
||||
|
||||
public King createKing() {
|
||||
return new OrcKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new OrcArmy();
|
||||
}
|
||||
public King createKing() {
|
||||
return new OrcKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new OrcArmy();
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@ -7,71 +8,71 @@ import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
private App app = new App();
|
||||
private KingdomFactory elfFactory;
|
||||
private KingdomFactory orcFactory;
|
||||
private App app = new App();
|
||||
private KingdomFactory elfFactory;
|
||||
private KingdomFactory orcFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
elfFactory = app.getElfKingdomFactory();
|
||||
orcFactory = app.getOrcKingdomFactory();
|
||||
}
|
||||
@Before
|
||||
public void setUp() {
|
||||
elfFactory = app.getElfKingdomFactory();
|
||||
orcFactory = app.getOrcKingdomFactory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void king() {
|
||||
final King elfKing = app.getKing(elfFactory);
|
||||
assertTrue(elfKing instanceof ElfKing);
|
||||
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
||||
final King orcKing = app.getKing(orcFactory);
|
||||
assertTrue(orcKing instanceof OrcKing);
|
||||
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
||||
}
|
||||
@Test
|
||||
public void king() {
|
||||
final King elfKing = app.getKing(elfFactory);
|
||||
assertTrue(elfKing instanceof ElfKing);
|
||||
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
||||
final King orcKing = app.getKing(orcFactory);
|
||||
assertTrue(orcKing instanceof OrcKing);
|
||||
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void castle() {
|
||||
final Castle elfCastle = app.getCastle(elfFactory);
|
||||
assertTrue(elfCastle instanceof ElfCastle);
|
||||
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
||||
final Castle orcCastle = app.getCastle(orcFactory);
|
||||
assertTrue(orcCastle instanceof OrcCastle);
|
||||
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
||||
}
|
||||
@Test
|
||||
public void castle() {
|
||||
final Castle elfCastle = app.getCastle(elfFactory);
|
||||
assertTrue(elfCastle instanceof ElfCastle);
|
||||
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
||||
final Castle orcCastle = app.getCastle(orcFactory);
|
||||
assertTrue(orcCastle instanceof OrcCastle);
|
||||
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void army() {
|
||||
final Army elfArmy = app.getArmy(elfFactory);
|
||||
assertTrue(elfArmy instanceof ElfArmy);
|
||||
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
||||
final Army orcArmy = app.getArmy(orcFactory);
|
||||
assertTrue(orcArmy instanceof OrcArmy);
|
||||
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
||||
}
|
||||
@Test
|
||||
public void army() {
|
||||
final Army elfArmy = app.getArmy(elfFactory);
|
||||
assertTrue(elfArmy instanceof ElfArmy);
|
||||
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
||||
final Army orcArmy = app.getArmy(orcFactory);
|
||||
assertTrue(orcArmy instanceof OrcArmy);
|
||||
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createElfKingdom() {
|
||||
app.createKingdom(elfFactory);
|
||||
final King king = app.getKing();
|
||||
final Castle castle = app.getCastle();
|
||||
final Army army = app.getArmy();
|
||||
assertTrue(king instanceof ElfKing);
|
||||
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
||||
assertTrue(castle instanceof ElfCastle);
|
||||
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
|
||||
assertTrue(army instanceof ElfArmy);
|
||||
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
|
||||
}
|
||||
@Test
|
||||
public void createElfKingdom() {
|
||||
app.createKingdom(elfFactory);
|
||||
final King king = app.getKing();
|
||||
final Castle castle = app.getCastle();
|
||||
final Army army = app.getArmy();
|
||||
assertTrue(king instanceof ElfKing);
|
||||
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
||||
assertTrue(castle instanceof ElfCastle);
|
||||
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
|
||||
assertTrue(army instanceof ElfArmy);
|
||||
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createOrcKingdom() {
|
||||
app.createKingdom(orcFactory);
|
||||
final King king = app.getKing();
|
||||
final Castle castle = app.getCastle();
|
||||
final Army army = app.getArmy();
|
||||
assertTrue(king instanceof OrcKing);
|
||||
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
||||
assertTrue(castle instanceof OrcCastle);
|
||||
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
|
||||
assertTrue(army instanceof OrcArmy);
|
||||
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
|
||||
}
|
||||
@Test
|
||||
public void createOrcKingdom() {
|
||||
app.createKingdom(orcFactory);
|
||||
final King king = app.getKing();
|
||||
final Castle castle = app.getCastle();
|
||||
final Army army = app.getArmy();
|
||||
assertTrue(king instanceof OrcKing);
|
||||
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
||||
assertTrue(castle instanceof OrcCastle);
|
||||
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
|
||||
assertTrue(army instanceof OrcArmy);
|
||||
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user