Usually we often encounter this situation, there are two of ready-made classes, there is no connection between them. But we now want to use one of the methods of the class. At the same time, you want to use a different class method. There is one solution. Changes to their respective interfaces. But this is the last thing we want to see. This time, adapter mode will come in handy.
There are three ways of adapter mode, one is object adapter, one is class adapter, one is interface adapter
The following examples illustrate:
Class Adapter Class diagram
public class DrawRectangle {//Draw party public void DrawRectangle (String msg) {System.out.println ("Draw Rectangle:" + msg);}}
Public interface Idrawcircle {//Draw round interface void Drawcircle ();}
/** * Class adapters use object inheritance in a way that is statically defined * @author Stone * */public class Drawadapter4class extends DrawRectangle implements Idrawcir CLE {//can draw square and can draw circle @overridepublic void Drawcircle () {System.out.println ("drawadapter4class:drawcircle");}}
Object Adapter Class Diagram:
/** * Object adapters: How you use object composition is a dynamic combination of methods. * Can draw the square and can draw the circle * @author Stone * Drawadapter is an adapter, DrawRectangle belongs to adapter, is the adapter, adaptor will be adapted to match the target (drawcircle) to adapt * */public Class Drawadapter4object implements Idrawcircle {//can draw square and can draw circle private DrawRectangle Drawrectangle;public Drawadapter4object (DrawRectangle drawrectangle) {this.drawrectangle = DrawRectangle;} @Overridepublic void Drawcircle () {System.out.println ("drawadapter4object:drawcircle");} public void DrawRectangle (String msg) {drawrectangle.drawrectangle (msg);}}
Interface Adapter
Class diagram
/* Interface Adapter: There are abstract methods in the interface, we just want to implement a few of them. Do not want all implementations, * so provide a default null implementation, and then inherit from it, overriding implements the method we want to implement */public interface IDraw {void drawcircle (); void DrawRectangle ();}
/* * The default implementation of the interface adapter */public class Defaultdrawadapter implements IDraw {//Draw square draw circle is empty implement @overridepublic void Drawcircle () {} @Ove rridepublic void DrawRectangle () {}}
public class Test {public static void main (string[] args) {//object adapter Drawadapter4object objadapter = new Drawadapter4object (n EW DrawRectangle ()); Objadapter.drawcircle (); Objadapter.drawrectangle ("in Drawadapter4object"); System.out.println ("--------------");//class adapter Drawadapter4class Clzadapter = new Drawadapter4class (); Clzadapter.drawcircle (); Clzadapter.drawrectangle ("in Drawadapter4class"); System.out.println ("--------------");//Interface Adapter Mydrawadapter Mydrawadapter = new Mydrawadapter (); Mydrawadapter.drawcircle (); Mydrawadapter.drawrectangle ();} Static Class Mydrawadapter extends Defaultdrawadapter {@Overridepublic void drawcircle () {System.out.println (" Drawcircle in Mydrawadapter ");}}
Print
Drawadapter4object:drawcircledraw Rectangle: In drawadapter4object--------------drawadapter4class: Drawcircledraw rectangle:in drawadapter4class--------------drawcircle in Mydrawadapter
Java Implementation Adapter (Adapter) mode