We often encounter this situation. With two ready-made classes, there is no connection between them, but now we want to use one of the methods of the class and the other. There is a workaround to modify their respective interfaces, but this is what we least want to see. At this time, Adapter mode will come in handy.
Adapter mode is also called adapter mode, which is one of 23 design modes proposed by GoF. The Adapter mode is one of the constructive modes. Through the Adapter mode, the interface form of existing classes (or external classes) can be changed.
There are three ways to adapter mode, one is object adapter, one is class adapter, and the other is interface adapter
The following examples are given:
1. Class adapter
Class diagram
public class DrawRectangle {//Draw square public void drawRectangle(String msg) { System.out.println("draw Rectangle: " + msg); } } public interface IDrawCircle {//Draw circle interface void drawCircle(); } /** * The class adapter uses object inheritance, which is a static definition method* @author stone * */ public class DrawAdapter4Class extends DrawRectangle implements IDrawCircle {// Can draw square and circle @Override public void drawCircle() { System.out.println("DrawAdapter4Class: drawCircle"); } }2. Object Adapter
Class diagram:
/** * Object adapter: The way to use object combination is a way to dynamically combine. * Can draw square and circles* @author stone * DrawAdapter is an adapter, DrawRectangle belongs to the adapter and is the adapter. The adapter will adapt to the adapter and the adapter (DrawCircle)* */ public class DrawAdapter4Object implements IDrawCircle {// Can draw square and circles private DrawRectangle drawRectangle; public DrawAdapter4Object(DrawRectangle drawRectangle) { this.drawRectangle = drawRectangle; } @Override public void drawCircle() { System.out.println("DrawAdapter4Object: drawcircle"); } public void drawRectangle(String msg) { drawRectangle.drawRectangle(msg); } } 3. Interface adapter
Class diagram
/* * Interface adapter: There are abstract methods in the interface. We only want to implement a few of them, but we don’t want to implement them all. * So provide a default empty implementation, and then inherit from it, and rewrite the method we want to implement */ public interface IDraw { void drawCircle(); void drawRectangle(); } /* * Default implementation of interface adapter*/ public class DefaultDrawAdapter implements IDraw {//Implementation of square and circles @Override public void drawCircle() { } @Override public 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 { @Override public void drawCircle() { System.out.println("drawCircle in MyDrawAdapter"); } } }DrawAdapter4Object: drawcircle draw Rectangle: in DrawAdapter4Object -------------- DrawAdapter4Class: drawCircle draw Rectangle: in DrawAdapter4Class -------------- drawCircle in MyDrawAdapter
Three features of adapter mode:
1 The adapter object implements the original interface
2 The adapter object combines an object that implements a new interface (this object can also not implement an interface, it is just a simple object)
3. A specific method in which the call to the adapter's original interface method is delegated to an instance of the new interface
Some people think that the examples explaining the design pattern are too simple, and it seems that way, but if you really want to use it in project development, it will really not be able to apply it. In fact, we don’t have to deliberately use design patterns in our projects, but should start from the actual design problems and use which model can solve our problems. Don’t use the pattern for the sake of using the pattern, then just skip the ground. Generally speaking, as long as you follow certain design principles, the design pattern is also summarized based on these principles. If you are familiar with these principles, the pattern will naturally exist.