/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank"></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
public static abstract class BeveragePreparation {
// Template method
public final void prepareBeverage(boolean request) {
// algoritmo
boilWater(); // far bollire l'acqua
brew(); // infusione
pourIntoCup(); // versare nella tazza
if (request )
addCondiments(); // aggiungere spezie
}
private void boilWater() {
System.out.println("Boiling water");
}
private void pourIntoCup() {
System.out.println("Pouring into cup");
}
// azione a discrezione del algoritmo specifico
abstract void brew();
abstract void addCondiments();
}
public static class Tea extends BeveragePreparation {
@Override
void brew() {
execute("Steeping the tea"); // azione : mettere in infusione il tè
}
@Override
void addCondiments() {
execute("Adding lemon"); // azione : aggiungere limone
}
private void execute(String action) {
System.out.println(action);
}
}
public static class Coffee extends BeveragePreparation {
@Override
void brew() {
execute("Dripping coffee through filter"); // azione : far gocciolare il caffè attraverso il filtro
}
@Override
void addCondiments() {
execute("Adding sugar and milk"); // azione : aggiungere zucchero e latte
}
private void execute(String action) {
System.out.println(action);
}
}
public static void main(String args[]) {
// Unit test - uso del design pattern: template method
BeveragePreparation tea = new Tea();
tea.prepareBeverage(true);
System.out.println();
BeveragePreparation coffee = new Coffee();
coffee.prepareBeverage(false);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: