Fundamentals 13 min read

Mastering the Chain of Responsibility Pattern with a Qing Dynasty Journey Example

This article introduces the Chain of Responsibility design pattern using a whimsical Qing dynasty south‑tour scenario, walks through class design, UML diagrams, and Java code examples—including abstract officials, a singleton emperor, and a prime minister orchestrating the travel—demonstrating flexible, extensible workflow construction.

macrozheng
macrozheng
macrozheng
Mastering the Chain of Responsibility Pattern with a Qing Dynasty Journey Example

Introduction

For developers who have already worked with object‑oriented design, the Chain of Responsibility pattern may be familiar, and for students it will eventually appear. This article gives beginners a quick overview of the pattern.

Example

Imagine you are transported to the Qing dynasty and need to model the emperor’s south‑bound tour using code. The journey passes through four provinces—Zhili, Shandong, Jiangsu, Zhejiang—so the itinerary can be split into four responsibilities, each handled by a provincial official.

Design

Step 1: Identify classes

Emperor (the object being processed)

PrimeMinister (the manager of the chain)

Official (abstract class for provincial officials)

Step 2: Define relationships

Provincial officials are peers; they implement serve(Emperor) to handle the request.

PrimeMinister holds a reference to the Emperor and a list of Officials.

We abstract the officials into an Official abstract class and use a concrete PrimeMinister to assemble the chain.

UML Diagram

Class diagram showing the relationships.

Code

Abstract official:

@Data
public abstract class Official {
    protected String title;
    protected abstract void serve(Emperor emperor);
    @Override
    public String toString() {
        return title;
    }
}

Concrete official example (Hebei):

public class HebeiOfficial extends Official {
    public HebeiOfficial() {
        this.title = "直隶总督";
    }
    @Override
    protected void serve(Emperor emperor) {
        emperor.play(this, "避暑山庄");
    }
}

Singleton Emperor:

public class Emperor {
    private static final Emperor INSTANCE = new Emperor("乾隆");
    private final String name;
    private Emperor(String name) { this.name = name; }
    public static Emperor getInstance() { return INSTANCE; }
    public void play(Official official, String place) {
        System.out.println(official.getTitle() + " 安排 " + name + "皇帝游览了: " + place);
    }
}

PrimeMinister assembling the chain:

public class PrimeMinister {
    private static List<Official> list = new ArrayList<>();
    public static void main(String[] args) {
        list.add(new HebeiOfficial());
        list.add(new ShandongOfficial());
        list.add(new JiangsuOfficial());
        list.add(new ZhejiangOfficial());
        Emperor emperor = Emperor.getInstance();
        System.out.println("乾隆下江南!");
        start(list, emperor);
    }
    private static void start(List<Official> officials, Emperor emperor) {
        for (Official o : officials) {
            o.serve(emperor);
        }
    }
}

Adding a new step (JinanOfficial) to handle a sudden request for Daming Lake:

public class JinanOfficial extends Official {
    public JinanOfficial() {
        title = "济南知府";
    }
    @Override
    protected void serve(Emperor emperor) {
        emperor.play(this, "大明湖畔");
    }
}

Modified chain assembly includes the new official before Shandong:

list.add(new HebeiOfficial());
list.add(new JinanOfficial());
list.add(new ShandongOfficial());
list.add(new JiangsuOfficial());
list.add(new ZhejiangOfficial());

Program output before and after the change demonstrates the flexible insertion of responsibilities.

Summary and Extensions

The Chain of Responsibility is essentially an ordered list of tasks that are executed sequentially. It can be externalized into configuration files, used in workflow engines such as Activiti, Netflix Conductor, Spring MVC filters, Jenkins pipelines, and many other frameworks.

Understanding this pattern helps improve code extensibility and readability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Software ArchitectureOOP
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

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.