Junior — Middle
Have you applied the Adapter pattern in your projects?
sobes.tech AI
Answer from AI
The Adapter pattern is often used in projects to integrate incompatible interfaces. For example, when you need to use a third-party class with an interface different from the expected one, an adapter is created that implements the required interface and internally calls the methods of the adapted class.
Example in Java:
// Target interface
interface Target {
void request();
}
// Class with incompatible interface
class Adaptee {
void specificRequest() {
System.out.println("Calling specific method");
}
}
// Adapter
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// Usage
public class Main {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.request(); // Calls specificRequest through the adapter
}
}