Use
Adapter mode (Adapter)
Transforms the interface of one class into another interface that the customer wants.
The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. Adapter mode is a
Structural mode。
structure
figure-Adapter pattern structure diagram
Target : Defines the interface that the user needs to use.
Abstract classTarget {
PublicAbstractvoidRequest ();
}
adaptee : Define an interface that needs to be adapted
classAdaptee {
PublicvoidSpecificrequest () {
SYSTEM.OUT.PRINTLN ("special Request");
}
}
Adapter : The source interface is converted to the target interface by wrapping a Adaptee object inside.
classAdapterextendsTarget {
PrivateAdaptee adaptee =NewAdaptee ();
@Override
PublicvoidRequest () {
Adaptee. Specificrequest ();
}
}
Client : The object that needs to call our code.
PublicclassAdapterpattern {
PublicStaticvoidMain (string[] args) {
Target target =NewAdapter ();
Target. Request ();
}
}
Application Scenarios
Want to use a class that already exists, but if its methods do not meet the requirements;
Two classes have the same or similar responsibilities, but use them when they have different interfaces;
You should use adapter mode adaptation when both sides are not easy to modify, instead of using it when it is different.
JAVA Design Mode Adapter mode