Skip to content

Commit 1427927

Browse files
authored
Sync readme.md with the new code
1 parent 933c84f commit 1427927

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

abstract-factory/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,45 @@ king.getDescription(); // Output: This is the Elven king!
120120
army.getDescription(); // Output: This is the Elven Army!
121121
```
122122

123+
Now, we can design a factory for our different kingdom factories. In this example, we created FactoryMaker, responsible for returning an instance of either ElfKingdomFactory or OrcKingdomFactory.
124+
The client can use FactoryMaker to create the desired concrete factory which, in turn, will produce different concrete objects (Army, King, Castle).
125+
In this example, we also used an enum to parameterize which type of kingdom factory the client will ask for.
126+
127+
```
128+
public static class FactoryMaker {
129+
130+
public enum KingdomType {
131+
ELF, ORC
132+
}
133+
134+
public static KingdomFactory makeFactory(KingdomType type) {
135+
switch (type) {
136+
case ELF:
137+
return new ElfKingdomFactory();
138+
case ORC:
139+
return new OrcKingdomFactory();
140+
default:
141+
throw new IllegalArgumentException("KingdomType not supported.");
142+
}
143+
}
144+
}
145+
146+
public static void main(String[] args) {
147+
App app = new App();
148+
149+
LOGGER.info("Elf Kingdom");
150+
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
151+
LOGGER.info(app.getArmy().getDescription());
152+
LOGGER.info(app.getCastle().getDescription());
153+
LOGGER.info(app.getKing().getDescription());
154+
155+
LOGGER.info("Orc Kingdom");
156+
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
157+
-- similar use of the orc factory
158+
}
159+
```
160+
161+
123162
## Applicability
124163
Use the Abstract Factory pattern when
125164

0 commit comments

Comments
 (0)