JAVA and mode 7th-adapter Mode

Source: Internet
Author: User

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.
________________________________________
Usage of the adapter Mode
Using electrical appliances as an example, the plug of the laptop is generally three-phase, that is, in addition to the anode and cathode, there is also 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.
Structure of the adapter Mode
The adapter mode has two different forms: Class adapter mode and Object Adapter mode.
Class adapter Mode
The adapter mode of the class converts the API of the adaptive class into the API of the target class.
 
 
As can be seen in, the Adaptee class does not have the sampleOperation2 () method, and the client expects this method. To enable the client to use the Adaptee class, a class Adapter is provided to link the Adaptee API with the Target class API. The Adapter and Adaptee are inherited, which determines that the Adapter mode is class:
The role involved in the mode is:
● Target role: This is the expected interface. Note: The class adapter mode is discussed here, so the target cannot be a class.
● Source (Adapee) role: the interface to be adapted now.
● Adapter (Adaper) role: the adapter class is the core of this mode. The adapter converts the source interface to the target interface. Obviously, this role cannot be an interface, but must be a specific class.
Source code
[Java]
1. public interface Target {
2 ./**
3. * This is also a method of source class Adaptee.
4 .*/
5. public void sampleOperation1 ();
6 ./**
7. * This is a method not available in the source class Adapteee.
8 .*/
9. public void sampleOperation2 ();
10 .}
 

 
The source code of the target role is provided above. This role is implemented in the form of a JAVA interface. We can see that this interface declares two methods: sampleOperation1 () and sampleOperation2 (). The source role Adaptee is a specific class. It has a sampleOperation1 () method, but there is no sampleOperation2 () method.
[Java]
1. public class Adaptee {
2.
3. public void sampleOperation1 (){}
4.
5 .}
 

 
The Adapter role Adapter extends Adaptee and implements the Target interface. Since Adaptee does not provide the sampleOperation2 () method and the target interface requires this method, the Adapter role implements this method.
[Java]
1. public class Adapter extends Adaptee implements Target {
2 ./**
3. * because the source class Adaptee does not have the sampleOperation2 () method ()
4. * therefore, the adapter adds this method.
5 .*/
6. @ Override
7. public void sampleOperation2 (){
8. // write related Code
9 .}
10.
11 .}
 

 
Object Adapter Mode
Like the adapter mode of the class, the Object Adapter mode converts the API of the adapted class into the API of the target class. Unlike the adapter mode of the class, the object's adapter mode is not to connect to the Adaptee class by using an inheritance relationship, but to connect to the Adaptee class by using a delegation relationship.
 
As you can see, the Adaptee class does not have the sampleOperation2 () 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 delegates, which determines that the Adapter mode is object.
Source code
[Java]
1. public interface Target {
2 ./**
3. * This is also a method of source class Adaptee.
4 .*/
5. public void sampleOperation1 ();
6 ./**
7. * This is a method not available in the source class Adapteee.
8 .*/
9. public void sampleOperation2 ();
10 .}
 

 
[Java]
1. public class Adaptee {
2.
3. public void sampleOperation1 (){}
4.
5 .}
 

 
[Java]
1. public class Adapter {
2. private Adaptee adaptee;
3.
4. public Adapter (Adaptee adaptee ){
5. this. adaptee = adaptee;
6 .}
7 ./**
8. * method sampleOperation1 for source class Adaptee
9. * therefore, the adapter class can be directly delegated.
10 .*/
11. public void sampleOperation1 (){
12. this. adaptee. sampleOperation1 ();
13 .}
14 ./**
15. * The Source class Adaptee does not have a method sampleOperation2
16. * therefore, this method needs to be supplemented by the adapter class
17 .*/
18. public void sampleOperation2 (){
19. // write related Code
20 .}
21 .}
 

 
Balance between class adapters and object adapters
● The class adapter uses the Object Inheritance method, which is a static definition method. The Object Adapter uses the object combination method, which is a dynamic combination method.
● For the class adapter, because the adapter directly inherits Adaptee, the adapter cannot work with the Adaptee subclass because the inheritance is a static relationship. After the adapter inherits the Adaptee, it is impossible to process the sub-classes of Adaptee.
For object adapters, an adapter can adapt multiple sources to the same target. In other words, the same adapter can adapt both the source class and its subclass to the target interface. Because the Object Adapter uses the relationship between object combinations, it doesn't matter if the object type is correct or not.
● For a class adapter, the adapter can redefine partial behavior of the Adaptee, which is equivalent to partial implementation of a subclass that overwrites the parent class.
For object adapters, 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 compose the subclass. 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.
● For Class adapters, only one object is introduced, and no additional reference is required to indirectly obtain Adaptee.
For object adapters, additional references are required to indirectly obtain Adaptee.
We recommend that you use the Object Adapter implementation method as much as possible, and use synthesis/aggregation instead of inheritance. Of course, you can select the implementation method based on the specific analysis of specific problems. The most suitable is the best.
Advantages of the adapter Mode
• 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.
• Better scalability
When implementing the adapter function, you can call your own developed functions to naturally expand the system functions.
Disadvantages of adapter Mode
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.
 
________________________________________
 
 
Default Adaptation 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.
Lu zhishen's story
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:
[Java]
1. public interface monk {
2. public void ();
3. public void ();
4. public void meditation ();
5. public void hitting the clock ();
6. public void Xi Wu ();
7. public String getName ();
8 .}
 
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.
[Java]
1. public class Lu zhishen implements monk {
2. public void Xi Wu (){
3. Boxing in Kansai;
4. Wutaishan;
5. Maha peach blossom village;
6. Burning the waguan temple;
7. Pull down willow trees;
8 .}
9. public String getName (){
10. return "Lu zhishen ";
11 .}
12 .}
 

 
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 shaves, the monk said: "This person describes ugliness, looks stubborn, do not shave him", but the elders said: "This person is straight to the stars and 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.
[Java]
1. public abstract class tianxing implements monk {
2. public void (){}
3. public void chanting (){}
4. public void meditation (){}
5. public void hitting the clock (){}
6. public void Xi Wu (){}
7. public String getName (){
8. return null;
9 .}
10 .}
 

 
Lu zhishen inherits the abstract class "tianxing"
[Java]
1. public class Lu zhishen extends monk {
2. public void Xi Wu (){
3. Boxing in Kansai;
4. Wutaishan;
5. Maha peach blossom village;
6. Burning the waguan temple;
7. Pull down willow trees;
8 .}
9. public String getName (){
10. return "Lu zhishen ";
11 .}
12 .}
 

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.
Structure of the default Adaptation Mode
The default adaptation mode is a "mediocre" adapter mode.
 
 
[Java]
1. public interface AbstractService {
2. public void serviceOperation1 ();
3. public int serviceOperation2 ();
4. public String serviceOperation3 ();
5 .}
 

 
[Java]
1. public class ServiceAdapter implements AbstractService {
2.
3. @ Override
4. public void serviceOperation1 (){
5 .}
6.
7. @ Override
8. public int serviceOperation2 (){
9. return 0;
10 .}
11.
12. @ Override
13. public String serviceOperation3 (){
14. return null;
15 .}
16.
17 .}
 

 
As you can see, the interface AbstractService requires defining three methods: serviceOperation1 (), serviceOperation2 (), and serviceOperation3 (); abstract adapter ServiceAdapter provides mediocre implementation for all three methods. Therefore, any specific class that inherits from the abstract class ServiceAdapter can select the method implementation it requires, without worrying about other unnecessary methods.
The purpose of the adapter mode is to change the source interface so that the target interface is compatible. Default adaptation has a slightly different purpose. It is a mediocre implementation provided to facilitate the establishment of a non-mediocre adapter class.
At any time, if you do not want to implement all the methods of an interface, you can use the "Default Adaptation Mode" to create an abstract class and give a mediocre implementation of all methods. In this way, the subclass inherited from this abstract class does not have to implement all the methods.

 

Author: m13666425773

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.