On Adapter mode

Keywords: Programming Mobile

I. Preface

Adapter mode can be divided into two types. The so-called "adapter" is the appropriate or appropriate coordination. Think of the adapter of the power supply, the function of which is to convert the AC 220V into different DC voltage to charge the mobile phone, computer, table lamp, etc. without these adapters, our equipment would have been on fire or scrapped, which is a terrible thing, so the adaptation The adapter is to transform the substance (data) from one form to another, so that the adapted party can receive and use it. Then we know from the analysis that the role of the adapter can be roughly divided into three roles (mobile phone, etc.), the adapter itself (adapter), and the adapted role (AC). However, as the design philosophy of high cohesion and low coupling, we add a layer of interface between the mobile phone and the adapter. The mobile phone directly uses this interface to use the adapter instead of the high coupling direct use, so there are four main roles. Considering the adaptation between the adapter and the adapted role, we can use inheritance to adapt or combination to adapt. Therefore, the adapter can be divided into two types: class adapter using inheritance and object adapter using combination (delegation).

Class II adapter

package zyr.dp.adapter;

public class Banner {
   private String name;
   public Banner(String name){
       this.name=name;
   }
   public void showWithParen(){
       System.out.println("("+name+")");
   }
   public void showWithAster(){
       System.out.println("*"+name+"*");
   }
}
package zyr.dp.adapter;

public interface Print {
  public abstract void printWeak();
  public abstract void printStrong();
}
package zyr.dp.adapter;

public class PrintBanner extends Banner implements Print {

    public PrintBanner(String name) {
        super(name);
    }

    public void printWeak() {
        System.out.println("...Start weak adaptation...");
        showWithParen();
        System.out.println("...Weak adaptation success...");
        System.out.println();
    }

    public void printStrong() {
        System.out.println("...Start strong fit...");
        showWithAster();
        System.out.println("...Strong adaptation success...");
        System.out.println();
    }
}

It can be seen that with the adapter, we use the high voltage of the Banner object through the PrintBanner adapter, and then use it through the main function. When we use it, we use it through the print interface to facilitate the scalability of the program. For example, there is another adapter that processes the string. We only need to modify a very small amount of code in the main. In this way, low cohesion and high coupling are realized.

III. object adapter

package zyr.dp.adapter.objectpattern;

public class Banner {
   private String name;
   public Banner(String name){
       this.name=name;
   }
   public void showWithParen(){
       System.out.println("("+name+")");
   }
   public void showWithAster(){
       System.out.println("*"+name+"*");
   }
}
package zyr.dp.adapter.objectpattern;

public abstract class Print {
  public abstract void printWeak();
  public abstract void printStrong();
}
package zyr.dp.adapter.objectpattern;

public class PrintBanner extends Print {

    Banner banner;
    public PrintBanner(String name) {
        banner=new Banner(name);
    }

    public void printWeak() {
        System.out.println("...Start weak adaptation...");
        banner.showWithParen();
        System.out.println("...Weak adaptation success...");
        System.out.println();
    }

    public void printStrong() {
        System.out.println("...Start strong fit...");
        banner.showWithAster();
        System.out.println("...Strong adaptation success...");
        System.out.println();
    }
}
package zyr.dp.adapter.objectpattern;

public class Main {

    public static void main(String[] args) {
        Print p=new PrintBanner("Zhu Yan Rong");
        p.printStrong();
        p.printWeak();
    }
}

It can be seen that the main function and banner class have not been changed, and the print interface has been changed into an abstract class. Then printBanner cannot inherit two classes at the same time, so it combines the banner object into the adapter, so it is called the object adapter, which can also achieve the expected results. The difference between the two is also very obvious. It is recommended to use the former, or screen according to the actual situation.

Four, summary

There are two kinds of adapter modes. We need to use them according to the actual situation. We may use some encapsulated codes, and sometimes we need to modify them slightly. However, these codes are locked by other components at the same time, so we can't change them on the original program. Therefore, through the adapter mode, we can further process these codes, and then Using, we can't change the original program any more, so through the adapter mode, we will further process these codes, and then use them again. This design method is very good, because we ensure that the code can be reused and personalized without modifying the original code. Generally, when upgrading the system, we need to ensure compatibility. We take the new system as the object to be adapted, and use the adapter defined by ourselves to ensure that the previous functions can be used in the old system section, so as to ensure compatibility.

 

Design mode @ table of contents

Posted by derrick24 on Wed, 16 Oct 2019 01:59:10 -0700