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
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
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:
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
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.
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!!
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>:
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
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:
Reason | Oogway’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.”