07 – Incredible Decorator Pattern

07 – Incredible Decorator Pattern

Decorator Pattern in Java: Adding Behavior Dynamically

The Problem

You want to add responsibilities to an object dynamically without changing its class or exploding the number of subclasses (one per feature combination). The Decorator pattern wraps an object with another object that adds behavior, while implementing the same interface.

Diagram

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor': '#eef2ff', 'primaryTextColor': '#1e293b', 'primaryBorderColor': '#818cf8', 'lineColor': '#94a3b8', 'secondaryColor': '#f0fdf4', 'secondaryBorderColor': '#86efac', 'tertiaryColor': '#fff7ed', 'tertiaryBorderColor': '#fdba74', 'background': '#ffffff', 'mainBkg': '#eef2ff', 'secondBkg': '#f0fdf4', 'tertiaryBkg': '#fff7ed', 'noteBkgColor': '#fff7ed', 'noteBorderColor': '#fdba74', 'actorBkg': '#eef2ff', 'actorBorder': '#818cf8' }}}%%
classDiagram
    class DataStream {
        <<interface>>
        +write(byte[] data)
    }
    class FileDataStream {
        +write(byte[] data)
    }
    class StreamDecorator {
        <<abstract>>
        -DataStream wrapped
        +write(byte[] data)
    }
    class EncryptingStream {
        +write(byte[] data)
    }
    class CompressingStream {
        +write(byte[] data)
    }

    DataStream <|.. FileDataStream
    DataStream <|.. StreamDecorator
    StreamDecorator <|-- EncryptingStream
    StreamDecorator <|-- CompressingStream
    StreamDecorator --> DataStream : wraps

Java Example

public interface DataStream {
    void write(byte[] data);
}

public class FileDataStream implements DataStream {
    public void write(byte[] data) {
        System.out.println("Writing " + data.length + " raw bytes to file");
    }
}

public abstract class StreamDecorator implements DataStream {
    protected final DataStream wrapped;

    protected StreamDecorator(DataStream wrapped) {
        this.wrapped = wrapped;
    }
}

public class CompressingStream extends StreamDecorator {
    public CompressingStream(DataStream wrapped) { super(wrapped); }

    public void write(byte[] data) {
        byte[] compressed = compress(data);
        wrapped.write(compressed);
    }

    private byte[] compress(byte[] data) { /* ... */ return data; }
}

public class EncryptingStream extends StreamDecorator {
    public EncryptingStream(DataStream wrapped) { super(wrapped); }

    public void write(byte[] data) {
        byte[] encrypted = encrypt(data);
        wrapped.write(encrypted);
    }

    private byte[] encrypt(byte[] data) { /* ... */ return data; }
}

Usage — compose behaviors freely:

DataStream stream = new EncryptingStream(new CompressingStream(new FileDataStream()));
stream.write("invoice data".getBytes());

Quarkus Example

Quarkus’s own @Decorator mechanism (part of CDI) implements this pattern natively — useful for cross-cutting logic like auditing on top of a business service, without touching the original bean:

public interface InvoiceService {
    Invoice issue(String customerId, double amount);
}

@ApplicationScoped
public class DefaultInvoiceService implements InvoiceService {
    public Invoice issue(String customerId, double amount) {
        return new Invoice(customerId, amount);
    }
}

@Decorator
@Priority(10)
public abstract class AuditingInvoiceDecorator implements InvoiceService {

    @Inject
    @Delegate
    InvoiceService delegate;

    @Override
    public Invoice issue(String customerId, double amount) {
        System.out.println("AUDIT: issuing invoice for " + customerId);
        return delegate.issue(customerId, amount);
    }
}

Register it in META-INF/beans.xml under <decorators>, and every call to InvoiceService is transparently audited.

Key Takeaways

  • Use Decorator to add behavior at runtime without subclass explosion — decorators can be stacked in any order.
  • Prefer composition (as above) over subclassing when features can combine independently.
  • Quarkus/CDI has first-class support for this pattern via @Decorator and @Delegate, commonly used for logging, auditing, and validation layers.