Design Patterns:
Proxy
Proxy is a structural pattern that lets you put a placeholder between the clients and a real object.
Problem
Imagine an application where there’s a client that communicates with an object that’s very heavy in terms of resources. You may want to implement lazy loading but it could lead to code duplication. Alternatively, the object might require pre-processing or post-processing.
Solution
This is where the proxy pattern comes into play. With this pattern, the client interacts with the proxy, which then communicates with the real object, potentially performing additional tasks before returning the result to the client.
In this scenario, there are three key components: the client, the proxy, and the real object. Both the proxy and the real object should implement the same interface, ensuring that the client remains unaware of which object it is communicating with.
classDiagram
accTitle: UML class diagram of the Proxy pattern
accDescr: UML class diagram of the Proxy pattern: Client uses the ObjectInterface interface and its doSomething() method. Both RealObject and Proxy implement that interface; Proxy receives an ObjectInterface in its constructor, keeps it as service and delegates doSomething() to it.
direction BT
class Client
class ObjectInterface {
<<interface>>
+doSomething()
}
class Proxy {
-service: ObjectInterface
+Proxy(s: ObjectInterface)
+doSomething()
}
class RealObject {
+doSomething()
}
Client --> ObjectInterface
Proxy ..|> ObjectInterface
RealObject ..|> ObjectInterface
Proxy o--> ObjectInterfaceReal-World example
Imagine you have a remote service that fetches data from a server. Accessing this service might be slow or expensive, so you want to add some logging to see when it’s being accessed. Instead of modifying the service itself, you can use a proxy to handle the logging.
classDiagram
accTitle: UML class diagram of the Proxy example with a remote data service
accDescr: UML class diagram of the example: Client uses the DataService interface and its fetchData() method. RealDataService and DataServiceProxy both implement it; DataServiceProxy receives a RealDataService in its constructor and keeps it as service.
direction BT
class Client
class DataService {
<<interface>>
+fetchData()
}
class DataServiceProxy {
-service: RealDataService
+DataServiceProxy(s: RealDataService)
+fetchData()
}
class RealDataService {
+fetchData()
}
Client --> DataService
DataServiceProxy ..|> DataService
RealDataService ..|> DataService
DataServiceProxy o--> RealDataServiceJava example
| |
Applications
The proxy pattern can be applied in various ways:
- Remote Proxy: When the real object is located remotely, and the proxy makes a remote call to fetch it.
- Virtual Proxy: When you want to lazily load a resource-intensive object.
- Protection Proxy: When you need to perform security checks before interacting with or returning the real object.
- Caching Proxy: When you need to cache the real object to avoid fetching it repeatedly.
- Logging Proxy: When you need to add logging before or after interacting with the real object.
Tips
- It is not always possible to extract a common interface from a pre-existing class, especially if doing so requires altering all client code or if the class belongs to a third-party library. In the first case, consider making the proxy a subclass. In the latter case, consider using the adapter pattern.
COMMENTSCOMMENTS