Adapter mode (Adapter): Converts the interface of a class into the interface that the client wants
1. Being adapted to the object
2. Adapter
3. Target interface
is to connect the object and the target interface, all dealing with the adapter, the adaptation object and the target interface is not directly interactive, to achieve loose coupling, the adapter is like a USB and a circular port two ways of connecting cable.
The computer requires a USB keyboard, but the keyboard is round-mouth.
Notebook PC
//Computer Public classDiannao { Public voidShanchu (Target t) {//dealing with adapter interfaces rather than keyboardsT.chuli (); } Public Static voidMain (string[] args) {Diannao dn=NewDiannao (); Target T=NewAdapter (NewJianpan ()); Dn.shanchu (t); } }
Keyboard
// Keyboard is adapted to the object Public class Jianpan { publicvoid Dazi () { System.out.println ("press DELETE");} }
Adapter interface, and a layer of abstraction
Public Interface Target { void Chuli ();}
Implementation classes for adapters
//Adapter Public classAdapterImplementstarget{//and be associated with the adapted object.//1 Inheriting the keyboard class adapter mode//2 Combo Keyboard object (recommended) object Adapter mode PrivateJianpan Jianpan; Public voidChuli () {Jianpan.dazi (); } PublicAdapter (Jianpan jianpan) { This. Jianpan =Jianpan; }}
It is recommended to use the above object to fit
As an example, the flow wrapper class is an adaptation process.
Java design mode--Adapter mode