forked from Seogeurim/CS-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryTest.java
41 lines (33 loc) Β· 857 Bytes
/
FactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class FactoryTest {
public static void main(String[] args) {
Product pencil = ProductFactory.getProduct("Pencil");
pencil.sell(); // sell Pencil !!!
Product note = ProductFactory.getProduct("Note");
note.sell(); // sell Note !!!
}
}
interface Product {
public void sell();
}
class Pencil implements Product {
@Override
public void sell() {
System.out.println("sell Pencil !!!");
}
}
class Note implements Product {
@Override
public void sell() {
System.out.println("sell Note !!!");
}
}
class ProductFactory {
public static Product getProduct(String className) {
Product p = null;
switch (className) {
case "Pencil": p = new Pencil(); break;
case "Note": p = new Note(); break;
}
return p;
}
}