Adapter mode in Java
We often encounter such a situation that there are two ready-made classes with no connection between them, but now we want to use one of the class methods, I also want to use another class method. One solution is to modify their respective interfaces, but this is what we are most reluctant to see. At this time, the Adapter mode will be used.
There are three adapter modes: Object Adapter, class adapter, and interface adapter.
The following is an example:
Public class DrawRectangle {// specifies the public void drawRectangle (String msg) {System. out. println ("draw Rectangle:" + msg );}}
Public interface IDrawCircle {// void drawCircle ();}
/*** Class adapter uses the Object Inheritance Method, is a static definition method * @ author stone **/public class DrawAdapter4Class extends DrawRectangle implements IDrawCircle {// draw both parties and circles @ Overridepublic void drawCircle () {System. out. println ("DrawAdapter4Class: drawCircle ");}}
/*** Object Adapter: the object combination is a dynamic combination. * Drawing both parties and circles * @ author stone * DrawAdapter is the adapter, DrawRectangle is the adapter, and is the adapter. The adapter will be the adapter and the matching target (DrawCircle) adapt **/public class DrawAdapter4Object implements IDrawCircle {// draw both parties and circles 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
/** Interface adapter: There are abstract methods in the interface. We only want to implement a few of them and do not want to implement them all. * therefore, we provide a default empty implementation and inherit from it, override the method we want to implement */public interface IDraw {void drawCircle (); void drawRectangle ();}
/** Default Implementation of the interface adapter */public class DefaultDrawAdapter implements IDraw {// Implementation of Overridepublic void drawCircle () {}@ Overridepublic void drawRectangle () {}}
Public class Test {public static void main (String [] args) {// Object Adapter DrawAdapter4Object objAdapter = new DrawAdapter4Object (new 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