(Design mode) Adapter mode, design mode adapter Mode
In the adapter mode, similar to the remote control, other different button functions (interfaces) are combined, and the remote control is used for unified encapsulation to adapt to all buttons. Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together. An existing class, but its interface does not meet my needs, is not compatible. You want to create a reusable class that can work together with other unrelated classes or unforeseen classes (that is, classes whose interfaces may not be compatible. If you want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces, the Object Adapter can adapt to its parent class interface. Existing subclass, applicable object
Public class Adaptee {public void adapteeMethod () {System. out. println ("adaptation method ");}}
Adapter Interface
Public interface Target {/*** adaptive interface */void adapteeMethod ();/*** new interface */void adapterMethod ();}
Interface implementation
Public class Adapter implements Target {private Adaptee adaptee; public Adapter (Adaptee adaptee) {this. adaptee = adaptee;} @ Override public void adapteeMethod () {this. adaptee. adapteeMethod () ;}@ Override public void adapterMethod () {System. out. println ("New Interface ");}}
Test
public static void main(String[] args) { Target target = new Adapter(new Adaptee()); target.adapteeMethod(); target.adapterMethod();}