1. Definition
The interface of a class is transformed into another interface expected by the client, so that the two classes that cannot work together due to interface mismatch can work together. The adapter mode is also called the transformer mode, or wrapper mode.
2. generic class diagram
Target role: this role defines the interface for converting other classes, that is, our expected interface.
Adaptee source role: who you want to convert to the target role? The "who" is the original role. It is an existing and running class or object packaged by the adapter role, it will become a brand new and beautiful role.
Adapter adapter role: the core role in the adapter mode. The other two roles are existing roles, and the adapter role needs to be created. This role has simple responsibilities, the source role is converted to the target role by means of integration or class association.
3. Code List
Package adapter;/*** target role * 2:13:53-limiracle */public interface target {// The target role has its own method public void request ();}
Package adapter;/*** implementation class of the target role * 2:15:21-limiracle */public class concretetarget implements target {public void request () {system. out. println ("= ");}}
Package adapter;/*** source role * 2:16:04-limiracle */public class adaptee {// original business logic public void dosomething () {system. out. println ("********");}}
Package adapter;/*** adapter role * 2:17:23-limiracle */public class adapter extends adaptee implements target {// target implementation interface, it also calls what the source role has to do. Public void request () {super. dosomething ();}}
Package adapter; public class main {public static void main (string [] ARGs) {// original business logic Target target = new concretetarget (); target. request (); // the business logic target TARGET2 = new adapter (); target2.request () ;}} after the adapter role is added ();}}