Skip to content

Commit 6df19d6

Browse files
committed
Factory Method(09/25)
1 parent 19a3f3e commit 6df19d6

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// 키보드 인터페이스
2+
interface Keyboard {}
3+
4+
// 로지텍 키보드 클래스
5+
class LogitechKeyboard implements Keyboard {
6+
LogitechKeyboard() {
7+
System.out.println("로지텍 키보드 생성");
8+
}
9+
}
10+
11+
// 커세어 키보드 클래스
12+
class CorsairKeyboard implements Keyboard {
13+
CorsairKeyboard() {
14+
System.out.println("커세어 키보드 생성");
15+
}
16+
}
17+
18+
// 키보드 제작 클래스
19+
class KeyboardFactory {
20+
public Keyboard createKeyboard(String name) {
21+
Keyboard keyboard = null;
22+
switch (name) {
23+
case "Logitech":
24+
keyboard = new LogitechKeyboard();
25+
case "Corsair":
26+
keyboard = new CorsairKeyboard();
27+
}
28+
return keyboard;
29+
}
30+
}
31+
32+
// 마우스 인터페이스
33+
interface Mouse {}
34+
35+
// 로지텍 마우스 클래스
36+
class LogitechMouse implements Mouse {
37+
LogitechMouse() {
38+
System.out.println("로지텍 마우스 생성");
39+
}
40+
}
41+
42+
// 커세어 마우스 클래스
43+
class CorsairMouse implements Mouse {
44+
CorsairMouse() {
45+
System.out.println("커세어 마우스 생성");
46+
}
47+
}
48+
49+
// 마우스 제작
50+
class MouseFactory {
51+
public Mouse createMouse(String name) {
52+
Mouse mouse = null;
53+
switch (name) {
54+
case "Logitech":
55+
mouse = new LogitechMouse();
56+
break;
57+
case "Corsair":
58+
mouse = new CorsairMouse();
59+
break;
60+
}
61+
return mouse;
62+
}
63+
}
64+
65+
// 포장 형식
66+
class Package {
67+
Keyboard keyboard;
68+
Mouse mouse;
69+
70+
Package(Keyboard keyboard, Mouse mouse) {
71+
this.keyboard = keyboard;
72+
this.mouse = mouse;
73+
}
74+
}
75+
76+
// 패키지를 포장하는 클래스
77+
class PackageFactory {
78+
public Package createPackage(String type) {
79+
KeyboardFactory keyboardFactory = new KeyboardFactory();
80+
MouseFactory mouseFactory = new MouseFactory();
81+
82+
return new Package(keyboardFactory.createKeyboard(type), mouseFactory.createMouse(type));
83+
}
84+
}
85+
86+
// 포장 팩토리에 호출
87+
class Client {
88+
public void buying() {
89+
PackageFactory factory = new PackageFactory();
90+
Package pack = factory.createPackage("Corsair");
91+
}
92+
}

0 commit comments

Comments
 (0)