Junior — Middle
What is the difference between Proxy and Adapter patterns in design patterns?
sobes.tech AI
Answer from AI
The main difference between the Proxy and Adapter patterns lies in their purpose and how they interact with objects:
-
Proxy — provides a surrogate object that controls access to another object. Proxy can add additional logic (such as lazy initialization, caching, access control), but the interface of the proxied object remains unchanged.
-
Adapter — transforms the interface of one class into the interface expected by the client. It is used when integrating incompatible interfaces.
Example:
- Proxy: a class that controls access to a database, checking user permissions before executing a query.
- Adapter: a class that allows using an old API with a new interface without changing existing code.
Thus, Proxy focuses on access control and additional logic around the object, while Adapter focuses on transforming the interface for compatibility.