DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Related

  • Kubernetes Installation in RedHat/CentOS
  • Advanced Java Garbage Collection Concepts: Weak References, Finalization, and Memory Leaks
  • Distributed Rate Limiting in Java: A Deep Dive into Bucket4j + PostgreSQL
  • From Java 8 to Java 21: How the Evolution Changed My Developer Workflow

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Integrating Lighthouse Test Automation Into Your CI/CD Pipeline
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • Unveiling Supply Chain Transformation: IIoT and Digital Twins
  1. DZone
  2. Coding
  3. Java
  4. Code of Shadows: Master Shifu and Po Use Functional Java to Solve the Decorator Pattern Mystery

Code of Shadows: Master Shifu and Po Use Functional Java to Solve the Decorator Pattern Mystery

A humorous and educational comparison of the OOP and functional approaches to the Decorator pattern in Java, featuring Po and Master Shifu.

By 
Shamik Mitra user avatar
Shamik Mitra
·
Jun. 16, 25 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
2.1K Views

Join the DZone community and get the full member experience.

Join For Free

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:

Java
 
//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:

Java
 
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.

Java
 
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 SimpleLogger, TimestampLogger, 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:

Java
 
 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..."

Java
 
public <T> void decoratedLogMessage(Function<T, T>... loggers) {


"This declares a generic method where T can be any type — String, Integer, 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." 

Java
 
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!"
Java
 
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

Java
 
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

Feature OOP Decorator Functional Decorator
Needs Class Yes No
Uses Interface Yes Optional
Composability Rigid Elegant
Boilerplate High Minimal
Flexibility Moderate High (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."

Decorator pattern Java (programming language) master

Opinions expressed by DZone contributors are their own.

Related

  • Kubernetes Installation in RedHat/CentOS
  • Advanced Java Garbage Collection Concepts: Weak References, Finalization, and Memory Leaks
  • Distributed Rate Limiting in Java: A Deep Dive into Bucket4j + PostgreSQL
  • From Java 8 to Java 21: How the Evolution Changed My Developer Workflow

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: