The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. It allows for the implementation of additional functionality, such as access control, logging, or lazy loading, without altering the original object's code. In this article, we will explore the Proxy Pattern in-depth, examining its structure, advantages, and providing practical examples in C#.
Understanding the Proxy Pattern
The Proxy Pattern involves the following key components:
- Subject: The interface or abstract class that declares the common interface for the RealSubject and Proxy.
- RealSubject: The class that defines the real object whose behavior the proxy controls or extends.
- Proxy: The class that acts as a surrogate for the RealSubject. It implements the same interface as the RealSubject and can control access to it.
- Client: The class that interacts with the Proxy or RealSubject through the common interface.
Implementation in C#
Let's delve into a simple example of the Proxy Pattern in C#. Suppose we have a scenario where we need to control access to an expensive image loading operation. The Proxy Pattern can be applied to implement a proxy that loads the image lazily.
// Step 1: Define Subject interface
public interface IImage
{
void Display();
}
// Step 2: Implement RealSubject
public class RealImage : IImage
{
private readonly string fileName;
public RealImage(string fileName)
{
this.fileName = fileName;
LoadImage();
}
private void LoadImage()
{
Console.WriteLine($"Loading image: {fileName}");
// Simulate expensive image loading operation
}
public void Display()
{
Console.WriteLine($"Displaying image: {fileName}");
}
}
// Step 3: Implement Proxy
public class ImageProxy : IImage
{
private RealImage realImage;
private readonly string fileName;
public ImageProxy(string fileName)
{
this.fileName = fileName;
}
public void Display()
{
if (realImage == null)
{
realImage = new RealImage(fileName);
}
realImage.Display();
}
}
// Step 4: Implement Client
public class Client
{
private readonly IImage image;
public Client(IImage image)
{
this.image = image;
}
public void DisplayImage()
{
image.Display();
}
}
In this example, IImage
is the subject interface that declares the common interface for RealImage
and ImageProxy
. RealImage
is the real subject class that performs the actual image loading and displaying. ImageProxy
is the proxy class that controls access to the real subject. Client
is the class that interacts with either the real subject or the proxy through the common interface.
Advantages of the Proxy Pattern
1. Controlled Access: The Proxy Pattern allows for controlled access to the real object. The proxy can implement additional functionality, such as access control or logging, before delegating to the real object.
2. Lazy Loading: The pattern supports lazy loading, where the real object is created and initialized only when needed. This can be beneficial for resource-intensive operations.
3. Security Enhancement: Proxies can enforce security measures, such as authentication or authorization, before granting access to the real object.
4. Separation of Concerns: The pattern promotes the separation of concerns by allowing the proxy to handle non-core concerns (e.g., logging, access control), leaving the real object focused on its primary functionality.
Real-world Examples
1. Virtual Proxy for Large Images
In a graphic editing application, the Proxy Pattern can be applied to implement a virtual proxy for large images. The virtual proxy loads only the visible portion of a large image, providing a responsive user experience without loading the entire image into memory.
// Simplified example in C#
public interface ILargeImage
{
void Display();
}
public class RealLargeImage : ILargeImage
{
private readonly string fileName;
public RealLargeImage(string fileName)
{
this.fileName = fileName;
LoadLargeImage();
}
private void LoadLargeImage()
{
Console.WriteLine($"Loading large image: {fileName}");
// Simulate loading a large image into memory
}
public void Display()
{
Console.WriteLine($"Displaying large image: {fileName}");
}
}
public class VirtualImageProxy : ILargeImage
{
private RealLargeImage realImage;
private readonly string fileName;
public VirtualImageProxy(string fileName)
{
this.fileName = fileName;
}
public void Display()
{
if (realImage == null)
{
realImage = new RealLargeImage(fileName);
}
realImage.Display();
}
}
2. Access Control Proxy for Sensitive Data
In a system handling sensitive data, the Proxy Pattern can be applied to implement an access control proxy. The proxy checks the user's credentials before allowing access to sensitive data.
// Simplified example in C#
public interface ISensitiveData
{
void AccessData();
}
public class RealSensitiveData : ISensitiveData
{
public void AccessData()
{
Console.WriteLine("Accessing sensitive data");
}
}
public class AccessControlProxy : ISensitiveData
{
private readonly RealSensitiveData realData;
private readonly string username;
private readonly string password;
public AccessControlProxy(string username, string password)
{
realData = new RealSensitiveData();
this.username = username;
this.password = password;
}
public void AccessData()
{
if (Authenticate())
{
realData.AccessData();
}
else
{
Console.WriteLine("Access denied. Invalid credentials.");
}
}
private bool Authenticate()
{
// Implement authentication logic
return username == "admin" && password == "secret";
}
}
Conclusion
The Proxy Pattern is a versatile tool for controlling access to objects and adding additional functionality transparently. Through practical examples in C#, we have demonstrated how the Proxy Pattern can be applied to real-world scenarios, providing a blueprint for creating systems that enhance security, improve performance, or enable lazy loading. Understanding and incorporating this pattern into your design practices can contribute to building modular, secure, and efficient software architectures, ensuring optimal control over access to sensitive resources in your applications.