java design patterns ---- adapter mode, plus how third-party landed Interface

Adapter mode (Adapter Pattern)

It refers to converting the interface of a class into another interface clients expect, so that the original interface is not compatible with the class can work together.

Belonging to the structural design patterns.

Applicable scene:

1. existing classes, methods and the case where it does not match the demand.

2. The adapter design pattern mode is not considered in the design phase of the software, along with software maintenance, due to the different solutions in products from different manufacturers cause similar function interface is not the same situation.

Code Scene:

Now we need to add a third-party login to the system, to add to retain the original landing in the case of the interface unchanged.

/**
 * @Author Darker
 * @Descrption
 * @Date : Created in 16:07 2020-3-12
 */
@Data
@AllArgsConstructor
public class MsgResult {

    private int code;
    private Object data;
    private String msg;

    public static MsgResult success(Object data){
        return new MsgResult(200,data,"成功");
    }

    public static MsgResult fail(Object data){
        return new MsgResult(404,data,"失败");
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  相当于系统已经写好的代码,不去动它
 * @E-Mail : [email protected]
 * @Date : Created in 13:38 2020-3-14
 */
public class LoginService {

    //用户名,密码登陆方法
    public MsgResult login(String username,String password){
        return null;
    }

    //注册方法
    public MsgResult register(String username,String password){
        return login(username,password);
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 新的登陆接口
 * @E-Mail : [email protected]
 * @Date : Created in 14:02 2020-3-14
 */
public interface NewLogin {

    public MsgResult loginQQ(String id);

    public MsgResult loginWechat(String id);
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 适配器方式
 * @E-Mail : [email protected]
 * @Date : Created in 13:54 2020-3-14
 */
public class LoginForThird extends LoginService implements NewLogin{


    @Override
    public MsgResult loginQQ(String id) {
        System.out.println("通过qq登陆");
        return null;
    }

    @Override
    public MsgResult loginWechat(String id) {
        System.out.println("通过微信登陆");
        return null;
    }
}

This is one of the most simple adapter mode, of course, much like the static proxy mode, because the design pattern is an idea, not limited to the code, but with the way the code to implement this idea, of course, this is certainly too simple, so we are now at the industry to achieve the more popular ways to increase third-party landing.

The first step: We reserve the original system login unchanged

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  相当于系统已经写好的代码,不去动它
 * @E-Mail : [email protected]
 * @Date : Created in 13:38 2020-3-14
 */
public class LoginService {

    //用户名,密码登陆方法
    public MsgResult login(String username,String password){
        System.out.println("用户pc登陆");
        return null;
    }

    //注册方法
    public MsgResult register(String username,String password){
        return login(username,password);
    }
}

Step two: we add a adapter interface, and all adapters

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 在适配器中,这个接口可有可无,不要和模板模式混淆
 *              模板模式一定是抽象类,这里只是一个接口
 * @E-Mail : [email protected]
 * @Date : Created in 14:38 2020-3-14
 */
public interface LoginAdapter {

    //判断是否是需要调用的适配器,是否兼容
    /**
     * 为什么要这个方法,因为适配器可能会有多个,比如再来个注册适配器
     * @param adapter
     * @return
     */
    boolean support(Object adapter);

    public MsgResult login(String id,Object adapter);
}
/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 14:41 2020-3-14
 */
public class LoginQQAdapter implements LoginAdapter{
    @Override
    public boolean support(Object adapter) {
        return adapter instanceof LoginQQAdapter;
    }

    @Override
    public MsgResult login(String id, Object adapter) {
        //写自己qq登陆的逻辑
        System.out.println("通过qq登陆");
        return null;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 14:42 2020-3-14
 */
public class LoginWechatAdapter implements LoginAdapter {
    @Override
    public boolean support(Object adapter) {
        return adapter instanceof LoginWechatAdapter;
    }

    @Override
    public MsgResult login(String id, Object adapter) {
        //写微信自己登陆的逻辑
        System.out.println("通过微信登陆");
        return null;
    }
}


/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 13:54 2020-3-14
 */
public class LoginForThird extends LoginService implements NewLogin{


    //这里可以继续使用策略模式来优化
    @Override
    public MsgResult loginQQ(String id) {
        return loginAdapterFactory(id,LoginQQAdapter.class);
    }

    @Override
    public MsgResult loginWechat(String id) {
        return loginAdapterFactory(id,LoginWechatAdapter.class);
    }

    //简单工厂
    public MsgResult loginAdapterFactory(String id,Class <? extends LoginAdapter> clazz){
        try {
            LoginAdapter adapter = clazz.newInstance();
            if(adapter.support(adapter)){
                return adapter.login(id,adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MsgResult.fail(null);
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 17:55 2020-3-13
 */
public class AdapterTest {
    public static void main(String[] args) {
        LoginForThird loginForThird = new LoginForThird();
        loginForThird.login("xiaoming","123456");
        loginForThird.loginQQ("dsadas");
        loginForThird.loginWechat("1556");
    }
}

This is consistent with the principle of opening and closing, and other third-party adapter to solve the needs of the landing, and if need other landing, only need to continue to increase adapter on it.

Look at the class diagram:

Let's look at strategy pattern spring-aop used in:

 

 

springAop messages notification on the use of the adapter mode, select the adapter you want, and then look after the notification before the method or methods notice.

Look at the spring-mvc adapters:

 

 

Adapter Here are mainly used in the servlet inside, we are interested can look to a small partner in the doDispatch in DispatcherServlet.

to sum up:

advantage:

1. To improve the transparency and class reuse, the conventional type do not need to change the multiplexing.

2. Decoupling target class and adapter classes.

3. many scenarios in line with the principle of opening and closing.

Disadvantages:

1. The need to fully consider, and it may increase the complexity of the system.

2. To increase the difficulty of reading the code, excessive use may make the code becomes messy.

Published 27 original articles · won praise 1 · views 3639

Guess you like

Origin blog.csdn.net/qq_40111437/article/details/104858328