introduce

See the adapter, we must have thought of the charger to charge the mobile phone, because the mobile phone can not directly use 220V voltage to charge the mobile phone, need to use the charger to convert 220V voltage to 5V to charge the mobile phone. The role of the adapter pattern is like mobile phone charger, used for the compatibility of the interface adapter pattern has two kind of way, kind of adapter and object adapter, class adapter using the inheritance way, the object adapter using the combination way Through the communication conversion under the case to more deeply understand the adapter pattern First of all, The alternating current at home is 220V. We build standard alternating current to supply power for our life

public class AC220{
	private int output = 220;
    
    public int useAC(a){
    	System.out.println("Output" + output + "V交流电");
        returnoutput; }}Copy the code

Since 220V AC cannot charge the phone directly, we need to change the ac voltage to 5V to charge the phone. Therefore, we create adapters. There are two ways to realize adapters

public class PhoneAdapter1 extends AC220{
	@Override
    public int useAC(a){
    	int output = super.useAC();
        output = output / 44;
        System.out.println("Alternating current transformer");
        returnoutput; }}Copy the code

Object adapter

public class PhoneAdapter2 {
    private AC220 ac220;
    
    public PhoneAdapter2(AC220 ac220){
        this.ac220 = ac220;
    }
	
    public int useAC(a){
    	int output = ac220.useAC();
        output = output / 44;
        System.out.println("Alternating current transformer");
        returnoutput; }}Copy the code

It’s the same thing, it’s just a little bit different when you test it and then you test it

public class Demo{
	public static void main(String[] args){
    	PhoneAdapter1 phoneAdapter1 = new PhoneAdapter1();
        int ac1 = phoneAdapter1.useAC();
        System.out.println("Output" + ac1 + "V交流电");

        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = =");

        PhoneAdapter2 phoneAdapter2 = new PhoneAdapter2(new AC220());
        int ac2 = phoneAdapter2.useAC();
        System.out.println("Output" + ac2 + "V交流电"); }}Copy the code

The results of

Output 220 v alternating current transformer output 5 v = = = = = = = = = = = = = = = = = = = = = = = = = = output 220 v alternating current transformer output 5 v alternating current (ac)Copy the code





Application of the adapter pattern to Java logging

There are many logging frameworks developed using Java, such as Log4J, LogBack, JUL provided by the JDK, and JCL provided by Apache. Each of these logging frameworks provides similar functionality, with different logging levels, INFO, WARN, ERROR, DEBUG, and so on, printing different logs at different levels, but they provide different interfaces, which makes it difficult to use two frameworks in a single project. For example, String and Mybatis, assuming that the two frameworks use different logging frameworks, we need to configure these two logging frameworks in the configuration file, such as log printing format, log output path, etc., which is very troublesome. So Slf4j came out, he and JDBC, only provide interface, no implementation, equivalent to each logging framework defined the specification, each logging framework as long as the implementation of his defined interface, no matter how many logging framework introduced in the project, are using a set of interfaces, there is no trouble, however, Slf4j does not have an interface defined from the beginning, as JDBC does. Slf4j is older than logging frameworks such as Log4J and LogBack. If each logging framework re-implements this interface, it will cause problems in the old project compilation and require complete changes, which does not comply with the open closed principle. Indeed, Slf4j has also thought about this, providing adapter classes for each logging framework, secondary encapsulation for each logging framework, and adapting to the Slf4j interface definition. Therefore, when developing business systems, frameworks, and components, we use the Slf4j interface to write printing logging code. Which logging framework to use can be specified dynamically, using Java’s SPI technology by simply importing the corresponding dependencies into the project