- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory_Pattern.java
More file actions
Latest commit
78 lines (61 loc) · 1.68 KB
/
Copy pathFactory_Pattern.java
File metadata and controls
78 lines (61 loc) · 1.68 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
76
77
78
importjava.util.*;
importjava.security.*;
interfaceFood {
publicStringgetType();
}
classPizzaimplementsFood {
publicStringgetType() {
return"Someone ordered a Fast Food!";
}
}
classCakeimplementsFood {
publicStringgetType() {
return"Someone ordered a Dessert!";
}
}
classFoodFactory {
publicFoodgetFood(Stringorder) {
//Write your code here
if (order.equals("cake"))
{
return (newCake()); //returning the object of the respective class..
}
else
{
return (newPizza());
}
}//End of getFood method..
}//End of factory class
publicclassSolution {
publicstaticvoidmain(Stringargs[]){
Do_Not_Terminate.forbidExit();
try{
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);
}
}