Fundamentals 6 min read

Understanding the Proxy Design Pattern with Java Examples

This article explains the Proxy design pattern, illustrates its core participants with clear Chinese idiom analogies, and provides a complete static‑proxy implementation in Java—including interface, real subject, proxy class, and client code—while also noting the distinction between static and dynamic proxies.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding the Proxy Design Pattern with Java Examples

In a series of articles about the 23 classic design patterns, the author uses Chinese idioms to help remember each pattern; for example, "一呼百应" for Observer and "聚沙成塔" for Builder.

The Proxy pattern, a structural pattern, is defined as providing a surrogate or placeholder for another object to control access to it. In Chinese: 为其他对象提供一种代理,以控制对这个对象的访问。

The pattern involves three roles: Subject (a common interface for real and proxy objects), Real Subject (the actual object containing core business logic), and Proxy (holds a reference to the real subject and adds extra behavior before or after delegating calls).

An image‑loading scenario is used to illustrate how a proxy can add functionality such as logging or lazy initialization around the actual image loading process.

Below are the complete Java code snippets for a static proxy implementation:

package com.sample.patterns.proxy;
// 图片抽象主题
public interface ImageSubject {
    void request();
}
package com.sample.patterns.proxy;
// 大图片加载,真实主题对象
public class LargeImageSubject implements ImageSubject {
    @Override
    public void request() {
        // 实际的业务逻辑处理
        System.out.println("Large image loaded!");
    }
}
package com.sample.patterns.proxy;
// 代理对象
public class ImageSubjectProxy implements ImageSubject {
    // 持有一个真实对象的引用
    private LargeImageSubject largeImageSubject;
    @Override
    public void request() {
        if (largeImageSubject == null) {
            largeImageSubject = new LargeImageSubject();
        }
        // 调用真实对象的处理方法前后,可以做一些增强操作
        System.out.println("Do something before invoke");
        largeImageSubject.request();
        System.out.println("Do something after invoke");
    }
}
package com.sample.patterns.proxy;
// 客户端与代理对象进行交互
public class Client {
    public static void main(String[] args) {
        // 创建一个代理对象
        ImageSubject imageSubject = new ImageSubjectProxy();
        imageSubject.request();
    }
}

Running the Client prints messages showing that the real subject's method is executed with additional behavior added by the proxy.

The article notes that this example demonstrates a static proxy, where the proxy class is written and compiled beforehand; dynamic proxies, which are generated at runtime using reflection (JDK or CGLIB), will be covered in a future post.

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.

Design PatternsJavaSoftware ArchitectureProxy PatternStatic Proxy
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.