Proxy Pattern

  • Software Design Pattern

The Proxy design pattern provides a surrogate or placeholder for another object to control its access. It acts as an intermediary between the client and the real object, allowing additional operations to be performed before or after accessing the real object. The Proxy pattern is useful in situations where you want to add functionality or control access to an object without modifying its original implementation.

Attributes:

• Name: Proxy

• Type: Structural pattern

• Purpose: Control access to an object by providing a surrogate or placeholder.

Key Participants:

• Proxy: Implements the same interface as the real object and maintains a reference to it. It controls access to the real object and can add additional behavior.

• RealObject: Represents the actual object that the proxy provides access to.

• Client: Interacts with the Proxy to perform operations on the RealObject.

Example:

Explanation:

In the above example, we have an Image interface that defines the common operations. The RealImage class represents the actual image and performs the loading and displaying operations. The ImageProxy acts as a proxy for the RealImage. When the display operation is called on the proxy, it checks if the real image is already loaded. If not, it creates an instance of RealImage and then delegates the display operation to it.

Benefits:

• Allows for additional functionality to be added without modifying the real object.

• Controls access to the real object, providing a level of indirection.

• Provides a way to manage the creation and lifecycle of the real object.

Drawbacks:

• Introduces an additional layer of complexity.

• Can impact performance if there are frequent calls through the proxy.

Note: The Proxy pattern can be applied in various contexts, such as remote proxies, virtual proxies, protection proxies, etc., each with its own specific implementation and use cases.


Name

Proxy Pattern

Description

The Proxy design pattern provides a surrogate or placeholder for another object to control its access.