Introducing the Proxy design pattern

Introducing the Proxy design pattern

Read Time : 2 Minutes

Sunday, 16 April 2023

Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Proxy design pattern.

Proxy pattern is a structural pattern that provides a substitute or placeholder for another object through which that object can be controlled. This pattern implements a mechanism that acts as an intermediary between the client and a target object and allows the client to communicate with the target object indirectly through the Proxy object. The main purpose of this pattern is to provide access to control an object without using it directly.

 

The proxy design pattern consists of three components:

1. Proxy: This object is a substitute for the original object that acts as a substitute for the desired object. This object has the same features as the main object, and by using them, commands are transferred to the main object.

2. Target: This object is the main object that the proxy modifies.

3. Client: This is the object that interacts with the proxy to access the desired object.

proxy design pattern

 

Here is an example of a proxy pattern in C#:

public interface IImage
{
    void Display();
}
public class RealImage : IImage
{
    private string _filename;
    public RealImage(string filename)
    {
        _filename = filename;
        LoadFromDisk();
    }
    public void Display()
    {
       Console.WriteLine($"Displaying {_filename}");
    }
    private void LoadFromDisk()
    {
       Console.WriteLine($"Loading {_filename} from disk");
    }
}
public class ProxyImage : IImage
{
    private RealImage _realImage;
    private string _filename;
    public ProxyImage(string filename)
    {
        _filename = filename;
    }
    public void Display()
    {
        if (_realImage == null)
        {
            _realImage = new RealImage(_filename);
        }
        _realImage.Display();
    }
}
public class Client
{
    private IImage _image;
    public Client(IImage image)
    {
        _image = image;
    }
    public void DisplayImage()
    {
        _image.Display();
    }
}
// Usage
var proxyImage = new ProxyImage("test.jpg");
var client = new Client(proxyImage);
client.DisplayImage();

In this example, IImage is the interface that the proxy and the actual image implement. RealImage is the target object that loads an image from disk and displays it. ProxyImage is a proxy object that binds to the real image. When the Display method is called on the proxy object, it checks to see if the actual image has been created yet. If not, it creates the actual image and then calls the Display method on it.

Finally, we can execute the desired processes through the Client.

 

 

To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.

design patterns

Creational:Factory Design Pattern
Builder Design Pattern
Singleton Design Pattern
Prototype Design Pattern
Abstract Factory Design Pattern
Structural:Adapter Design Pattern
Bridge Design Pattern
Composite Design Pattern
Decorator Design Pattern
Facade Design Pattern
Flyweight Design Pattern
Proxy Design Pattern
Behavioral:Chain of Responsibility Design Pattern
Command Design Pattern
Interpreter Design Pattern
Mediator Design Pattern
Memento Design Pattern
Observer Design Pattern
State Design Pattern
Strategy Design Pattern
Template Method Design Pattern
Visitor Design Pattern
Iterator Design Pattern
  • Share:
reza babakhani
Reza Babakhani

I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.

Latest post

Service Mesh Simplifying Microservice Communication

In the ever-evolving landscape of software development, microservices architecture has gained considerable popularity due to its scalability, flexibility, and extensibility. However, as the number of microservices in an application increases, it becomes increasingly challenging to manage their communication and ensure that they are all properly visible. This is where Service Mesh comes into play.

The Importance of Edge Computing

Due to the speed of technological evolution, one of the concepts that has attracted a lot of attention and changes the way we interact with digital systems is edge computing.

What is event-driven architecture?

Event-driven architecture (EDA) is a software design pattern that has become increasingly popular in modern software development. In this architecture, the flow of data is determined by the occurrence of events. Unlike traditional centralized systems that are constantly checking for new status. Event-driven architecture is especially useful for systems that need to process large amounts of data in real-time.

leave a comment