Skip to content

Commit 0719d7c

Browse files
committed
Template Method Design Pattern - Juicy Application
1 parent 6deb6b9 commit 0719d7c

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.premaseem.templateMethod;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public abstract class AbstractBaseJuice {
10+
11+
/*
12+
* This is template method which defines the sequence and steps for preparation.
13+
*/
14+
// Note: Sequence is important and should be modified by sub classes
15+
final public void prepare() {
16+
pickUpFruits();
17+
washFruits();
18+
pileFruits();
19+
cutFruits();
20+
addLiquid();
21+
operateJuicer();
22+
decorateJuiceHook();
23+
}
24+
25+
// Variable methods would be implemented by sub classes
26+
abstract String getFruitName();
27+
28+
abstract void pickUpFruits();
29+
30+
abstract void addLiquid();
31+
32+
// Reusable methods are defined in base class
33+
void washFruits() {
34+
System.out.println("Washing the fruit ... " + getFruitName());
35+
}
36+
37+
void pileFruits() {
38+
System.out.println("piling the fruit ... " + getFruitName());
39+
}
40+
41+
void cutFruits() {
42+
System.out.println("cutting the fruit ... " + getFruitName());
43+
}
44+
45+
void operateJuicer() {
46+
System.out.println("... operate juicer Ghur Ghur Ghur ... ");
47+
}
48+
49+
void decorateJuiceHook() {
50+
System.out.println("add few dry fruits on top ");
51+
}
52+
}
53+
54+
class AppleMilkJuice extends AbstractBaseJuice {
55+
56+
@Override
57+
String getFruitName() {
58+
return " Apples ";
59+
}
60+
61+
@Override
62+
void pickUpFruits() {
63+
System.out.println(getFruitName() + " is picked up for Juice preparation ");
64+
}
65+
66+
@Override
67+
void addLiquid() {
68+
System.out.println("Add Milk in apple milk shake ");
69+
}
70+
71+
void decorateJuiceHook() {
72+
System.out.println("decorate with Strawberry on top ");
73+
}
74+
}
75+
76+
class OrangeJuice extends AbstractBaseJuice {
77+
@Override
78+
String getFruitName() {
79+
return " Orange ";
80+
}
81+
82+
@Override
83+
void pickUpFruits() {
84+
System.out.println(getFruitName() + " is picked up for Juice prepration ");
85+
}
86+
87+
@Override
88+
void addLiquid() {
89+
System.out.println("Add some water in Orange juice ");
90+
}
91+
92+
void decorateJuiceHook() {
93+
System.out.println("decorate with lime slice and straw ");
94+
}
95+
}
96+
97+
class MixFruitJuice extends AbstractBaseJuice {
98+
@Override
99+
String getFruitName() {
100+
return " Mixfruit ";
101+
}
102+
103+
@Override
104+
void pickUpFruits() {
105+
System.out.println(getFruitName() + " is picked up for Juice preparation ");
106+
}
107+
108+
@Override
109+
void addLiquid() {
110+
System.out.println("Adding some flavour ");
111+
}
112+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.premaseem.templateMethod;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import java.util.Scanner;
10+
11+
public class ClientForTemplateMethodJuicer {
12+
13+
public static void main(String[] args) {
14+
15+
System.out.println("Welcome to Template Method Fruit Juice Extractor Program ");
16+
17+
AbstractBaseJuice juice;
18+
Scanner scan = new Scanner(System.in);
19+
int repeatRunFlag = 1;
20+
while (repeatRunFlag == 1) {
21+
System.out.println("What fruit Juice would you like to have today ");
22+
System.out.println(" Press 1 for Apple Milk Shake Juice");
23+
System.out.println(" Press 2 for Orange Juice ");
24+
System.out.println(" Press 3 for Mix fruit Juice ");
25+
26+
int type = scan.nextInt();
27+
28+
switch (type) {
29+
case 1:
30+
juice = new AppleMilkJuice();
31+
break;
32+
case 2:
33+
juice = new OrangeJuice();
34+
break;
35+
case 3:
36+
juice = new MixFruitJuice();
37+
break;
38+
default:
39+
juice = new MixFruitJuice();
40+
}
41+
42+
juice.prepare();
43+
System.out.println("=============================");
44+
45+
System.out.println("Press 1 to make more Juices ;-) and 0 for EXIT .... ");
46+
try {
47+
repeatRunFlag = scan.nextInt();
48+
} catch (Exception e) {
49+
repeatRunFlag = 0;
50+
}
51+
}
52+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
53+
54+
}
55+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Template Method Fruit Juice example defines a recipe (with exact steps and quantity) to prepare the drink which is kept in the extract class. However the flavor of the fruit Juice can be changed by it subclasses by overriding (which is defined as hooks/abstractor in parent abstract class).
2+
3+
Intent:
4+
Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
5+
Base class declares algorithm 'placeholders', and derived classes implement the placeholders.
6+
7+
8+
Problem:
9+
Two different components have significant similarities, but demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended.
10+
11+
Definition:
12+
Template method design pattern is to define an algorithm as skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm is preserved by the parent class.
13+
14+
15+
The component designer decides which steps of an algorithm are invariant (or standard), and which are variant (or customizable). The invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all. The variant steps represent "hooks", or "placeholders", that can, or must, be supplied by the component's client in a concrete derived class.
16+
17+
The component designer mandates the required steps of an algorithm, and the ordering of the steps, but allows the component client to extend or replace some number of these steps.
18+
19+
Template Method is used prominently in frameworks. Each framework implements the invariant pieces of a domain's architecture, and defines "placeholders" for all necessary or interesting client customization options. In so doing, the framework becomes the "center of the universe", and the client customizations are simply "the third rock from the sun". This inverted control structure has been affectionately labelled "the Hollywood principle" - "don't call us, we'll call you".
20+
21+
22+
23+
Rules of thumb:
24+
Strategy is like Template Method except in its granularity.
25+
Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
26+
Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.
27+
Factory Method is a specialization of Template Method.

0 commit comments

Comments
 (0)