I. Introduction to Adapter Mode
The simplest way for a class in Java to use another object that is irrelevant to itself is to use the adapter pattern.
The following is a simple code implementation:
Package Com.lz.adapter; /* */Publicclass adaptee {publicvoid request () { System.out.println ("provides related services. "); }}
PackageCom.lz.adapter;/** Adapters with adapter interface are implemented*/ Public classAdapterImplementstarget{/** methods that are called by the member variable to be adapted to the object*/ Privateadaptee adaptee; PublicAdapter (adaptee adaptee) {Super(); This. Adaptee =adaptee; } @Override Public voidHandlereq () {adaptee.request (); }}
Package Com.lz.adapter; /* */Publicinterface Target {void handlereq ();}
here is like the computer wants to use the mouse to need the USB or the PS/2 interface to link the two, here the adapter will play a the role of the connection.
Java design mode GOF23 05 Adapter mode