Design pattern--Adapter pattern

The adapter pattern converts the existing interface into the interface expected by the client class, and realizes the reuse of the existing class. It is a design pattern that is used very frequently and is widely used in software development. It is widely used in open source frameworks such as Spring . , Adapter pattern is also used in driver design (such as database driver in JDBC ).

 

       1.  Main advantages

       Both the object adapter pattern and the class adapter pattern have the following advantages:

       (1)  Decouple the target class and the adapter class , and reuse the existing adapter class by introducing an adapter class without modifying the original structure.

       (2)  The transparency and reusability of the class are increased, and the specific business implementation process is encapsulated in the adapter class, which is transparent to the client class, and improves the reusability of the adapter. An adapter class can be reused in many different systems.

       (3)  The flexibility and extensibility are very good . By using the configuration file, the adapter can be easily replaced, and new adapter classes can be added without modifying the original code, which fully conforms to the "open-closed principle".

      Specifically, the class adapter pattern has the following advantages:

      Since the adapter class is a subclass of the adapter class, some adapter methods can be replaced in the adapter class , making the adapter more flexible.

      The object adapter pattern also has the following advantages:

      (1)  An object adapter can adapt multiple different adapters to the same target ;

      (2)  It is possible to adapt a subclass of an adaptor . Since the adapter and the adaptor are related, according to the "Liskov Substitution Principle", the subclass of the adaptor can also be adapted through the adaptor.

 

      2.  Major Disadvantages

     The disadvantages of the class adapter pattern are as follows:

      (1)  For languages ​​such as Java and C# that do not support multiple class inheritance, only one adaptor class can be adapted at most at a time, and multiple adaptors cannot be adapted at the same time ;

      (2)  The adapter class cannot be a final class , such as a final class in Java and a sealed class in C# ;

      (3)  In languages ​​such as Java and C# , the target abstract class in the class adapter pattern can only be an interface, not a class , and its use has certain limitations.

      The disadvantages of the Object Adapter pattern are as follows:

      Compared to the class adapter pattern, it is more cumbersome to replace some methods of the adapter class in the adapter . If you must replace one or more methods of the adapter class, you can first make a subclass of the adapter class, replace the methods of the adapter class, and then treat the subclass of the adapter class as the real The adaptor is adapted, and the implementation process is more complicated.

 

      3.  Applicable scenarios

      在以下情况下可以考虑使用适配器模式:

       (1) 系统需要使用一些现有的类,而这些类的接口(如方法名)不符合系统的需要,甚至没有这些类的源代码。

       (2) 想创建一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。


直接代码:

demo1:

public class Banner {
    private String string;
    public Banner(String string) {
        this.string = string;
    }
    public void showWithParen() {
        System.out.println("(" + string + ")");
    }
    public void showWithAster() {
        System.out.println("*" + string + "*");
    }
}
public interface Print {
    public abstract void printWeak();
    public abstract void printStrong();
}
public class PrintBanner extends Banner implements Print {
    public PrintBanner(String string) {
        super(string);
    }
    public void printWeak() {
        showWithParen();
    }
    public void printStrong() {
        showWithAster();
    }
}
public class Main {
    public static void main(String[] args) {
        Print p = new PrintBanner("Hello");
        p.printWeak();
        p.printStrong();
    }
}

稍微变一下;

demo2:

public class Banner {
    private String string;
    public Banner(String string) {
        this.string = string;
    }
    public void showWithParen() {
        System.out.println("(" + string + ")");
    }
    public void showWithAster() {
        System.out.println("*" + string + "*");
    }
}
public abstract class Print {
    public abstract void printWeak();
    public abstract void printStrong();
}
public class PrintBanner extends Print {
    private Banner banner;
    public PrintBanner(String string) {
        this.banner = new Banner(string);
    }
    public void printWeak() {
        banner.showWithParen();
    }
    public void printStrong() {
        banner.showWithAster();
    }
}
public class Main {
    public static void main(String[] args) {
        Print p = new PrintBanner("Hello");
        p.printWeak();
        p.printStrong();
    }
}

demo3:

import java.io.*;

public interface FileIO {
    public void readFromFile(String filename) throws IOException;
    public void writeToFile(String filename) throws IOException;
    public void setValue(String key, String value);
    public String getValue(String key);
}
import java.io.*;
import java.util.*;

public class FileProperties extends Properties implements FileIO {
    public void readFromFile(String filename) throws IOException {
        load(new FileInputStream(filename));
    }
    public void writeToFile(String filename) throws IOException {
        store(new FileOutputStream(filename), "written by FileProperties");
    }
    public void setValue(String key, String value) {
        setProperty(key, value);
    }
    public String getValue(String key) {
        return getProperty(key, "");
    }
}
import java.io.*;

public class Main {
    public static void main(String[] args) {
        FileIO f = new FileProperties();
        try {
            f.readFromFile("file.txt");
            f.setValue("year", "2004");
            f.setValue("month", "4");
            f.setValue("day", "21");
            f.writeToFile("newfile.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Guess you like

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