Secret Recipe of the Template Method: Po Learns the Art of Structured Cooking

 

Java, Design Patterns, Template Method Pattern, Functional Programming, Po and Mr. Ping, Java Lambda, Clean Code, Code Refactoring, Story-Driven Learning, JavaOnFly

Gala at the Jade Palace

A grand gala was being held at the Jade Palace. The Furious Five were preparing, and Po was helping his father, Mr. Ping, in the kitchen. But as always, Po had questions.

Po (curious): "Dad, how do you always make the perfect noodle soup no matter what the ingredients are?"

Mr. Ping (smiling wisely): "Ah, my boy, that’s because I follow the secret recipe—a fixed template!"

Mr. Ping Reveals the Template Method Pattern

Mr. Ping: "Po, the Template Method Pattern is like my noodle recipe. The skeleton of the cooking steps stays the same, but the ingredients and spices can vary!"

Po: "Wait, you mean like... every dish has a beginning, middle, and end—but I can change what goes inside?"

Mr. Ping: "Exactly! The fixed steps are defined in a base class, but subclasses—or in our case, specific dishes—override the variable parts."

Traditional Template Method in Java (Classic OOP)

Java
 
public abstract class DishRecipe {
// Template method
public final void cookDish() {
boilWater();
addIngredients();
addSpices();
serve();
}

private void boilWater() {
System.out.println("Boiling water...");
}

protected abstract void addIngredients();
protected abstract void addSpices();

private void serve() {
System.out.println("Serving hot!");
}
}

class NoodleSoup extends DishRecipe {
protected void addIngredients() {
System.out.println("Adding noodles, veggies, and tofu.");
}

protected void addSpices() {
System.out.println("Adding soy sauce and pepper.");
}
}

class DumplingSoup extends DishRecipe {
protected void addIngredients() {
System.out.println("Adding dumplings and bok choy.");
}

protected void addSpices() {
System.out.println("Adding garlic and sesame oil.");
}
}
public class TraditionalCookingMain {
public static void main(String[] args) {
DishRecipe noodle = new NoodleSoup();
noodle.cookDish();

System.out.println("\n---\n");

DishRecipe dumpling = new DumplingSoup();
dumpling.cookDish();
}
}
//Output
Boiling water...
Adding noodles, veggies, and tofu.
Adding soy sauce and pepper.
Serving hot!

---

Boiling water...
Adding dumplings and bok choy.
Adding garlic and sesame oil.
Serving hot!

Po: "Whoa! So each dish keeps the boiling and serving, but mixes up the center part. Just like kung fu forms!"

Functional Template Method Style

Po: "Dad, can I make it more... functional?"

Mr. Ping: "Yes, my son. We now wield the power of higher-order functions."

Java
 
import java.util.function.Consumer;

public class FunctionalTemplate {

public static <T> void prepareDish(T dishName, Runnable boil, Consumer<T> addIngredients, Consumer<T> addSpices, Runnable serve) {
boil.run();
addIngredients.accept(dishName);
addSpices.accept(dishName);
serve.run();
}

public static void main(String[] args) {
prepareDish("Noodle Soup",
() -> System.out.println("Boiling water..."),
dish -> System.out.println("Adding noodles, veggies, and tofu to " + dish),
dish -> System.out.println("Adding soy sauce and pepper to " + dish),
() -> System.out.println("Serving hot!")
);

prepareDish("Dumpling Soup",
() -> System.out.println("Boiling water..."),
dish -> System.out.println("Adding dumplings and bok choy to " + dish),
dish -> System.out.println("Adding garlic and sesame oil to " + dish),
() -> System.out.println("Serving hot!")
);
}
}

Po: "Look dad! Now we can cook anything, as long as we plug in the steps! It's like building recipes with Lego blocks!"

Mr. Ping (beaming): "Ah, my son. You are now a chef who understands both structure and flavor."

Real-World Use Case – Coffee Brewing Machines

Po: “Dad, Now I want to build the perfect coffee-making machine, just like our noodle soup recipe!”

Mr. Ping: “Ah, coffee, the elixir of monks and night-coders! Use the same template method wisdom, my son.”

Step-by-Step Template – Java OOP Coffee Brewer 

Java
 
abstract class CoffeeMachine {
// Template Method
public final void brewCoffee() {
boilWater();
addCoffeeBeans();
brew();
pourInCup();
}

private void boilWater() {
System.out.println("Boiling water...");
}

protected abstract void addCoffeeBeans();
protected abstract void brew();

private void pourInCup() {
System.out.println("Pouring into cup.");
}
}

class EspressoMachine extends CoffeeMachine {
protected void addCoffeeBeans() {
System.out.println("Adding finely ground espresso beans.");
}

protected void brew() {
System.out.println("Brewing espresso under high pressure.");
}
}

class DripCoffeeMachine extends CoffeeMachine {
protected void addCoffeeBeans() {
System.out.println("Adding medium ground coffee.");
}

protected void brew() {
System.out.println("Dripping hot water through the grounds.");
}
}
public class CoffeeMain {
public static void main(String[] args) {
CoffeeMachine espresso = new EspressoMachine();
espresso.brewCoffee();

System.out.println("\n---\n");

CoffeeMachine drip = new DripCoffeeMachine();
drip.brewCoffee();
}
}
//Ouput
Boiling water...
Adding finely ground espresso beans.
Brewing espresso under high pressure.
Pouring into cup.

---

Boiling water...
Adding medium ground coffee.
Dripping hot water through the grounds.
Pouring into cup.

Functional & Generic Coffee Brewing (Higher-Order Zen)

Po, feeling enlightened, says:

Po: “Dad! What if I want to make Green Tea or Hot Chocolate too?”

Mr. Ping (smirking): “Ahhh... Time to use the Generic Template of Harmony™!”

Functional Java Template for Any Beverage

Java
 
import java.util.function.Consumer;

public class BeverageBrewer {

public static <T> void brew(T name, Runnable boil, Consumer<T> addIngredients, Consumer<T> brewMethod, Runnable pour) {
boil.run();
addIngredients.accept(name);
brewMethod.accept(name);
pour.run();
}

public static void main(String[] args) {
brew("Espresso",
() -> System.out.println("Boiling water..."),
drink -> System.out.println("Adding espresso grounds to " + drink),
drink -> System.out.println("Brewing under pressure for " + drink),
() -> System.out.println("Pouring into espresso cup.")
);

System.out.println("\n---\n");

brew("Green Tea",
() -> System.out.println("Boiling water..."),
drink -> System.out.println("Adding green tea leaves to " + drink),
drink -> System.out.println("Steeping " + drink + " gently."),
() -> System.out.println("Pouring into tea cup.")
);
}
}
//Output
Boiling water...
Adding espresso grounds to Espresso
Brewing under pressure for Espresso
Pouring into espresso cup.

---

Boiling water...
Adding green tea leaves to Green Tea
Steeping Green Tea gently.
Pouring into tea cup.


Mr. Ping’s Brewing Wisdom

“In code as in cooking, keep your recipe fixed… but let your ingredients dance.”

  • Template Pattern gives you structure.

  • Higher-order functions give you flexibility.

  • Use both, and your code becomes as tasty as dumplings dipped in wisdom!


Mr. Ping: "Po, a great chef doesn't just follow steps. He defines the structure—but lets each ingredient bring its own soul."

Po: "And I shall pass down the Template protocol to my children’s children’s children!


Other Articles in this Series.

================================

1.Kung Fu Code: Master Shifu Teaches Strategy Pattern to Po – the Functional Way!

2. Code of Shadows:Mastering Decorator Pattern in Java – Po & Shifu’s Thriller Story of Clean Code

Kung Fu Code: Master Shifu Teaches Strategy Pattern to Po – the Functional Way!


 "There is no good or bad code . But how you write it… that makes all the difference.”-- Master Shifu


The sun had just touched the tips of the Valley of Peace. Birds chirped, the wind whispered tales of warriors, and Po—the Dragon Warrior—was busy trying to write some Java code. Yes, you read that right.

Master Shifu stood behind him, watching, amused and concerned.

Po (scratching his head): “Master Shifu, I’m trying to make this app where each Kung Fu move is chosen based on the enemy. But the code is… bloated. Classes everywhere. If OOP was noodles, this is a full buffet.”

Shifu (calmly sipping tea): “Ah, the classic Strategy Pattern. But there’s a better way, Po… a functional way. Let me show you the path.”

The Traditional (OOP) Strategy Pattern – Heavy Like Po’s Lunch   

Po wants to choose a fighting strategy based on his opponent.

// Strategy Interface
interface FightStrategy {
void fight();
}

// Concrete Strategies
class TigerFightStrategy implements FightStrategy {
public void fight() {
System.out.println("Attack with swift tiger strikes!");
}
}

class MonkeyFightStrategy implements FightStrategy {
public void fight() {
System.out.println("Use agile monkey flips!");
}
}

// Context
class Warrior {
private FightStrategy strategy;

public Warrior(FightStrategy strategy) {
this.strategy = strategy;
}

public void fight() {
strategy.fight();
}

public void setStrategy(FightStrategy strategy) {
this.strategy = strategy;
}
}

Usage

Warrior po = new Warrior(new TigerFightStrategy());
po.fight(); // Output: Attack with swift tiger strikes!

po.setStrategy(new MonkeyFightStrategy());
po.fight(); // Output: Use agile monkey flips!


Why This Is a Problem (and Why Po Is Annoyed)

Po: “So many files, interfaces, boilerplate! All I want is to change moves easily. This feels like trying to meditate with a noodle cart passing by!”

Indeed, OOP Strategy pattern works, but it's verboserigid, and unnecessarily class-heavy. It violates the spirit of quick Kung Fu adaptability!

Enter Functional Programming – The Way of Inner Simplicity

Shifu (nodding): “Po, what if I told you… that functions themselves can be passed around like scrolls of wisdom?”  

Po: “Whoa... like… JScrolls

Shifu: “No, Po. Java lambdas.” 

In functional programmingfunctions are first-class citizens. You don’t need classes to wrap behavior. You can pass behavior directly.

Higher-Order Functions – functions that take other functions as parameters or return them.

Po, In Java8 onwards , now we can do that easily with the help of lambda, lambda can wrap the functionality and can be pass to another method as a parameter.

Strategy Pattern – The Functional Way in Java



import java.util.function.Consumer;
class Warrior {
private Consumer<Void> strategy;

public Warrior(Consumer<Void> strategy) {
this.strategy = strategy;
}

public void fight() {
strategy.accept(null);
}

public void setStrategy(Consumer<Void> strategy) {
this.strategy = strategy;
}
}

But there’s a better, cleaner way with just lambdas and no class at all.

import java.util.function.Supplier;

public class FunctionalStrategy {

public static void main(String[] args) {
// Each strategy is just a lambda
Runnable tigerStyle = () -> System.out.println("Attack with swift tiger strikes!");
Runnable monkeyStyle = () -> System.out.println("Use agile monkey flips!");
Runnable pandaStyle = () -> System.out.println("Roll and belly-bounce!");

// Fighter is a high-order function executor
executeStrategy(tigerStyle);
executeStrategy(monkeyStyle);
executeStrategy(pandaStyle);
}

static void executeStrategy(Runnable strategy) {
strategy.run();
}
}

Shifu (with a gentle tone):

“Po, in the art of code—as in Kung Fu—not every move needs a name, nor every master a title. In our example, we summoned the ancient scroll of Runnable… a humble interface with but one method—run(). In Java8 , we called it Functional Interface.

Think of it as a silent warrior—it expects no inputs(parameters) , demands no rewards(return type), and yet, performs its duty when called.

Each fighting style—tiger, monkey, panda—was not wrapped in robes of classes, but flowed freely as lambdas.

And then, we had the executeStrategy() method…
a higher-order sensei.

It does not fight itself, Po. It simply receives the wisdom of a move—a function—and executes it when the time is right.

This… is the way of functional composition.
You do not command the move—you invite it.
You do not create many paths—you simply choose the next step.”

Benefits – As Clear As The Sacred Pool of Tears

  • No extra interfaces or classes
  •  Easily switch behaviors at runtime

  • More readable, composable, and flexible

  •  Promotes the power of behavior as data.

Real-World Example: Choosing Payment Strategy in an App

Map<String, Runnable> paymentStrategies = Map.of(
"CARD", () -> System.out.println("Processing via Credit Card"),
"UPI", () -> System.out.println("Processing via UPI"),
"CASH", () -> System.out.println("Processing via Cash")
);

String chosen = "UPI";
paymentStrategies.get(chosen).run(); // Output: Processing via UPI

Po: “This is amazing! It’s like picking dumplings from a basket, but each dumpling is a deadly move.” 

Shifu: “Exactly. The Strategy   was never about the class, Po. It was about choosing the right move at the right moment… effortlessly.” 

One move=One lambda.

The good part is this lambda only holds the move details nothing else. So any warrior can master these moves , to apply the move unnecessary he does not need to reference a bounded object which wrapped this move in a boilerplate class.

Final Words of Wisdom  

“The strength of a great developer lies not in how many patterns they know… but in how effortlessly they flow between object thinking and function weaving to craft code that adapts like water, yet strikes like steel.”-- Master Shifu, on the Tao of Design Patterns.


Coming Up in the Series