-
Notifications
You must be signed in to change notification settings - Fork 0
Template method pattern #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| public class Coffee extends Drink { | ||
| @Override | ||
| public void createDrink() { | ||
| System.out.println("Drip hot water through filter and ground coffee."); | ||
| } | ||
|
|
||
| @Override | ||
| public void addMore(boolean addition) { | ||
| this.addition = addition; | ||
|
|
||
| if (addition) { | ||
| System.out.println("Add milk and sugar."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| public abstract class Drink { | ||
| boolean addition = true; | ||
|
|
||
| public final void order(String addition) { | ||
| boilWater(); | ||
| createDrink(); | ||
| serveDrink(); | ||
| } | ||
|
|
||
| public final void boilWater() { | ||
| System.out.println("Boil Water."); | ||
| } | ||
|
|
||
| public abstract void createDrink(); | ||
|
|
||
| public void addMore(boolean addition) { | ||
| this.addition = addition; | ||
| } | ||
|
|
||
| public final void serveDrink() { | ||
| System.out.println("Pour into cup."); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| public class Egg extends Food { | ||
| @Override | ||
| public void gatherIngredients() { | ||
| super.gatherIngredients(); | ||
| System.out.println("Get 2 eggs."); | ||
| System.out.println("Get 1 tablespoon of butter."); | ||
| System.out.println("Get salt."); | ||
| System.out.println("Get pepper."); | ||
| } | ||
|
|
||
| @Override | ||
| public void prepareFood() { | ||
| super.prepareFood(); | ||
| System.out.println("Crack the eggs into a bowl."); | ||
| System.out.println("Wisk the eggs."); | ||
| System.out.println("Melt butter in pan on medium heat."); | ||
| System.out.println("When the butter is melted and hot, pour in eggs."); | ||
| System.out.println("Fold eggs as they cook."); | ||
| System.out.println("When eggs are cooked through turn off stove."); | ||
| System.out.println("Add salt and pepper to taste."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| public abstract class Food { | ||
| public final void order() { | ||
| gatherIngredients(); | ||
| prepareFood(); | ||
| serveFood(); | ||
| } | ||
|
|
||
| public void gatherIngredients() { | ||
| System.out.println("Gather your ingredients."); | ||
| } | ||
|
|
||
| public void prepareFood() { | ||
| System.out.println("Prepare your food."); | ||
| } | ||
|
|
||
| public final void serveFood() { | ||
| System.out.println("Serve the food."); | ||
| System.out.println("Serve on plate with any condiments you want."); | ||
| System.out.println("Enjoy!"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| public class Pancakes extends Food { | ||
| @Override | ||
| public void gatherIngredients() { | ||
| super.gatherIngredients(); | ||
| System.out.println("Get 1 and 1/2 cups flour."); | ||
| System.out.println("Get 3 and 1/2 teaspoons baking powder."); | ||
| System.out.println("Get 2 tablespoon sugar."); | ||
| System.out.println("Get 1 1/4 cups milk."); | ||
| System.out.println("Get 3 tablespoons of melted butter."); | ||
| System.out.println("Get 1 egg."); | ||
| } | ||
|
|
||
| @Override | ||
| public void prepareFood() { | ||
| super.prepareFood(); | ||
| System.out.println("Sift flour, baking powder, sugar, and salt together in a large bowl."); | ||
| System.out.println("Make a well in the center and add milk, melted butter, and egg."); | ||
| System.out.println("Mix until smooth."); | ||
| System.out.println("Heat a lightly oiled griddle or pan over medium-high heat."); | ||
| System.out.println("Pour or scoop the batter onto the griddle, using approximately 1/4 cup for each pancake."); | ||
| System.out.println("Cook until bubbles form and the edges are dry, about 2 to 3 minutes."); | ||
| System.out.println("Flip and cook until browned on the other side."); | ||
| System.out.println("Repeat with remaining batter."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| public class Tea extends Drink { | ||
| @Override | ||
| public void createDrink() { | ||
| System.out.println("Steep the tea."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package assignments.strategy_pattern; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class TemplateMethodRecipe { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner userInput = new Scanner(System.in); | ||
| String userChoice; | ||
| Food userFood = null; | ||
| Drink userDrink = null; | ||
|
|
||
| System.out.println("Hello! We are going to learn how to make breakfast!"); | ||
| System.out.println("What do you want for breakfast?"); | ||
| System.out.println("Choose eggs or pancakes."); | ||
|
|
||
| userChoice = userInput.nextLine(); | ||
|
|
||
| if (userChoice.equalsIgnoreCase("eggs")) { | ||
| userFood = new Egg(); | ||
| } else if (userChoice.equalsIgnoreCase("pancakes")) { | ||
| userFood = new Pancakes(); | ||
| } | ||
|
|
||
| userFood.order(); | ||
|
|
||
| System.out.println("Would you like coffee or tea as well?"); | ||
|
|
||
| userChoice = userInput.nextLine(); | ||
|
|
||
| if (userChoice.equalsIgnoreCase("coffee")) { | ||
| userDrink = new Coffee(); | ||
| } else if (userChoice.equalsIgnoreCase("tea")) { | ||
| userDrink = new Tea(); | ||
| } | ||
|
|
||
| userDrink.boilWater(); | ||
| userDrink.createDrink(); | ||
| userDrink.serveDrink(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AM-2: You should only be calling the order method. Without it you are not enforcing the pattern. |
||
|
|
||
| if (userChoice.equalsIgnoreCase("coffee")) { | ||
| System.out.println("Would you like milk and sugar with your coffee (y/n)?"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AM-1.5: The code inside here should be the hook method, and in the base class you default it to false. As this is now the Drinks are not using the template method. |
||
| userChoice = userInput.nextLine(); | ||
| if (userChoice.equalsIgnoreCase("y") || userChoice.equalsIgnoreCase("yes")) { | ||
| userDrink.addMore(true); | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Enjoy your Breakfast!"); | ||
|
|
||
| userInput.close(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AM-1: The hook method should be on the base class. The subclasses can implement "helper" private methods, but not other public methods.