Provide an intermediary object that refers to another object. The proxy pattern uses a class named proxy class to represent the functionality of another class.
Types of Proxies
Proxies are classified into three categories. They are as follows.
Virtual Proxy: A virtual proxy serves as a placeholder for items that are "expensive to create". The actual object is only created when a client first requests or accesses it.
Remote Proxy: A remote proxy provides local representation for an object that is located in a separate address space.
Protection Proxy: A protection proxy limits access to a sensitive master object. Before sending the request, the surrogate object ensures that the caller has the necessary access permissions.
Examples of Proxy Patterns
Examples of proxy patterns include:
Payment Processing
Accessing Remote Service
Accessing Remote Database
Why do we need the C# Proxy Design Pattern?
The Proxy server has two main purposes. They are as follows:
Improve Performance with Proxy Servers
Filter requests using a proxy server.
Proxy Design Pattern with UML Diagram
The UML class diagram above includes the following classes, interfaces, and objects:
Subject: This interface defines members that will be implemented by the RealSubject and Proxy classes.
RealSubject: This is a class that we wish to use more efficiently with the proxy class.
Proxy: This is a class that contains an instance of the RealSubject class and can access its members as needed.
When to use the Proxy Design pattern?
The proxy design pattern is applicable in the following situations:
Objects must be produced on-demand, meaning when their activities are requested.
Access control to the original object is required.
Allow access to a remote object via a local object (which will refer to the remote object).