(Adapter Pattern)

(Adapter Pattern)

Its function is to turn the interface of one class into another interface that the client expects, so that classes that would otherwise not work together due to interface mismatch work together. In short, there was a function before, but the new feature requires some code from the original function, and we don't want to develop a new one, so we create a new adapter so that it can be used。Similarly, the function of the mobile phone charging adapter,Our mobile phone charger has a very head, a double-ended three-head, we can use an adapter to insert the three-ended, two-ended plug into the adapter, and then the adapter for power input.Function:Resolve compatibility issues with different features Common Formulation:Class adaptation, interface adaptation, and object adaptation

Class Adapter

 

public class AC220 {
    public int outputAC220(){
        int output=220;
        System.out.println("outPut"+output);
        return output;
    }
}
public interface DC5 {
    int output5();
}
public class PowerAdapter extends AC220 implements DC5 {
    public int output5() {
        int adapterInput=super.outputAC220();
        int adaptOutput=adapterInput/44;
        System.out.println("adapterInput:"+adapterInput+"adaptOutput:"+adaptOutput);
        return adaptOutput;
    }
}

 

To test

public class Test {
    public static void main(String[] args) {
        DC5 powerAdapter=new PowerAdapter();
        powerAdapter.output5();
    }
}

To explain

The Least Knowledge Principle would be against(bacause in PowerAdapter we can use methods of AC220)

Object adapter

public class PowerAdapter  implements DC5 {
    private AC220 ac220;
    public PowerAdapter(AC220 ac220) {
        this.ac220 = ac220;
    }
    public int output5() {
        int adapterInput=ac220.outputAC220();
        int adaptOutput=adapterInput/44;
        System.out.println("adapterInput:"+adapterInput+"adaptOutput:"+adaptOutput);
        return adaptOutput;
    }
}

To explain

Actually,we just pass on object by construction,the problem of Least Knowledge Principle will be solved easily, but do we need create each adapter for each new classes?it is too complex to create adatper for each new classes ,so we use interface adapter to solve the problem

interface adapter

public class PowerAdapter implements DC {
    private AC220 ac220;

    public PowerAdapter(AC220 ac220) {
        this.ac220 = ac220;
    }

    public int outPut5V() {
        // can be use
        ac220.outputAC220();
        System.out.println("outPut5V");
        return 5;
    }

    public int outPut12V() {
        // can be use
        ac220.outputAC220();
        return  12;
    }

    public int outPut24V() {
        // can be use
        ac220.outputAC220();
        System.out.println("outPut24V");
        return 24;
    }
}
public interface DC {
    int outPut5V();
    int outPut12V();
    int outPut24V();
}

To Test

public class Test {
    public static void main(String[] args) {
        DC powerAdapter=new PowerAdapter(new AC220());
        powerAdapter.outPut5V();
        powerAdapter.outPut12V();
        powerAdapter.outPut24V();
    }
}

To explain

apparently, Interface Adapter like a  proxy pattern,we pass on class named AC220  then to reconstruct method of class of  AC220 

A scene of  login to illustrate how to use Adapter Pattern

 that is our kernel class to process logic  of login

public class LoginService {
    // to do register
    protected ResultMeg register(String userName, String password) {
        System.out.println("succeed in  register" + userName + ":" + password);
        return null;
    }

    // to do login
    protected ResultMeg login(String userName, String password) {
        System.out.println("succeed  in login" + userName + ":" + password);
        return null;
    }
}

we need an adapter to redirect  all of third party login

  • we create an interface to restrict all thrid part login(IPassportForThird)
  • we extends LoginService ,which can use method of LoginService(actually,that is class that we shoud adapt)
  • because each  thrid part login have different logic,we haven't to write all logic in the same class named  LoginAdapterForThird,so we have to have a method named processLogin to distribute all requirement into each calsses

tips: the method named support is to verify rationality of classes

public class LoginAdapterForThird implements IPassportForThird {
    // login for  QQ
    public ResultMeg loginForQQ(String openId) {
        return processLogin(openId, LoginForQQAdapter.class);
    }
    // login for WeChat
    public ResultMeg loginForWeChat(String openId) {
        return processLogin(openId, LoginForWeChatAdapter.class);
    }

    // login for Token
    public ResultMeg loginForToken(String token) {
        return processLogin(token, LoginForTokenAdapter.class);
    }

    // all third part login are distributed with this method
    private ResultMeg processLogin(String id, Class<? extends ILoginAdapter> clazz) {
        try {
            ILoginAdapter iLoginAdapter = clazz.newInstance();
            if (iLoginAdapter.support(iLoginAdapter)) {
                return iLoginAdapter.login(id, iLoginAdapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
public interface  IPassportForThird {
    ResultMeg loginForQQ(String openId);
    ResultMeg loginForWeChat(String openId);
    ResultMeg loginForToken(String token);
}

Then each adapter have to has two methods one of is method named support the other is login,Two of them are  ruled by ILoginAdapter

public interface ILoginAdapter {
    boolean support(Object o);
    ResultMeg login(String id, Object adapter);
}

each adapter have to has logic of LoginService ,which means each adapter should extends LoginService implements ILoginAdapter,do we need to create an abstract class to write some common method and make each adapter extends AbstractAdapter?,we do!

public abstract class AbstractAdapter extends LoginService implements ILoginAdapter {
    ResultMeg loginForRegister(String userName, String password) {
        if (null == password) {
            password = "EMPTY_THIRD";
        }
        super.register(userName, password);
        return super.login(userName, password);
    }
}

each adapter like them

public class LoginForQQAdapter extends AbstractAdapter{
    public boolean support(Object o) {
        return o instanceof LoginForQQAdapter;
    }

    public ResultMeg login(String id, Object adapter) {
        System.out.println("LoginForQQAdapter");
        return super.loginForRegister(id,null);    }

}
public class LoginForTokenAdapter extends AbstractAdapter {
    public boolean support(Object o) {
        return o instanceof LoginForTokenAdapter;    }

    public ResultMeg login(String id, Object adapter) {
        System.out.println("LoginForTokenAdapter");
        return super.loginForRegister(id,null);    }
}
public class LoginForWeChatAdapter extends AbstractAdapter {
    public boolean support(Object o) {
        return o instanceof LoginForWeChatAdapter;
    }
    public ResultMeg login(String id, Object adapter) {
        System.out.println("LoginForWeChatAdapter");
        return super.loginForRegister(id,null);
    }
}

To test(we can use LoginAdapterForThird  to get different method to login)

public class Test {
    public static void main(String[] args) {
        LoginAdapterForThird loginAdapterForThird=new LoginAdapterForThird();
        loginAdapterForThird.loginForQQ("121212");
    }
}

additional class

public class ResultMeg {
    String code;
    String message;
}

 

 How does  the adpater  use in source of code

 org.springframework.aop.framework.adapter.AdvisorAdapter(its classes that implementing it are all like what we wrote-(its supportsAdvice like our method named support,dosen't it)

 org.springframework.web.servlet.HandlerAdapter

 

 

 

  Sum up

advantages:

  •  the classes can be reused,and do not need to be change
  •  raise expansibility of system as you see if you want add a new function just add a new adapter

disadvantages:

  • raise complexity of system(we have to add many new adaoters)
  • raise difficulty of reading

 

 

posted @ 2021-04-27 22:57  UpGx  阅读(104)  评论(0)    收藏  举报