Junior — Middle
How to distinguish the roles and differences of the Adapter and Facade patterns in software design?
sobes.tech AI
Answer from AI
Adapter and Facade patterns solve different problems, although both serve to simplify interaction with other classes.
Adapter — a structural pattern that allows objects with incompatible interfaces to work together. It "wraps" one interface into another so that the client can use the class without changes.
Facade — provides a simplified interface to a complex system of classes or subsystem. It hides complexity and offers a convenient interface for the client.
Main differences:
- Adapter transforms one class's interface into the interface expected by the client.
- Facade provides a simple interface to a complex system without changing internal class interfaces.
Example:
- Adapter: you have a class with method
specificRequest(), but the client expectsrequest(). The adapter implementsrequest()by callingspecificRequest(). - Facade: you have several classes with different methods, and the facade combines their calls into one method for client convenience.