Backend Development 8 min read

Applying the Decorator Pattern in a Spring Boot Project: A Practical Guide

This article demonstrates how to use the Decorator (Wrapper) pattern in a Spring Boot application to extend existing services with additional responsibilities, showing both single‑layer and multi‑layer decorations through concrete code examples and controller endpoints.

Top Architect
Top Architect
Top Architect
Applying the Decorator Pattern in a Spring Boot Project: A Practical Guide

The article explains how to apply the Decorator pattern in a Spring Boot project to extend functionality without modifying existing classes.

Decorator (Wrapper) pattern is a structural design pattern that adds responsibilities to objects dynamically, offering a more flexible alternative to inheritance.

Example interface ISurfTheInternetService defines a doSurfing() method:

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:18
 * @Description: 网上冲浪
 */
public interface ISurfTheInternetService {
    /**
     * 冲起来
     */
    void doSurfing();
}

Two concrete implementations provide basic surfing behavior:

import com.example.mydemo.service.ISurfTheInternetService;
import org.springframework.stereotype.Service;

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:21
 * @Description: 时尚 网咖
 */
@Service("fadInternetCafeService")
public class FadInternetCafe implements ISurfTheInternetService {
    @Override
    public void doSurfing() {
        System.out.println("在时尚 网咖 ,网上冲浪咯~");
    }
}
import com.example.mydemo.service.ISurfTheInternetService;
import org.springframework.stereotype.Service;

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:21
 * @Description: 复古 网吧
 */
@Service("retroInternetBarService")
public class RetroInternetBar implements ISurfTheInternetService {
    @Override
    public void doSurfing() {
        System.out.println("在复古 网吧 ,网上冲浪咯~");
    }
}

A SurfDecorator class implements the same interface, holds a reference to an ISurfTheInternetService , and adds extra behavior before delegating:

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:29
 * @Description:
 */
public class SurfDecorator implements ISurfTheInternetService {
    private ISurfTheInternetService surfTheInternetService;
    public SurfDecorator(ISurfTheInternetService surfTheInternetService) {
        this.surfTheInternetService = surfTheInternetService;
    }
    @Override
    public void doSurfing() {
        System.out.println("SurfDecorator 模拟业务 增强器在玩一点很新的东西,可能是一些额外的职责业务....");
        surfTheInternetService.doSurfing();
        System.out.println("SurfDecorator 模拟业务 增强器在玩一点很新的东西,比如说是XXXX");
    }
}

Controller methods illustrate single‑layer decoration:

@GetMapping("/useDecoratorTest")
public void useDecoratorTest() {
    SurfDecorator fadInternetCafeDecoratorService = new SurfDecorator(fadInternetCafeService);
    fadInternetCafeDecoratorService.doSurfing();
    SurfDecorator retroInternetBarDecoratorService = new SurfDecorator(retroInternetBarService);
    retroInternetBarDecoratorService.doSurfing();
}

To demonstrate multi‑layer decoration, a RechargeDecorator extends SurfDecorator and adds a recharge‑checking step:

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:29
 * @Description:
 */
public class RechargeDecorator extends SurfDecorator {
    public RechargeDecorator(ISurfTheInternetService surfTheInternetService) {
        super(surfTheInternetService);
    }
    @Override
    public void doSurfing() {
        super.doSurfing();
        checkRecharge();
    }
    private void checkRecharge(){
        System.out.print("RechargeDecorator 也在增强,看看这个货卡里面充了有多少,就来上网");
    }
}

Multi‑layer usage in a controller:

@GetMapping("/moreDecoratorTest")
public void moreDecoratorTest() {
    // first layer
    SurfDecorator retroInternetBarDecoratorService = new SurfDecorator(retroInternetBarService);
    // second layer
    RechargeDecorator rechargeDecorator = new RechargeDecorator(retroInternetBarDecoratorService);
    rechargeDecorator.doSurfing();
}

Running the endpoints shows the original surfing messages together with the additional messages injected by the decorators, confirming that responsibilities can be stacked transparently.

design patternsJavabackend developmentSpring BootDecorator Pattern
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.