Adapter pattern of design patterns

First post a URL https://blog.csdn.net/elegant_shadow/article/details/5006175
Thanks to the blogger for sharing, it is very clear that
the blogger understands deeply, especially this sentence:
Adaptation is to achieve a certain The purpose is to temporarily add a certain method to a source class, but it does not destroy the structure of the original class. The
blogger said it very well, and I basically copied it:
first, let’s talk about adapters first. Adaptation is the adaptation from "source" to "target", and the relationship between the two is the adaptor. It is responsible for transitioning the "source" to the "target". To give a simple example, for example, there is a "source" who is a target person. He has two skills: speaking Japanese and speaking English, and a certain position (target) requires you to speak Japanese, English, and French at the same time, okay. Now, our task is to adapt the "source" of people to this position. How to adapt it? Obviously we need to add a way for people to speak French in order to meet the needs of the target

Then discuss how to add the method of speaking French. Maybe you will say, why not add the method directly to the "source". My understanding is that adaptation is to temporarily add a certain method to a source class to achieve a certain purpose. method, so it cannot destroy the structure of the original class. At the same time, not doing this is also in line with Java's high cohesion and low coupling principles. Since we can't add it directly, let's talk about how to add a method for the "source" of people without destroying the structure of the "source" itself.

There are two types of adapter patterns: the first is "class-oriented adapter pattern", the second is "object-oriented adapter pattern"

One: Class-Oriented Adapter Pattern:

Let's talk about the "class-oriented adapter pattern" first. As the name suggests, this type of adapter mode is mainly used to implement adaptation for a single class. Why is it only implemented for a certain class? As mentioned later, we will show this class adaptation mode first. code implementation

The source code is as follows:

 1 public class Person {
 3     private String name;
 4     private String sex;
 5     private int age;
 6     
 7     public void speakJapanese(){
 8         System.out.println("I can speak Japanese!");
 9     }
10     
11     public void speakEnglish(){
12         System.out.println("I can speak English!");
13     }
14     ...//以下省略成员变量的get和set方法
15 }

The code for the target interface is as follows:

1 public interface Job {
2     public abstract void speakJapanese();
3     public abstract void speakEnglish();
4     public abstract void speakFrench();
5 }

The code for the adapter is as follows:

1  public  class Adapter extends Person implements Job{
 2      // Only need to rewrite the French-speaking method, other methods have been rewritten in the parent class
 3      // Can only be adapted for the source Person 
4      @Override
 5      public  void speakFrench( ) {
 6          System.out.println("I can speak French too" );
 7      }
 8 }

Well, after reading the code, I will make some explanations. Why is it called the class adaptation mode, a problem left before? Obviously, the Adapter class inherits the Person class, and in a single-inheritance language like Java, it means that it is impossible for him to inherit other classes, so that the adapter only serves the Person class. So it's called the class adaptation pattern

Two: object-oriented adapter pattern:

After talking about the adaptation mode of the class, we have to start talking about the adapter mode of the second object. The Object Adapter pattern aggregates the "source" as an object into the adapter class. Don't say the same thing, paste the code:

The source code and target code are the same as above, and will not be repeated again.

Just posting the adapter code:

1  public  class AdapterTwo implements Job {
 2      private Person person;
 3      // In the constructor, pass the source in as a parameter
 4      // Then call the source method
 5      // Advantages: It can be adapted for multiple sources and written differently 
6      public AdapterTwo (Person per) {
 7          this .person = per;
 8      }
 9  
10      @Override
 11      public  void speakJapanese() {
 12          person.speakJapanese();
 13      }
 14  
15      @Override
 16     public  void speakEnglish() {
 17          person.speakEnglish();
 18      }
 19  
20      @Override
 21      public  void speakFrench() {
 22          System.out.println("I can speak French too" );
 23      }
 24 }

The adapter pattern for objects, passing the "source" as a constructor parameter to the adapter, and then executing the methods required by the interface. This adaptation mode can be adapted for multiple sources. Make up for the lack of class adaptation mode

Now let's analyze the two adaptation modes:

1. The class adaptation mode is used for the adaptation of a single source. Due to the singleness of its source, the code implementation does not need to write selection logic, which is very clear; while the object adaptation mode can be used for multi-source adaptation, making up for The insufficiency of the class adaptation mode makes it impossible to write a lot of adapters in the class adaptation mode. The weakness is that because the number of sources can be large, the specific implementation condition selection branches are more, and it is not clear.

2. The adapter mode is mainly used in several situations: (1) The system needs to use the existing classes, but the existing classes do not fully meet the needs. (2) Classes that are not much related to each other are introduced to complete a certain work together (referring to object adaptation)

Three: Default adapter mode:
The core of this mode boils down to the following: when you want to implement an interface but do not want to implement all interface methods, but only want to implement some methods, use the default adapter mode, his method is in the interface and Add an abstract class to the concrete implementation class, and use the abstract class to empty all methods of the target interface. The specific implementation class only needs to override the methods it needs to complete.
code show as below:

Interface class:

1 public interface Job {
2     public abstract void speakJapanese();
3     public abstract void speakEnglish();
4     public abstract void speakFrench();
5     public abstract void speakChinese();
6 }

Abstract class:

 1 public abstract class JobDefault implements Job{
 2 
 3     public void speakChinese() {
 4         
 5     }
 6 
 7     public void speakEnglish() {
 8         
 9     }
10 
11     public void speakFrench() {
12         
13     }
14 
15     public void speakJapanese() {
16         
17     }
18 
19 }

Implementation class:

1  public  class JobImpl extends JobDefault{
 2      public  void speakChinese(){
 3          System.out.println("I only speak Chinese" );
 4      }
 5 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325154979&siteId=291194637