- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Factory Pattern.java
More file actions
Latest commit
75 lines (58 loc) · 1.57 KB
/
Copy pathJava Factory Pattern.java
File metadata and controls
75 lines (58 loc) · 1.57 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
importjava.security.Permission;
importjava.util.Scanner;
interfaceFood {
publicStringgetType();
}
classPizzaimplementsFood {
publicStringgetType() {
return"Someone ordered a Fast Food!";
}
}
classCakeimplementsFood {
publicStringgetType() {
return"Someone ordered a Dessert!";
}
}
classFoodFactory {
publicFoodgetFood(Stringorder) {
if ("pizza".equals(order)) {
returnnewPizza();
} elseif ("cake".equals(order)) {
returnnewCake();
}
returnnull;
}// End of getFood method
}// End of factory class
publicclassSolution {
publicstaticvoidmain(Stringargs[]) {
Do_Not_Terminate.forbidExit();
try {
@SuppressWarnings("resource")
Scannersc = newScanner(System.in);
// creating the factory
FoodFactoryfoodFactory = newFoodFactory();
// factory instantiates an object
Foodfood = foodFactory.getFood(sc.nextLine());
System.out.println("The factory returned " + food.getClass());
System.out.println(food.getType());
} catch (Do_Not_Terminate.ExitTrappedExceptione) {
System.out.println("Unsuccessful Termination!!");
}
}
}
classDo_Not_Terminate {
publicstaticclassExitTrappedExceptionextendsSecurityException {
privatestaticfinallongserialVersionUID = 1L;
}
publicstaticvoidforbidExit() {
finalSecurityManagersecurityManager = newSecurityManager() {
@Override
publicvoidcheckPermission(Permissionpermission) {
if (permission.getName().contains("exitVM")) {
thrownewExitTrappedException();
}
}
};
System.setSecurityManager(securityManager);
}
}