The Filter Chain Pattern: Defending the Jade Palace from Chameleon's Mimic Attack

"Po using Filter Chain Pattern to block Chameleon's Mimic Attack at Jade Palace - Java Design Pattern Tutorial"

 

The Chameleon Strikes!

The peaceful Jade Palace is under siege! The sinister Chameleon launches a cunning assault, mimicking the powerful moves of the Furious Five.

  •  Tigress’s strength

  •  Mantis’s sharp precision

  •  And Po’s own style!

The Chameleon thinks he can bypass the Jade Palace defenses. But he doesn’t know... the Jade Palace doesn’t just fight back—it filters threats with masterful design patterns.

What is the Filter Chain Pattern?

Po (striking a pose): "Chameleon! You see, we use the Filter Chain Pattern. Each of us intercepts and processes your move one step at a time. If the move is dangerous—we not only block it, but blocked by the warrior who is perfect for denaturalized the move. Until Jade Palace is safe —we denaturalize all moves.

The Filter Chain Pattern is a behavioral design pattern where multiple objects (filters) process a request sequentially.

"One does not stop the storm with one hand—but with many forming a chain." — Master Oogway

 Traditional Java Implementation

Java
 
interface MoveFilter {
void doFilter(Move move, FilterChain chain);
}

class Move {
String type;
public Move(String type) {
this.type = type;
}
}

class FilterChain {
private List<MoveFilter> filters = new ArrayList<>();
private int index = 0;

public FilterChain addFilter(MoveFilter filter) {
filters.add(filter);
return this;
}

public void doFilter(Move move) {
if (index < filters.size()) {
filters.get(index++).doFilter(move, this);
}
}
}

Po: “Every hero has a technique. The MoveFilter interface is like our kung fu scroll—each warrior must follow its rule to block the move and pass it on.”

Po: “Move class is the move itself! It can be anything—strength, stealth, dumpling—the Chameleon’s weapon!”

Po: “FilterChain is the chain of defense! Each master takes their turn. If the move is blocked, we stop it. If not, we pass it along. One block. One strike. One chain.”

Filter Implementation

Java
 
class StrengthFilter implements MoveFilter {
public void doFilter(Move move, FilterChain chain) {
if (move.type.contains("strength")) {
System.out.println("Tigress blocks the brute force.");
} else {
chain.doFilter(move);
}
}
}

class PrecisionFilter implements MoveFilter {
public void doFilter(Move move, FilterChain chain) {
if (move.type.contains("precision")) {
System.out.println("Mantis counters the sharp strike.");
} else {
chain.doFilter(move);
}
}
}

class PoStyleFilter implements MoveFilter {
public void doFilter(Move move, FilterChain chain) {
if (move.type.contains("dumpling")) {
System.out.println("Po devours the move with a dumpling roll!");
} else {
chain.doFilter(move);
}
}
}

Tigress: StrengthFilter- “Brute strength? That’s my turf. If it reeks of force, I end it right there. If not, let it pass to the next warrior.

Mantis: PrecisionFilter- “A calculated strike? I see through its angles. I neutralize it with elegance before it can even land.”

Po: PoStyleFilter- “When he mimicked my dumpling move—I responded in kind. With flavor and fury, I rolled it back with my own style!”

Jade Palace Defense System:

Java
 
public class JadePalace {
public static void main(String[] args) {
Move chameleonMove = new Move("precision + dumpling");

FilterChain chain = new FilterChain()
.addFilter(new StrengthFilter())
.addFilter(new PrecisionFilter())
.addFilter(new PoStyleFilter());

chain.doFilter(chameleonMove);
}
}

Po: “So Chameleon dared use precision + dumpling? First, Tigress sensed no brute force and let it pass. Then Mantis neutralized the precision. And finally—I rolled up his dumpling into oblivion!”

Functional Filter Chain-Sleek Like a Wuxi Finger Hold 

Java
 
class FunctionalFilterChain<T> {
private Function<T, T> chain = Function.identity();

public FunctionalFilterChain<T> addFilter(Function<T, T> nextFilter) {
chain = chain.andThen(nextFilter);
return this;
}

public T execute(T move) {
return chain.apply(move);
}
}

Oogway: “In the world of functions, your thoughts flow like a river. Each function gently shapes the water. One filter, one ripple.”

Po  recall Oogway's wisdom on defense and update the defense system in a functional way.

Po explained to other warriors  what he did with the defense system .

Explanation

  • I use functions instead of interfaces and classes.

  • Each filter is a transformation (like a Kung Fu move).

  • The chain is built using andThen, composing them one after the other.

Java
 
Function<String, String> strengthFilter = move -> {
if (move.contains("strength")) {
System.out.println("[Strength Filter] Blocked by Tigress.");
return move.replace("strength", "");
}
return move;
};

Function<String, String> precisionFilter = move -> {
if (move.contains("precision")) {
System.out.println("[Precision Filter] Countered by Mantis.");
return move.replace("precision", "");
}
return move;
};

Function<String, String> poFilter = move -> {
if (move.contains("dumpling")) {
System.out.println("[Po Filter] Eaten with sauce!");
return move.replace("dumpling", "");
}
return move;
};

Tigress: “Even in a functional world, I see the brute strength. I remove it from the stream.” 

Mantis: “A precise attack flows in. I slice it out with grace and allow the rest to move forward.”

Po: “No dumpling gets past me. Ever. It enters the stream, but I eat it before it hits the palace.”  

The Battle in Functional Way!!

Java
 
public class FunctionalBattle {
public static void main(String[] args) {
FunctionalFilterChain<String> battle = new FunctionalFilterChain<>();
battle.addFilter(strengthFilter)
.addFilter(precisionFilter)
.addFilter(poFilter);

String result = battle.execute("strength precision dumpling");
System.out.println("After filters: " + result);
}
}

Po: “The Chameleon tried to combine all three again. But we filtered them out—one by one. What remained was silence.” 

Po: "Each move gets purified step by step, like making a soup—first boil the strength, strain the precision, and spice up with dumplings!"

Result"  " (empty string, everything blocked!)

The Eternal Scroll of Flexibility

After the Chameleon's attack was neutralized by strength, precision, and dumpling-fueled belly rolls…

Po (still chewing):

“But Master... what if next time the enemy doesn't use brute strength or sharp moves? What if it's... spicy noodles or cold logic?” 

Master Oogway (meditating on a peach pit):

“Ahh, my dear dumpling. We must not build defenses only for what we know. We must prepare for the unknown. Just like the seasons change, so do threats.”

So Oogway unfurled the Eternal Scroll, crafting a GenericFilterChain<T>:

Java
 
class GenericFilterChain<T> {
private List<Function<T, T>> filters = new ArrayList<>();

public GenericFilterChain<T> addFilter(Function<T, T> filter) {
filters.add(filter);
return this;
}

public T apply(T input) {
for (Function<T, T> filter : filters) {
input = filter.apply(input);
}
return input;
}
}

What’s the magic?

  •  T means it works for anything—a string, a move, a potion, or a scroll.

  •  Each filter is just a transformation: "see the attack, mold the attack, pass it on."

The Last Attack

Java
 
String attack = "mimic stealth dumpling";
GenericFilterChain<String> guardian = new GenericFilterChain<>();
String result = guardian.apply(attack);
System.out.println("Post-defense: " + result);

Po: "Even your mimicry is nothing against Oogway’s wisdom. Every move, every thought—it’s filtered by design." 

Why Oogway Did This:

ReasonOogway’s Wisdom
 Futureproofing“Tomorrow’s danger may not resemble today’s.”
 Reusability“Why carve one statue when you can mold any?”
 Simplicity“Let the form emerge from the function, not the other way around.”
 Adaptability“One chain to defend them all.”

Po (wide-eyed): “So... we no longer build for tigers or dumplings. We build for anything.” 

Oogway (vanishing into petals): “Exactly. Now you are not just defending the Jade Palace—you are guarding the code realm itself.”

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

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

 


The Logging Conspiracy 

It was a cold, misty morning at the Jade Palace. The silence was broken not by combat… but by a mysterious glitch in the logs.

Po (rushing in): "Shifu! The logs… they're missing timestamps!"

Shifu (narrowing his eyes): "This is no accident, Po. This is a breach in the sacred code path. The timekeeper has been silenced."

Traditional OOP Decorator

Shifu unfurled an old Java scroll:

//Interface
package com.javaonfly.designpatterns.decorator.oops;
public interface Loggable {

public void logMessage(String message);
}
//Implementation
package com.javaonfly.designpatterns.decorator.oops.impl;
import com.javaonfly.designpatterns.decorator.oops.Loggable;
public class SimpleLogger implements Loggable {
@Override
public void logMessage(String message) {
System.out.println(message);
}
}
//Implementation
class TimestampLogger implements Loggable {
private Loggable wrapped;

public TimestampLogger(Loggable wrapped) {
this.wrapped = wrapped;
}

public void logMessage(String message) {
String timestamped = "[" + System.currentTimeMillis() + "] " + message;
wrapped.logMessage(timestamped);
}
}

//Calling the decorator
public class Logger {
public static void main(String[] args){
Loggable simpleLogger = new SimpleLogger();
simpleLogger.logMessage("This is a simple log message.");

Loggable timestampedLogger = new TimestampLogger(simpleLogger);
timestampedLogger.logMessage("This is a timestamped log message.");
}
}
//Output
This is a simple log message.
[1748594769477] This is a timestamped log message.

Po: "Wait, we’re creating all these classes just to add a timestamp?"

Shifu: "That is the illusion of control. Each wrapper adds bulk. True elegance lies in Functional Programming."

Functional Decorator Pattern with Lambdas

Shifu waved his staff and rewrote the scroll:

package com.javaonfly.designpatterns.decorator.fp;

import java.time.LocalDateTime;
import java.util.function.Function;

public class Logger {
  //higer order function
    public void decoratedLogMessage(Function<String, String> simpleLogger, Function<String, String> timestampLogger) {
        String message = simpleLogger.andThen(timestampLogger).apply("This is a log message.");
        System.out.println(message);
    }

    public static void main(String[] args){
        Logger logger = new Logger();

        Function<String, String> simpleLogger = message -> {
            System.out.println(message);
            return message;
        };

        Function<String, String> timestampLogger = message -> {
            String timestampedMessage =  "[" + System.currentTimeMillis() + "] " + ": " + message;
            return timestampedMessage;
        };

        logger.decoratedLogMessage(simpleLogger, timestampLogger);
    }
}
//Output
This is a log message.
[1748595357335] This is a log message.

Po (blinking): "So... no more wrappers, just function transformers?"

Shifu (nodding wisely): "Yes, Po. In Functional Programming, functions are first-class citizens. The Function<T, R> interface lets us compose behavior. Each transformation can be chained using andThen, like stacking skills in Kung Fu."

Breaking Down the Code – Functional Wisdom Explained

Po (scratching his head): "Shifu, what exactly is this Function<T, R> thing? Is it some kind of scroll?"

Shifu (gently): "Ah, Po. It is not a scroll. It is a powerful interface from the java.util.function package—a tool forged in the fires of Java 8."

"Function<T, R> represents a function that accepts an input of type T and produces a result of type R."

In our case:

Java
 
Function<String, String> simpleLogger

This means: “Take a String message, and return a modified String message.”

Each logger lambda—like simpleLogger and timestampLogger—does exactly that.

The Art of Composition — andThen

Po (eyes wide): "But how do they all work together? Like… kung fu moves in a combo?"

Shifu (smiling): "Yes. That combo is called composition. And the technique is called andThen."

Java
 
simpleLogger.andThen(timestampLogger)

This means:

  1. First, execute simpleLogger, which prints the message and passes it on.

  2. Then, take the result and pass it to timestampLogger, which adds the timestamp.

This is function chaining—the essence of functional design.

String message = simpleLogger
.andThen(timestampLogger)
.apply("This is a log message.");

Like chaining martial arts techniques, each function passes its result to the next—clean, fluid, precise.

Po: "So the message flows through each function like a river through stones?"

Shifu: "Exactly. That is the way of the Stream."

Functional Flow vs OOP Structure

Shifu (serenely): "Po, unlike the OOP approach where you must wrap one class inside another—creating bulky layers—the functional approach lets you decorate behavior on the fly, without classes or inheritance."

  • No need to create SimpleLoggerTimestampLogger, or interfaces.

  • Just use Function<String, String> lambdas and compose them.

The Secret to Clean Code

“A true master does not add weight to power. He adds precision to purpose.” – Master Shifu

This approach:

  • Eliminates boilerplate.
  • Encourages reusability.
  • Enables testability (each function can be unit-tested in isolation).
  • Supports dynamic behavior chaining.

Po's New Move: Making the Logger Generic

After mastering the basics, Po's eyes sparkled with curiosity.

Po: "Shifu, what if I want this technique to work with any type—not just strings?"

Shifu (with a deep breath): "Yes of course you can ! Try to write it, Dragon warrior."

Po meditated for a moment, and then rewrote the logger:

 public <T> void decoratedLogMessage(Function<T, T>... loggers) {
        Function<T, T> pipeline= Arrays.stream(loggers).sequential().reduce(Function.identity(), Function::andThen);
        T message = pipeline.apply((T) "This is a log message.");
        System.out.println(message);
    }
Po (bowing):
"Master Shifu, after learning to compose logging functions using 
Function<String, String>, I asked myself — what if I could decorate not just strings, but any type of data? Numbers, objects, anything! So I used generics and built this move..."

public <T> void decoratedLogMessage(Function<T, T>... loggers) 
"This declares a generic method where T can be any type — StringInteger, or even a custom User object.
The method takes a 
varargs of Function<T, T> — that means a flexible number of functions that take and return the same type."

Function<T, T> pipeline=
  Arrays.stream(loggers).sequential().reduce(Function.identity(), Function::andThen);
  • "I stream all the logger functions and reduce them into a single pipeline function using Function::andThen.

    • Function.identity() is the neutral starting point — like standing still before striking.

    • Function::andThen chains each logger — like chaining combos in kung fu!"

  • T message = pipeline.apply((T) "This is a log message.");
    

    I apply the final pipeline function to a sample input.
    Since this time I tested it with a String, I cast it as (T). But this method can now accept any type!"

    Shifu (smiling, eyes narrowing with pride):
    "You’ve taken the form beyond its scroll, Po. You have learned not just to use functions—but to respect their essence. This generic version... is the true Dragon Scroll of the Decorator."

    Modified Code by Po

  • package com.javaonfly.designpatterns.decorator.fp;
    
    import java.time.LocalDateTime;
    import java.util.Arrays;
    import java.util.function.Function;
    
    public class Logger {  
        public <T> void decoratedLogMessage(Function<T, T>... loggers) {
            Function<T, T> pipeline= Arrays.stream(loggers).sequential().reduce(Function.identity(), Function::andThen);
            T message = pipeline.apply((T) "This is a log message.");
            System.out.println(message);
        }
    
        public static void main(String[] args){
            Logger logger = new Logger();
            Function<String, String> simpleLogger = message -> {
                System.out.println(message);
                return message;
            };
    
            Function<String, String> timestampLogger = message -> {
                String timestampedMessage =  "[" + System.currentTimeMillis() + "] " + message;
                return timestampedMessage;
            };
            Function<String, String> JadeLogger = message -> {
                String JadeLoggedMessage =  "[jadelog] " + message;
                return JadeLoggedMessage;
            };
       
            logger.decoratedLogMessage(simpleLogger, timestampLogger,JadeLogger);
        }
    }
    //Output
    This is a log message.
    [jadelog] [1748598136677] This is a log message.

  • Wisdom Scroll: OOP vs Functional Decorator

    FeatureOOP DecoratorFunctional Decorator
    Needs ClassYesNo
    Uses InterfaceYesOptional
    ComposabilityRigidElegant
    BoilerplateHighMinimal
    FlexibilityModerateHigh (thanks to lambdas)


Final Words from Master Shifu

"Po, the world of code is full of distractions—designs that look powerful but slow us down. A true Kung Fu developer learns to adapt. To decorate without weight. To enhance without inheritance. To flow with functions, not fight the structure."


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