Skip to content

Commit 72268f6

Browse files
committed
Visitor Design Pattern - Visitor Cook party project
Cook visits at party location for cooking
1 parent 05e7f51 commit 72268f6

File tree

5 files changed

+150
-26
lines changed

5 files changed

+150
-26
lines changed

pattern/src/com/premaseem/Client.java

-13
This file was deleted.

patternBonus/src/com/premaseem/Client.java

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.premaseem;
2+
3+
import java.util.Scanner;
4+
5+
public class ClientFile {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println("Welcome to Party host example which uses visitor pattern ");
10+
Scanner scan = new Scanner(System.in);
11+
Party party;
12+
CookVisitorI visitorCook;
13+
int repeatRunFlag = 1;
14+
while (repeatRunFlag == 1) {
15+
System.out.println("Which party do you want to host ");
16+
System.out.println("press 1 for Week end party ");
17+
System.out.println("press 2 for Week day party ");
18+
int tvType = scan.nextInt();
19+
if (tvType == 1) {
20+
party = new LoudParty();
21+
} else {
22+
party = new CalmParty();
23+
}
24+
25+
System.out.println("How would you want to manage cooking of food ");
26+
System.out.println(" Press 1 for a visitor Veg Cook ");
27+
System.out.println(" Press 2 for a visitor Non- Veg Cook ");
28+
System.out.println(" Press 3 for in house cooking (no visitor) ");
29+
30+
int type = scan.nextInt();
31+
try {
32+
switch (type) {
33+
case 1:
34+
visitorCook = new VegCookVisitor();
35+
party.accept(visitorCook);
36+
break;
37+
case 2:
38+
visitorCook = new NonVegCookVisitor();
39+
party.accept(visitorCook);
40+
break;
41+
case 3:
42+
party.cookInHouse();
43+
break;
44+
}
45+
46+
47+
} catch (Exception e1) {
48+
e1.printStackTrace();
49+
}
50+
System.out.println("=============================");
51+
System.out.println("Press 1 to Repeat .... ");
52+
try {
53+
repeatRunFlag = scan.nextInt();
54+
} catch (Exception e) {
55+
repeatRunFlag = 0;
56+
}
57+
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.premaseem;
2+
3+
public interface CookVisitorI {
4+
void cook (LoudParty loudParty);
5+
void cook (CalmParty calmParty);
6+
}
7+
8+
class VegCookVisitor implements CookVisitorI{
9+
10+
@Override
11+
public void cook(LoudParty loudParty) {
12+
loudParty.meal = "Spicy Vegetables";
13+
loudParty.drink = "Fruit Beer";
14+
loudParty.music = "Loud music";
15+
loudParty.getPartyDetail();
16+
}
17+
18+
@Override
19+
public void cook(CalmParty calmParty) {
20+
calmParty.meal = "Boiled Vegetables";
21+
calmParty.drink = "Fruit Juice";
22+
calmParty.music = "Meditation Music";
23+
calmParty.getPartyDetail();
24+
}
25+
}
26+
27+
class NonVegCookVisitor implements CookVisitorI{
28+
29+
@Override
30+
public void cook(LoudParty loudParty) {
31+
loudParty.meal = "Spicy chicken";
32+
loudParty.drink = "Beer";
33+
loudParty.music = "High Beat music";
34+
loudParty.getPartyDetail();
35+
}
36+
37+
@Override
38+
public void cook(CalmParty calmParty) {
39+
calmParty.meal = "Non spicy chicken";
40+
calmParty.drink = "Fruit Beer";
41+
calmParty.music = "Meditation music";
42+
calmParty.getPartyDetail();
43+
}
44+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.premaseem;
2+
3+
public abstract class Party {
4+
5+
String music = " No Music";
6+
String meal = " empty";
7+
String drink = "plane water ";
8+
CookVisitorI visitor = null;
9+
10+
abstract void accept(CookVisitorI visitor);
11+
12+
13+
String getPartyDetail() {
14+
String detials = visitor!= null ?visitor.getClass().getSimpleName() : "in house cooking" + " organized " + this.getClass().getSimpleName() + " has Music : " + music + " with Drink :"
15+
+ drink + " & Meal : " + meal;
16+
System.out.println(detials);
17+
return detials;
18+
}
19+
20+
public void cookInHouse() {
21+
meal = "Spicy Vegetables";
22+
drink = "in house drink";
23+
music = "DJ music";
24+
getPartyDetail();
25+
}
26+
27+
}
28+
29+
class LoudParty extends Party {
30+
31+
@Override
32+
void accept(CookVisitorI visitor) {
33+
this.visitor = visitor;
34+
visitor.cook(this);
35+
}
36+
}
37+
38+
class CalmParty extends Party {
39+
40+
@Override
41+
void accept(CookVisitorI visitor) {
42+
this.visitor = visitor;
43+
visitor.cook(this);
44+
}
45+
}
46+

0 commit comments

Comments
 (0)