Backend Development 17 min read

Understanding the Delegation Pattern Through SLF4J, JDBC, and Dubbo Case Studies

The article explains how the delegation pattern—combining a fixed core processing path with extensible open interfaces—underpins Java toolkits such as SLF4J, JDBC, and Dubbo, illustrating its implementation steps, when to apply it, and pitfalls like reverse delegation loops.

vivo Internet Technology
vivo Internet Technology
vivo Internet Technology
Understanding the Delegation Pattern Through SLF4J, JDBC, and Dubbo Case Studies

This article explores how to transform general technical solutions into mature toolkits, using the delegation pattern as a central theme. The author analyzes popular Java tools like SLF4J to derive general methodologies for technical construction.

1. SLF4J Overview

SLF4J (Simple Logging Facade for Java) provides a unified interface standard for various logging implementations, allowing business code to remain independent of specific logging solutions. When you include slf4j-api, you only get the interface standard, not a concrete implementation. The core classes are org.slf4j.Logger and org.slf4j.LoggerFactory. Since version 1.6.0, if no implementation is provided, SLF4J defaults to NOPLogger (a no-operation implementation).

2. How SLF4J Works

SLF4J uses two mechanisms: direct implementation for native adapters, and SPI (Service Provider Interface) mechanism for delegated implementations. For example, slf4j-log4j contains both slf4j-api and log4j dependencies, implementing SLF4J interfaces using LOG4J functionality. The SPI mechanism tells the JVM which implementation class to use at runtime.

3. The Delegation Pattern

The delegation pattern consists of two core elements: Core Path (a specific set of components executing computation in a defined order) and Open Interfaces (interfaces that delegate implementation details to external components).

When to use delegation pattern:

There is a need to set standards with a centralized team responsible

Users have strong requirements to customize certain local implementations

Three steps to implement:

Confirm the pattern is needed

Extract the core process

Abstract open interfaces

4. Industry Case Studies

JDBC: JDBC (Java Database Connectivity) was designed to allow unified database access interfaces. The core path includes: introducing JDBC implementation package, registering driver, establishing connection, creating statement, executing and reading results, processing ResultSet, and closing resources.

Apache Dubbo: Dubbo's core path involves serialization, network requests, deserialization, provider computation, result serialization, and response. Dubbo uses a "similar to SPI" mechanism with @SPI, @Adaptive, and @Activate annotations for extensibility.

LOG4J: LOG4J uses a "reverse delegation" mechanism. When both log4j-over-slf4j and slf4j-log4j are included simultaneously, it causes a StackOverflowError due to the delegation loop.

5. Summary

The delegation pattern's use cases: 1) Need for centralized standards with a responsible team; 2) Strong user demand for customizing implementations. Core points: Core Path + Open Interfaces. Hidden mechanism: Implementation registration/discovery.

design patternssoftware architectureJDBCSlf4jApache Dubbodelegation patternJava LoggingSPI Mechanism
vivo Internet Technology
Written by

vivo Internet Technology

Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.

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.