I. Overview
Concept
In fact, to give a life example, adapter mode can be analogous to adapters, such as Typec and USB adaptor, the original can only be connected to Typec interface, expand to be able to connect to ordinary USB; Here the adaptor on the one hand need to check on the Typec, on the one hand need as a USB jack.
If you want to cite a Java example, take a look at the JDBC Chapter! A simplified UML diagram for JDBC (the middle of the xxx JDBC is our adapter for JDBC and the major database vendors!) )
Thumbnail
class's Adapter mode
The adapter mode of the object (note the relationship of the class, above is the inheritance, the following is the delegation)
Second, practice
For examples of the adapters mentioned above, we refer to: http://www.cnblogs.com/V1haoge/p/6479118.html
Of course, this example is essentially the same as the example of the above-mentioned adapter (just the way the interface is separated by function)
class, we write the characters in the thumbnail
Target role
/** * Target interface * Administrator * Date: 2017/10/28 * */publicinterface Target {/** * Target operation 1 * /void operation1 () ; /** * Target operation 2 * /void operation2 ();}
SOURCE role
/** * Source Role * Administrator * Date: 2017/10/28 * */publicclass adaptee {public void Operation1 () { System.out.println("Adaptee#opetion1 ()");} }
Adapter
/** * Adapter Role * Administrator * Date: 2017/10/28 * */publicclassextendsimplements target{ @Override publicvoid Operation2 () { System.out.println ("Adapter#operation2 ()");} }
Client uses
Public class Client { publicstaticvoid main (string[] args) { new Adapter (); T.operation2 (); }}
The adapter for the object we look at the core of the adapter role
Adapter
/*** Adapter Role * Administrator * Date: 2017/10/28 **/ Public classAdapterImplementstarget{Privateadaptee adaptee; PublicAdapter (adaptee adaptee) { This. Adaptee =adaptee; } @Override Public voidOperation1 () {adaptee.operation1 (); } @Override Public voidOperation2 () {System.out.println ("Adapter#operation2 ()"); }}
Java design mode (7)--Adapter mode for structured mode