-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Visitor Design Pattern - Visitor Cook party project
Cook visits at party location for cooking
- Loading branch information
Showing
5 changed files
with
150 additions
and
26 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.premaseem; | ||
|
||
import java.util.Scanner; | ||
|
||
public class ClientFile { | ||
|
||
public static void main(String[] args) { | ||
|
||
System.out.println("Welcome to Party host example which uses visitor pattern "); | ||
Scanner scan = new Scanner(System.in); | ||
Party party; | ||
CookVisitorI visitorCook; | ||
int repeatRunFlag = 1; | ||
while (repeatRunFlag == 1) { | ||
System.out.println("Which party do you want to host "); | ||
System.out.println("press 1 for Week end party "); | ||
System.out.println("press 2 for Week day party "); | ||
int tvType = scan.nextInt(); | ||
if (tvType == 1) { | ||
party = new LoudParty(); | ||
} else { | ||
party = new CalmParty(); | ||
} | ||
|
||
System.out.println("How would you want to manage cooking of food "); | ||
System.out.println(" Press 1 for a visitor Veg Cook "); | ||
System.out.println(" Press 2 for a visitor Non- Veg Cook "); | ||
System.out.println(" Press 3 for in house cooking (no visitor) "); | ||
|
||
int type = scan.nextInt(); | ||
try { | ||
switch (type) { | ||
case 1: | ||
visitorCook = new VegCookVisitor(); | ||
party.accept(visitorCook); | ||
break; | ||
case 2: | ||
visitorCook = new NonVegCookVisitor(); | ||
party.accept(visitorCook); | ||
break; | ||
case 3: | ||
party.cookInHouse(); | ||
break; | ||
} | ||
|
||
|
||
} catch (Exception e1) { | ||
e1.printStackTrace(); | ||
} | ||
System.out.println("============================="); | ||
System.out.println("Press 1 to Repeat .... "); | ||
try { | ||
repeatRunFlag = scan.nextInt(); | ||
} catch (Exception e) { | ||
repeatRunFlag = 0; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.premaseem; | ||
|
||
public interface CookVisitorI { | ||
void cook (LoudParty loudParty); | ||
void cook (CalmParty calmParty); | ||
} | ||
|
||
class VegCookVisitor implements CookVisitorI{ | ||
|
||
@Override | ||
public void cook(LoudParty loudParty) { | ||
loudParty.meal = "Spicy Vegetables"; | ||
loudParty.drink = "Fruit Beer"; | ||
loudParty.music = "Loud music"; | ||
loudParty.getPartyDetail(); | ||
} | ||
|
||
@Override | ||
public void cook(CalmParty calmParty) { | ||
calmParty.meal = "Boiled Vegetables"; | ||
calmParty.drink = "Fruit Juice"; | ||
calmParty.music = "Meditation Music"; | ||
calmParty.getPartyDetail(); | ||
} | ||
} | ||
|
||
class NonVegCookVisitor implements CookVisitorI{ | ||
|
||
@Override | ||
public void cook(LoudParty loudParty) { | ||
loudParty.meal = "Spicy chicken"; | ||
loudParty.drink = "Beer"; | ||
loudParty.music = "High Beat music"; | ||
loudParty.getPartyDetail(); | ||
} | ||
|
||
@Override | ||
public void cook(CalmParty calmParty) { | ||
calmParty.meal = "Non spicy chicken"; | ||
calmParty.drink = "Fruit Beer"; | ||
calmParty.music = "Meditation music"; | ||
calmParty.getPartyDetail(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.premaseem; | ||
|
||
public abstract class Party { | ||
|
||
String music = " No Music"; | ||
String meal = " empty"; | ||
String drink = "plane water "; | ||
CookVisitorI visitor = null; | ||
|
||
abstract void accept(CookVisitorI visitor); | ||
|
||
|
||
String getPartyDetail() { | ||
String detials = visitor!= null ?visitor.getClass().getSimpleName() : "in house cooking" + " organized " + this.getClass().getSimpleName() + " has Music : " + music + " with Drink :" | ||
+ drink + " & Meal : " + meal; | ||
System.out.println(detials); | ||
return detials; | ||
} | ||
|
||
public void cookInHouse() { | ||
meal = "Spicy Vegetables"; | ||
drink = "in house drink"; | ||
music = "DJ music"; | ||
getPartyDetail(); | ||
} | ||
|
||
} | ||
|
||
class LoudParty extends Party { | ||
|
||
@Override | ||
void accept(CookVisitorI visitor) { | ||
this.visitor = visitor; | ||
visitor.cook(this); | ||
} | ||
} | ||
|
||
class CalmParty extends Party { | ||
|
||
@Override | ||
void accept(CookVisitorI visitor) { | ||
this.visitor = visitor; | ||
visitor.cook(this); | ||
} | ||
} | ||
|