Design Mode-Adapter Mode)
Basic Concepts
The adapter mode converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together.
In the GoF design mode, there are two types of adapter modes: Class adapter mode and Object Adapter mode. There is also a special case of the Adapter mode, that is, the Default Adapter, also known as the interface adaptation mode.
1. Class adapter Mode
Converts an adaptation class API to an API of the target class. Because the class adapter mode matches one interface with another through multi-inheritance, although C #, java and other languages do not support multiple inheritance, you can also simply use the class adapter mode.
2. Object Adapter Mode
Like the adapter mode of the class, the difference is that the adapter mode of the object is not to connect to the Adaptee class by using an inheritance relationship, but to connect to the Adaptee class by using an association relationship.
3. Default adapter mode, also known as interface adapter Mode
The default adaptation mode provides a default implementation for an interface, so that the type can be extended from this default implementation without the need to extend from the original interface. As a special case of the adapter mode, the default adaptation mode has special applications in the JAVA language.
Problems
So that classes that cannot be combined because of interface incompatibility can work together.
Class adapter Mode
We use an example to learn the adapter mode: in daily life, the plug of the laptop is generally three-phase, that is, in addition to the anode, cathode, there is a ground pole. In some places, the power outlet is only two poles without a ground pole. The power plug of the power outlet does not match the power plug of the laptop so that the laptop cannot be used. At this time, a three-phase to two-phase converter (adapter) can solve this problem, and this is like what this mode does.
Class diagram:
Source code
Core Idea: There is an Adaptee class that has a method getReceptacle1 (), to be adapted, the Target interface Target, through the Adapter class, extends the Adaptee function to the Target, read the code:
Adaptee class, the class to be adapted
Public class Adaptee {/*** get socket */public void getReceptacle1 () {System. out. println (this is a two-hole socket !);}}
Target class, Target Interface
Public interface Target {/*** get two-hole socket */public void getReceptacle1 ();/*** get three-hole socket */public void getReceptacle2 ();}
Adapter class
/*** This type of goal is to enable the computer to use 2 empty sockets, solve the interface mismatch problem, and expand the function. * @ Author ZSL */public class Adapter extends Adaptee implements Target {@ Overridepublic void getReceptacle2 () {System. out. println (expansion plug-in, this is a three-hole plug !);}}
Test class:
/*** Test class ** @ author ZSL */public class Computer {public static void main (String [] args) {Target target = new Adapter (); target. getReceptacle1 (); target. getReceptacle2 (); // This function is the extended feature of the adapter }}
Object Adapter Mode
Like the adapter mode of the class, the difference is that the adapter mode of the object does not connect to the Adaptee class by using an inheritance relationship, but connects to the Adaptee class by using an association relationship. The class diagram is as follows:
As you can see, the Adaptee class does not have the getReceptacle2 () method, and the client expects this method. To enable the client to use the Adaptee class, a Wrapper Adapter is required. This packaging class encapsulates an Adaptee instance, so that this packaging class can link the Adaptee API with the Target class API. The Adapter and Adaptee are dependent, which determines that the Adapter mode is object.
Source code
Compared with the class adapter mode, you need to modify the adapter class.
Adapter class
/*** Object Adapter mode, * @ author Administrator **/public class Adapter2 implements Target {private Adaptee adaptee; public Adapter2 (Adaptee adaptee) {this. adaptee = adaptee;}/*** there is a method for the class to be adapted. You can directly use the adapter */@ Overridepublic void getReceptacle1 () {this. adaptee. getReceptacle1 ();}/*** this method is not available for the adaptive class. The adapter supplements */@ Overridepublic void getReceptacle2 () {System. out. println (expansion plug-in, this is a three-hole plug-in !);}}
Test class:
/*** Test class ** @ author ZSL */public class Computer {public static void main (String [] args) {Adaptee adaptee = new Adaptee (); target target = new Adapter2 (adaptee); target. getReceptacle1 (); target. getReceptacle2 (); // This function is the extended feature of the adapter }}
Interface adapter Mode
The Default Adapter mode provides a Default implementation for an interface, so that the type can be extended from this Default implementation instead of the original interface. As a special case of the adapter mode, the default adaptation mode has special applications in the JAVA language.
Sometimes an interface we write has multiple abstract methods. When we write the implementation class of this interface, all methods of this interface must be implemented, which is obviously a waste of time, because not all methods are needed, sometimes we only need some. To solve this problem, we introduce the adapter mode of the interface, with the help of an abstract class, this abstract class implements this interface and implements all the methods. Instead of dealing with the original interface, we only get in touch with this abstract class. Therefore, we write a class that inherits this abstract class, rewrite the method we need.
(Source: http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html)
What do monks do? Eat fast, read Sutra, sit, hit clock, Xi Wu, etc. If you design a monk interface and provide all methods that need to be implemented, the interface should be as follows:
Public interface monk {public void chuzhai (); public void chanting (); public void meditation (); public void hitting clock (); public void Xi Wu (); public String getName ();}
Obviously, all Monk classes should implement all the methods defined by interfaces, otherwise they will not be able to use the JAVA editor. Similar to the following Lu zhishen class.
Public class Lu Zhi-Shen implements monk {public void Xi Wu () {boxing town Kansai; Wutai Mountains; Maha peach blossom village; burning waguan temple; pulling down willow;} public String getName () {return Lu zhishen ;}}
Lu zhishen only implements the getName () and XI Wu () methods, but does not implement any other methods. Therefore, it does not pass through the Java compiler. Lu zhishen class can only use Java compiler to implement all methods of monk interface, but Lu zhishen is no longer Lu zhishen. Learn from history and know the world. Studying how Lu zhishen shaved a Monk several hundred years ago will inspire Java programming. Good, when Ruda shedges, the monk said: "This person describes ugliness and looks stubborn. He cannot shave him, but the elders said:" This person is straight to the stars and his heart. Although the current fierce, hit complex, but after a long time but clean. The evidence is remarkable, and you are inferior to him ."
So it turns out! It seems that the problem will be solved as long as a star is used here! If you use an object-oriented language, you can implement it as well. If you are a "Star", you can also use an abstract class.
Public abstract class tianxing implements monk {public void eat Zhai () {} public void read Sutra () {} public void SIT () {} public void hit clock () {} public void Xi Wu () {} public String getName () {return null ;}}
Lu zhishen inherits the abstract class "tianxing"
Public class Lu zhishen extends tianxing {public void Xi Wu () {boxing town Kansai; Wutai Mountain; huanao peach blossom village; burning waguan temple; pulling down Yangliu;} public String getName () {return Lu zhishen ;}}
The abstract star category is an adapter class, and Lu zhishen actually achieved the purpose of shaving by means of the adapter mode. This adapter class implements all the methods required by the monk interface. However, unlike the usual adapter mode, the implementation of all the methods provided by this adapter class is "mediocre. This "mediocre" adapter mode is called the default adaptation mode.
In many cases, a specific class must implement an interface, but this class does not use all the methods specified by the interface. The common solution is to implement all methods in this specific class, implement useful methods, and implement idle and mediocre methods.
These empty methods are a waste and sometimes a mess. Unless you have read the code for these empty methods, the programmer may think that these methods are not empty. Even if he knows that some of the methods are empty, he does not necessarily know which methods are empty and which are not empty unless he has read the source code or documentation of these methods.
The default adaptation mode can handle this situation well. You can design an abstract adapter class to implement the interface. This abstract class provides an empty method for each method required by the interface. Just like Lu zhishen's "Shang Ying tianxing", this abstract class can save its specific subclass from being forced to implement null methods.
Comparison between class adapters and object adapters
Class Adapter |
Object Adapter |
Object Inheritance is a static definition method. |
Object combination is a dynamic combination. |
The adapter can redefine some behavior of Adaptee, which is equivalent to partial implementation of sub-classes covering the parent class. |
It is difficult to redefine the behavior of Adaptee. In this case, you need to define a subclass of Adaptee to implement redefinition, and then let the adapter combine subclasses. Although it is difficult to redefine Adaptee behavior, it is convenient to add some new behavior, and the newly added behavior can be applied to all sources at the same time. |
Only one object is introduced, and no additional reference is required to indirectly obtain Adaptee. |
An additional reference is required to indirectly obtain Adaptee. |
Adapter advantages
1. Better reusability
The system needs to use existing classes, and such interfaces do not meet the requirements of the system. The adapter mode enables better reuse of these functions.
2. better scalability
When implementing the adapter function, you can call your own developed functions to naturally expand the system functions.
Adapter disadvantages
Using too many adapters will make the system messy and difficult to grasp as a whole. For example, we can see that the called a interface is actually adapted to the implementation of B interface internally. If there are too many systems, this would be a disaster. Therefore, if not necessary, you can directly refactor the system without using the adapter.