-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethods.java
More file actions
49 lines (43 loc) · 977 Bytes
/
FactoryMethods.java
File metadata and controls
49 lines (43 loc) · 977 Bytes
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
42
43
44
45
46
47
48
49
import java.util.*;
interface SmartPhone {
void use();
}
class Oppo implements SmartPhone {
public void use() {
System.out.println("Switching on Oppo Device.. ");
}
}
class Redmi implements SmartPhone {
public void use() {
System.out.println("Switching on Redmi Device..");
}
}
class Samsung implements SmartPhone {
public void use() {
System.out.println("Switching on Samsung Device..");
}
}
class SwitchOn {
// factory method
public SmartPhone turn(String phone) {
if (phone == null)
return null;
switch (phone) {
case "Oppo":
return new Oppo();
case "Redmi":
return new Redmi();
case "Samsung":
return new Samsung();
default:
throw new IllegalArgumentException("Unknown Device " + phone);
}
}
}
class FactoryMethods {
public static void main(String[] args) {
SwitchOn on = new SwitchOn();
SmartPhone phone = on.turn("Samsung");
phone.use();
}
}