Reformat Abstract Factory - Issue #224

This commit is contained in:
Ankur Kaushal 2015-11-01 17:18:39 -05:00
parent 44b7b94da6
commit c0c21ebd91
14 changed files with 192 additions and 194 deletions

View File

@ -3,80 +3,78 @@ package com.iluwatar.abstractfactory;
/** /**
* *
* The Abstract Factory pattern provides a way to encapsulate a group of individual * The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
* factories that have a common theme without specifying their concrete classes. In * have a common theme without specifying their concrete classes. In normal usage, the client
* normal usage, the client software creates a concrete implementation of the abstract * software creates a concrete implementation of the abstract factory and then uses the generic
* factory and then uses the generic interface of the factory to create the concrete * interface of the factory to create the concrete objects that are part of the theme. The client
* objects that are part of the theme. The client does not know (or care) which * does not know (or care) which concrete objects it gets from each of these internal factories,
* concrete objects it gets from each of these internal factories, since it uses only * since it uses only the generic interfaces of their products. This pattern separates the details
* the generic interfaces of their products. This pattern separates the details of * of implementation of a set of objects from their general usage and relies on object composition,
* implementation of a set of objects from their general usage and relies on object * as object creation is implemented in methods exposed in the factory interface.
* composition, as object creation is implemented in methods exposed in the factory
* interface.
* <p> * <p>
* The essence of the Abstract Factory pattern is a factory interface * The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and
* ({@link KingdomFactory}) and its implementations ({@link ElfKingdomFactory}, * its implementations ({@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both
* {@link OrcKingdomFactory}). The example uses both concrete implementations to * concrete implementations to create a king, a castle and an army.
* create a king, a castle and an army.
* *
*/ */
public class App { public class App {
private King king; private King king;
private Castle castle; private Castle castle;
private Army army; private Army army;
/** /**
* Creates kingdom * Creates kingdom
* @param factory *
*/ * @param factory
public void createKingdom(final KingdomFactory factory) { */
setKing(factory.createKing()); public void createKingdom(final KingdomFactory factory) {
setCastle(factory.createCastle()); setKing(factory.createKing());
setArmy(factory.createArmy()); 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;
}
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
*/ */
public interface Army { public interface Army {
String getDescription(); String getDescription();
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
*/ */
public interface Castle { public interface Castle {
String getDescription(); String getDescription();
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfArmy implements Army { public class ElfArmy implements Army {
static final String DESCRIPTION = "This is the Elven Army!"; static final String DESCRIPTION = "This is the Elven Army!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfCastle implements Castle { public class ElfCastle implements Castle {
static final String DESCRIPTION = "This is the Elven castle!"; static final String DESCRIPTION = "This is the Elven castle!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfKing implements King { public class ElfKing implements King {
static final String DESCRIPTION = "This is the Elven king!"; static final String DESCRIPTION = "This is the Elven king!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,16 +7,16 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfKingdomFactory implements KingdomFactory { public class ElfKingdomFactory implements KingdomFactory {
public Castle createCastle() { public Castle createCastle() {
return new ElfCastle(); return new ElfCastle();
} }
public King createKing() { public King createKing() {
return new ElfKing(); return new ElfKing();
} }
public Army createArmy() { public Army createArmy() {
return new ElfArmy(); return new ElfArmy();
} }
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
*/ */
public interface King { public interface King {
String getDescription(); String getDescription();
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public interface KingdomFactory { public interface KingdomFactory {
Castle createCastle(); Castle createCastle();
King createKing(); King createKing();
Army createArmy(); Army createArmy();
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcArmy implements Army { public class OrcArmy implements Army {
static final String DESCRIPTION = "This is the Orc Army!"; static final String DESCRIPTION = "This is the Orc Army!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcCastle implements Castle { public class OrcCastle implements Castle {
static final String DESCRIPTION = "This is the Orc castle!"; static final String DESCRIPTION = "This is the Orc castle!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcKing implements King { public class OrcKing implements King {
static final String DESCRIPTION = "This is the Orc king!"; static final String DESCRIPTION = "This is the Orc king!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,16 +7,15 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcKingdomFactory implements KingdomFactory { public class OrcKingdomFactory implements KingdomFactory {
public Castle createCastle() { public Castle createCastle() {
return new OrcCastle(); return new OrcCastle();
} }
public King createKing() { public King createKing() {
return new OrcKing(); return new OrcKing();
} }
public Army createArmy() {
return new OrcArmy();
}
public Army createArmy() {
return new OrcArmy();
}
} }

View File

@ -1,4 +1,5 @@
package com.iluwatar.abstractfactory; package com.iluwatar.abstractfactory;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -7,71 +8,71 @@ import org.junit.Test;
public class AppTest { public class AppTest {
private App app = new App(); private App app = new App();
private KingdomFactory elfFactory; private KingdomFactory elfFactory;
private KingdomFactory orcFactory; private KingdomFactory orcFactory;
@Before @Before
public void setUp() { public void setUp() {
elfFactory = app.getElfKingdomFactory(); elfFactory = app.getElfKingdomFactory();
orcFactory = app.getOrcKingdomFactory(); orcFactory = app.getOrcKingdomFactory();
} }
@Test @Test
public void king() { public void king() {
final King elfKing = app.getKing(elfFactory); final King elfKing = app.getKing(elfFactory);
assertTrue(elfKing instanceof ElfKing); assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
final King orcKing = app.getKing(orcFactory); final King orcKing = app.getKing(orcFactory);
assertTrue(orcKing instanceof OrcKing); assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
} }
@Test @Test
public void castle() { public void castle() {
final Castle elfCastle = app.getCastle(elfFactory); final Castle elfCastle = app.getCastle(elfFactory);
assertTrue(elfCastle instanceof ElfCastle); assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
final Castle orcCastle = app.getCastle(orcFactory); final Castle orcCastle = app.getCastle(orcFactory);
assertTrue(orcCastle instanceof OrcCastle); assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
} }
@Test @Test
public void army() { public void army() {
final Army elfArmy = app.getArmy(elfFactory); final Army elfArmy = app.getArmy(elfFactory);
assertTrue(elfArmy instanceof ElfArmy); assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
final Army orcArmy = app.getArmy(orcFactory); final Army orcArmy = app.getArmy(orcFactory);
assertTrue(orcArmy instanceof OrcArmy); assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
} }
@Test @Test
public void createElfKingdom() { public void createElfKingdom() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final King king = app.getKing(); final King king = app.getKing();
final Castle castle = app.getCastle(); final Castle castle = app.getCastle();
final Army army = app.getArmy(); final Army army = app.getArmy();
assertTrue(king instanceof ElfKing); assertTrue(king instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, king.getDescription()); assertEquals(ElfKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof ElfCastle); assertTrue(castle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription()); assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
assertTrue(army instanceof ElfArmy); assertTrue(army instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, army.getDescription()); assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
} }
@Test @Test
public void createOrcKingdom() { public void createOrcKingdom() {
app.createKingdom(orcFactory); app.createKingdom(orcFactory);
final King king = app.getKing(); final King king = app.getKing();
final Castle castle = app.getCastle(); final Castle castle = app.getCastle();
final Army army = app.getArmy(); final Army army = app.getArmy();
assertTrue(king instanceof OrcKing); assertTrue(king instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, king.getDescription()); assertEquals(OrcKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof OrcCastle); assertTrue(castle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
assertTrue(army instanceof OrcArmy); assertTrue(army instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, army.getDescription()); assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
} }
} }