dev-resources.site
for different kinds of informations.
Adapter Pattern
Published at
9/27/2020
Categories
structural
pattern
designpatterns
java
Author
eidher
Main Article
Author
6 person written this
eidher
open
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Participants
- Target: defines the domain-specific interface that Client uses.
- Adapter: adapts the interface Adaptee to the Target interface.
- Adaptee: defines an existing interface that needs adapting.
- Client: collaborates with objects conforming to the Target interface.
Code
public class Main {
public static void main(String[] args) {
Target target = new Adapter();
target.request();
}
}
public interface Target {
void request();
}
public class Adapter implements Target {
Adaptee adaptee = new Adaptee();
@Override
public void request() {
adaptee.specificRequest();
}
}
public class Adaptee {
void specificRequest() {
System.out.println("Called specificRequest()");
}
}
Output
Called specificRequest()
structural Article's
11 articles in total
Top Architect and Structural Engineer for Custom Designs
read article
Proxy
read article
PRISTON STRUCTURAL SYSTEMS
read article
Proxy Pattern
read article
Flyweight Pattern
read article
Facade Pattern
read article
Composite Pattern
read article
Bridge Pattern
read article
Adapter Pattern
currently reading
Decorator Pattern
read article
Main Design Patterns
read article
Featured ones: