Adapter Mode(Adapter pattern) is sometimes called a packaging style or packaging. Transfers the interface of a class to what the user expects. An adaptation means that a class cannot work together because the interface is incompatible. The method is to wrap its own interface in an existing class.
There are two types of adapter modes:
· Object Adapter mode-in this adapter mode, the adapter holds an instance of the class that it wraps. In this case, the adapter calls the physical entity of the wrapped object.
· Class adapter mode-in this adapter mode, the adapter inherits its own implemented classes (generally multiple inheritance ).
Next we can look at the class diagram:
The following is a small example. If your friend brings you an original Japanese camera from Japan, you can only use the V voltage for charging, if you want to use it in China, you must use a voltage converter to convert the domestic 220v voltage to 110v voltage. For example
The voltage of 220v is converted to 110v.
Start code:
Customers: Cameras
public class Camera {public void charging(String strVoltage) {if (strVoltage.equalsIgnoreCase("110V")) {System.out.println("can charge");} else {System.out.println("the Voltage is not in conformity with the Camera");}}}
The charging function is simply implemented here!
Adapter: V China Voltage
Public class chinavoltage {string getvoltage () {system. Out. println ("China voltage is 220 V"); Return "220 V ";};}
Adapter: 110 V
public class JapanVoltage {private String strVoltage = "110V";public String getVoltage() {System.out.println("Japan Voltage is 110V");return strVoltage;}}
There is no interface defined here, and the object class is used directly!
Adapter:
public class VoltageAdapter extends ChinaVoltage{private JapanVoltage japanVoltage;public void setJapanVoltage(JapanVoltage japanVoltage) {this.japanVoltage = japanVoltage;}@Overridepublic String getVoltage() {// TODO Auto-generated method stubreturn japanVoltage.getVoltage();}}
Test cases:
Public static void main (string [] ARGs) {// obtain the camera = new camera (); // obtain the Japanese voltage and converter japanvoltage = new japanvoltage (); voltageadapter = new voltageadapter (); // converts it to a Japanese voltageadapter. setjapanvoltage (japanvoltage); // charge camera. charging (voltageadapter. getvoltage (); // directly use the Chinese voltage to charge camera. charging (New chinavoltage (). getvoltage ());}
Test results:
Japan voltage is 110 V
Can Charge
China's voltage is 220 V
The voltage is not in conformity with the camera
The Object Adapter mode is used here. in Java, multi-inheritance is not supported, so there is basically no way to inherit classes.
But try using the interface to add objects:
public interface IJapanVoltage {String getJapanVoltage();}
Adapter:
Public class voltageadapter2 extends chinavoltage implements ijapanvoltage {@ overridepublic string getjapanvoltage () {getvoltage (); system. out. println ("converted to 110 V"); Return "110 V ";}}
Then test:
Public class test {public static void main (string [] ARGs) {// get the camera = new camera (); // obtain the Japanese voltage and converter japanvoltage = new japanvoltage (); voltageadapter2 voltageadapter = new voltageadapter2 (); // charge camera. charging (voltageadapter. getjapanvoltage (); // directly use the Chinese voltage to charge camera. charging (voltageadapter. getvoltage ());}}
Test results:
China's voltage is 220 V
Converts the voltage to 110 V in Japan.
Can Charge
China's voltage is 220 V
The voltage is not in conformity with the camera
Applicability:
1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.
2. Create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces.
3. (For object adapters) in the design, you need to change the interfaces of multiple existing sub-classes. If you use the class adapter mode, you need to create an adapter for each sub-class, which is not practical.
Effects and advantages and disadvantages:
For Class adapters:
1. Use a specific adapter class to match adaptee and taget. The result is that when we want to match a class and all its subclasses, the class adapter will not be competent.
2. Make the adapter override (redefinition) partial behavior of adaptee, because the adapter is a subclass of adaptee.
For object adapters:
1. Allow an adapter to work with multiple adaptee, that is, the adaptee itself and all its subclasses (if any. The adapter can also add functions to all adaptee at a time.
2. Making override (redefinition) adaptee behavior more difficult. If you need the override adaptee method, you have to first create an adaptee subclass using the override adaptee method, and then use this subclass as a real adaptee source for adaptation.
Finally: The graph used in this article is taken from the head. First design pattern book. If you want to learn the design model well, you can take a good look at the book.
Have a good weekend...
If you think it is okay, please help me with it...