Skip to content

Commit 937ad97

Browse files
committed
facotry pattern of creational patterns
1 parent 9131e98 commit 937ad97

File tree

5 files changed

+188
-2
lines changed

5 files changed

+188
-2
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,18 @@
1313
<maven.compiler.target>17</maven.compiler.target>
1414
</properties>
1515

16+
<dependencies>
17+
<dependency>
18+
<groupId>org.reflections</groupId>
19+
<artifactId>reflections</artifactId>
20+
<version>0.9.11</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.javatuples</groupId>
25+
<artifactId>javatuples</artifactId>
26+
<version>1.2</version>
27+
</dependency>
28+
</dependencies>
1629

1730
</project>

src/main/java/creational/builder/Demo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package creational.builder;
22

33

4+
import creational.builder.exercise.CodeBuilder;
5+
46
public class Demo {
57

68
public static void main(String[] args) {
@@ -37,7 +39,6 @@ public static void main(String[] args) {
3739
System.out.println(builder);
3840

3941

40-
4142
// Class Builder
4243
CodeBuilder cb = new CodeBuilder("Person").addField("age", "int").addField("name", "String");
4344
System.out.println(cb);

src/main/java/creational/builder/CodeBuilder.java renamed to src/main/java/creational/builder/exercise/CodeBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package creational.builder;
1+
package creational.builder.exercise;
22

33
import java.util.ArrayList;
44
import java.util.List;
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package creational.factory;
2+
3+
import org.javatuples.Pair;
4+
import org.reflections.Reflections;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.util.*;
10+
11+
interface IHotDrink
12+
{
13+
void consume();
14+
}
15+
16+
class Tea implements IHotDrink
17+
{
18+
@Override
19+
public void consume()
20+
{
21+
System.out.println("This tea is nice but I'd prefer it with milk.");
22+
}
23+
}
24+
25+
class Coffee implements IHotDrink
26+
{
27+
@Override
28+
public void consume()
29+
{
30+
System.out.println("This coffee is delicious");
31+
}
32+
}
33+
34+
interface IHotDrinkFactory
35+
{
36+
IHotDrink prepare(int amount);
37+
}
38+
39+
class TeaFactory implements IHotDrinkFactory
40+
{
41+
@Override
42+
public IHotDrink prepare(int amount)
43+
{
44+
System.out.println(
45+
"Put in tea bag, boil water, pour "
46+
+ amount + "ml, add lemon, enjoy!"
47+
);
48+
return new Tea();
49+
}
50+
}
51+
52+
class CoffeeFactory implements IHotDrinkFactory
53+
{
54+
55+
@Override
56+
public IHotDrink prepare(int amount)
57+
{
58+
System.out.println(
59+
"Grind some beans, boil water, pour "
60+
+ amount + " ml, add cream and sugar, enjoy!"
61+
);
62+
return new Coffee();
63+
}
64+
}
65+
66+
class HotDrinkMachine
67+
{
68+
public enum AvailableDrink
69+
{
70+
COFFEE, TEA
71+
}
72+
73+
private Map<AvailableDrink, IHotDrinkFactory> factories =
74+
new HashMap<>();
75+
76+
private List<Pair<String, IHotDrinkFactory>> namedFactories =
77+
new ArrayList<>();
78+
79+
public HotDrinkMachine() throws Exception
80+
{
81+
// option 1: use an enum
82+
for (AvailableDrink drink : AvailableDrink.values())
83+
{
84+
String s = drink.toString();
85+
String factoryName = "" + Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();
86+
Class<?> factory = Class.forName("creational.factory." + factoryName + "Factory");
87+
factories.put(drink, (IHotDrinkFactory) factory.getDeclaredConstructor().newInstance());
88+
}
89+
90+
// option 2: find all implementors of IHotDrinkFactory
91+
Set<Class<? extends IHotDrinkFactory>> types =
92+
new Reflections("creational.factory") // ""
93+
.getSubTypesOf(IHotDrinkFactory.class);
94+
for (Class<? extends IHotDrinkFactory> type : types)
95+
{
96+
namedFactories.add(new Pair<>(
97+
type.getSimpleName().replace("Factory", ""),
98+
type.getDeclaredConstructor().newInstance()
99+
));
100+
}
101+
}
102+
103+
public IHotDrink makeDrink() throws IOException
104+
{
105+
System.out.println("Available drinks");
106+
for (int index = 0; index < namedFactories.size(); ++index)
107+
{
108+
Pair<String, IHotDrinkFactory> item = namedFactories.get(index);
109+
System.out.println("" + index + ": " + item.getValue0());
110+
}
111+
112+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
113+
while (true)
114+
{
115+
String s;
116+
int i, amount;
117+
if ((s = reader.readLine()) != null
118+
&& (i = Integer.parseInt(s)) >= 0
119+
&& i < namedFactories.size())
120+
{
121+
System.out.println("Specify amount: ");
122+
s = reader.readLine();
123+
if (s != null
124+
&& (amount = Integer.parseInt(s)) > 0)
125+
{
126+
return namedFactories.get(i).getValue1().prepare(amount);
127+
}
128+
}
129+
System.out.println("Incorrect input, try again.");
130+
}
131+
}
132+
133+
public IHotDrink makeDrink(AvailableDrink drink, int amount)
134+
{
135+
return ((IHotDrinkFactory)factories.get(drink)).prepare(amount);
136+
}
137+
}
138+
139+
class AbstractFactoryDemo
140+
{
141+
public static void main(String[] args) throws Exception
142+
{
143+
HotDrinkMachine machine = new HotDrinkMachine();
144+
IHotDrink tea = machine.makeDrink(HotDrinkMachine.AvailableDrink.TEA, 200);
145+
tea.consume();
146+
147+
// interactive
148+
IHotDrink drink = machine.makeDrink();
149+
drink.consume();
150+
}
151+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package creational.factory.exercise;
2+
3+
import java.util.stream.Stream;
4+
5+
record Person(int id, String name) {}
6+
7+
public class PersonFactory {
8+
int numOfPerson = 0;
9+
10+
public Person createPerson(String name) {
11+
return new Person(numOfPerson++, name);
12+
}
13+
}
14+
15+
class ExerciseDemo {
16+
public static void main(String[] args) {
17+
final PersonFactory pf = new PersonFactory();
18+
19+
Stream.of("Tommy", "Sunguck", "Meow").forEach(n -> System.out.println(pf.createPerson(n)));
20+
}
21+
}

0 commit comments

Comments
 (0)