Problem Introduction: for example, if there are A-type and B-type nuts, you can directly use A-type screws produced by A-type nuts on a-type nuts, you can also directly use B-type screws produced according to B-type Nut standards on B-type nuts. However, because a-type nut and B-type nut have different standards, you cannot directly use B-type screws on A-type nut, and vice versa. How can we achieve this goal?
You can solve this problem by using an adapter: produce a "A-type nut adapter". The front end of this a-type nut adapter complies with the-type nut standard and can be screwed to A-type nut, A type-B nut is welded at the backend. In this way, the user can use a type screw on a type nut with the help of a type nut adapter.
The adapter mode, also known as the package, is used to convert an interface of a class into another interface that the customer wants. This makes it possible for classes that cannot work together due to interface incompatibility. The key to the adapter mode is to create an adapter that implements the Target Interface and contains references from the adapter.
Three roles in the adapter mode:
1. Goal: The target is an interface that the customer wants to use.
2. Adapter: the adapter is an existing interface or abstract class. This interface or abstract class needs to be adapted.
3. Adapter: the adapter is a class that implements the Target Interface and contains references from the adapter. That is, the adapter is responsible for adapting the adapter interface or abstract class to the target interface.
Here is a simple question to describe the roles involved in the adapter mode.
The user already has a hatchback outlet, but recently the user has a new hatchback outlet. The user has a washing machine and a TV. The washing machine is a three-compartment plug, while the TV is a hatchback plug. Now, the user wants to use the washing machine and TV through a dedicated three-compartment outlet, that is, to enable the current for the washing machine and TV.
To solve the above problems, use the adapter mode to design several classes.
1. Objectives
This problem is to use a three-compartment outlet to connect the current to the TV and washing machine, so the target is a three-compartment outlet. Set the sedan socket as an interface:
1 package com. adatpe; 2 3 // adaptation target: Three-phase socket 4 public interface threeelectricoutlet {5 void connectelectrelectriccurrent (); 6}
2. Adapter
For this problem, the user wants to use the sedan socket to connect the current to the TV with the hatchback plug. Therefore, the adapter should be a hatchback socket and set it as an interface:
1 package com. adatpe; 2 3 // adapter: Two-Phase socket 4 public interface twoelectricoutlet {5 void connectelectrelectriccurrent (); 6}
3. Adapter
The adapter realizes the threeelectricoutlet of the third-compartment outlet of the target interface, and also contains the reference of twoelectricoutlet of the hatchback outlet:
1 package COM. adatpe; 2 3 // adapter: implementation target interface 4 public class threepolicicadapter implements attributes {5 // The adapter contains references from the adapter 6 private twoelectricoutlet outlet; 7 public threepolicicadapter (twoelectricoutlet outlet) {8 This. outlet = outlet; 9} 10 public void connectelectriccurrent () {11 outlet. connectelectriccurrent (); 12} 13 14}
In the following applications, application. java uses the classes involved in the adapter mode. Applications use the wash class to create an object to simulate a washing machine, and the TV class to create an object to simulate a TV.
Use the threeelectricoutlet interface variable to call the connectelectrelectriccurrent () method of the wash object, and use the adapter to call the connectelectriccurrent () method of the TV object, that is, use a three-compartment outlet to connect the current of the washing machine and the TV.
1 package COM. adatpe; 2 3 Public class application {4 public static void main (string [] ARGs) {5 threeelectricoutlet outlet; // Target Interface (three-phase socket) 6 wash = new WASH (); // Washing Machine 7 outlet = WASH; // the washing machine is inserted into the three-phase outlet for 8 systems. out. println ("using three-phase socket to connect current"); 9 outlet. connectelectriccurrent (); // The current is used to start washing clothes 10 TV = new TV (); // The TV 11 threeelectricadapter adapter = new threeelectricadapter (TV ); // Insert the TV Into the adapter 12 outlet = adapter; // Insert the adapter into the third compartment socket 13 system. out. println ("Current connected using a sedan outlet"); 14 outlet. connectelectriccurrent (); // turn on the current and start playing the TV program 15} 16} 17 18 // the washing machine uses a three-phase socket 19 class wash implements threeelectricoutlet {20 private string name; 21 public wash () {22 name = "Yellow River Washing Machine"; 23} 24 public wash (string name) {25 this. name = Name; 26} 27 public void connectelectrelectriccurrent () {28 turnon (); 29} 30 public void turnon () {31 system. out. println (name + "starting to wash clothes"); 32} 33} 34 35 36 // TV uses hatchback socket 37 class TV implements twoelectricoutlet {38 private string name; 39 public TV () {40 name = "Changjiang TV"; 41} 42 Public TV (string name) {43 this. name = Name; 44} 45 public void connectelectrelectriccurrent () {46 turnon (); 47} 48 Public void turnon () {49 system. out. println (name + "start to play TV programs"); 50} 51 52}
The running result is:
Use a three-phase socket to enable current
The washing machine in the Yellow River begins to wash clothes.
Enable current using the sedan Outlet
Changjiang TV began to play TV programs
The following is an example of a two-way adapter.
In adapter mode, if the adapter role simultaneously implements the target interface and the adapter interface, and contains the reference of the target interface and the adapted interface, the adapter is a two-way adapter. Using a two-way adapter, you can use both new interfaces and existing interfaces. In the above example, if you want to have a three-compartment outlet to connect the current of the washing machine and the TV, you can use a two-way outlet to connect the current of the washing machine and the TV, then you must use a two-way adapter. The Code is as follows:
1 package COM. adatpe; 2 3 Public class extends implements libraries, 4 twoelectricoutlet {5 private extends; 6 private extends; 7 public extends (extends threeoutlet, twoelectricoutlet twooutlet) {8 outputs = threeoutlet; 9 outputs = twooutlet; 10} 11 Public outputs (twoelectricoutlet twooutlet, FIG) {12 outputs = threeoutlet; 13 twoelectricoutlet = twooutlet; 14} 15 public void connectelectrelectriccurrent () {16 if (this instanceof threeelectricoutlet) {17 twoelectricoutlet. connectelectriccurrent (); // twoelectricoutlet is the adapted interface 18} 19 if (this instanceof twoelectricoutlet) {20 threeelectricoutlet. connectelectriccurrent (); // threeelectricoutlet is the adapted interface 21} 22} 23 public static void main (string [] ARGs) {24 threeelectricoutlet threeoutlet; 25 twoelectricoutlet twoutlet; 26 wash = new WASH (); 27 TV = new TV (); 28 threeandtwo?icadapter adapter = new threeandtwo=icadapter (wash, TV); 29 threeoutlet = adapter; 30 system. out. println ("power-on with tri-compartment outlet"); 31 threeoutlet. connectelectriccurrent (); 32 twoutlet = adapter; 33 system. out. println ("power-on with hatchback socket"); 34 twoutlet. connectelectriccurrent (); 35} 36 37}
The running result is:
Connect to the power source using a sedan Outlet
Changjiang TV began to play TV programs
The washing machine in the Yellow River begins to wash clothes.
Power on with hatchback Outlet
Changjiang TV began to play TV programs
The washing machine in the Yellow River begins to wash clothes.
In this way, you can use a three-compartment outlet and a two-compartment outlet to connect the current to the TV and washing machine.
The adapter mode has the following advantages:
1. The target and the adaption are completely decoupled.
2. the adapter mode meets the "On-Off-closing principle". When a new class implementing the adapter interface is added, the adapter can adapt to the instance of the new class without having to modify the adapter.