February 2, 2024   -   David Oyinbo

Overview of The Proxy Design Pattern

A simplified overview of the proxy design pattern. Example given in Java

Design PatternsSoftware DevelopmentCodingProxyJavaArchitecture

Overview

The proxy design pattern is a structural design pattern that provides a surrogate or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

The proxy pattern is often used to:

  • Control access to an object - For example, you could use a proxy to restrict access to a database object based on the user's credentials.
  • Lazy load an object - This means that the object is not created until it is actually needed. This can be useful for objects that are expensive to create or that are only used occasionally.
  • Cache the results of an expensive operation - This can improve performance by avoiding the need to repeat the operation each time it is requested.

Implementation

The proxy pattern is implemented by creating a proxy class that implements the same interface as the original object. The proxy class then delegates all requests to the original object, but it can also perform additional tasks before or after the request is processed.

Here is an example of the proxy design pattern:

ProxyExample.javajava
class Image {
  public void display() {
    // Do something to display the image.
  }
}

class ProxyImage {
  private Image image;
  
  public ProxyImage(Image image) {
    this.image = image;
  }
  
  public void display() {
    // Do something before displaying the image.
    image.display();
    // Do something after displaying the image.
  }
}

In this example, the ProxyImage class is a proxy for the Image class. The ProxyImage class delegates all requests to the Image class, but it can also perform additional tasks before or after the request is processed.

The proxy design pattern is a powerful tool that can be used to solve a variety of problems. It is a versatile pattern that can be used in a wide range of applications.

Comparison with the Mediator Design Pattern

The proxy design pattern is often confused with the mediator design pattern. However, the two patterns are quite different.

The proxy design pattern is used to control access to an object or to provide a surrogate for an object. The mediator design pattern is used to decouple components in a system by providing a central communication channel.

Key Differences:

  • Proxy Design Pattern: The proxy object is a direct replacement for the real object. The proxy object delegates all requests to the real object.
  • Mediator Design Pattern: The mediator object is not a direct replacement for the real objects. The mediator object simply provides a communication channel between the real objects.

The following table summarizes the key differences between the proxy design pattern and the mediator design pattern:

AspectProxy Design PatternMediator Design Pattern
PurposeControls access to an object or provides a surrogateDecouples components by providing central communication
RelationshipDirect replacement for the real objectCommunication channel between objects
Object InteractionProxy delegates all requests to real objectMediator facilitates interaction between multiple objects
Use CasesAccess control, lazy loading, cachingLoose coupling, centralized communication
StructureOne-to-one (proxy to real object)One-to-many (mediator to multiple objects)

Conclusion

The proxy design pattern is a powerful tool that can be used to solve a variety of problems. It is a versatile pattern that can be used in a wide range of applications.

If you are looking for a way to control access to an object, to provide a surrogate for an object, or to lazy load an object, then the proxy design pattern is a good option to consider.


Originally published on Medium by David Oyinbo

Related Logs

April 21, 2024

Understanding the Client-Server Model in Distributed Computing

Exploring the fundamental architecture in distributed computing that facilitates efficient allocation of tasks and workloads between clients and servers through network communication

distributed-computingclient-servernetworking
View log
February 2, 2024

Overview of Lifetimes in Rust: A Guide for Developers

A comprehensive guide to understanding Rust's lifetime system and how it ensures memory safety without a garbage collector.

rustlifetimesmemory-safety
View log
April 28, 2024

Is Programming Enough? The Tales of a Software Engineer

Exploring the essential soft skills beyond programming that software engineers need to succeed in the business world - from communication and time management to collaboration and problem-solving

software-engineeringcareer-developmentsoft-skills
View log

Let's build something together

Available for senior engineering roles, consulting, and architecture reviews.

© 2025 David Oyinbo