设计模式之适配器模式

适配器模式

  1. 适配器模式结构图:

  2. 示例代码:

    // 已有登录类实现
    public class PassportService {
    
        public String regist(String userName, String password){
            System.out.println("注册成功=============");
            return "success";
        }
    
        public String loginOld(String userName, String password){
            System.out.println("登录成功=============");
            return "success";
        }
    }
    
    // 登录接口
    public interface LoginAdapter {
    
        boolean support(Object obj);
    
        String login(String username, String password);
    }
    
    // 抽象适配器类(继承已有类,并实现登录接口)
    public abstract class AbstractAdapter extends PassportService implements LoginAdapter{
        public abstract boolean support(Object obj) ;
        public abstract String login(String username,  String password) ;
    }
    
    // QQ登录,继承抽象适配器类
    public class LoginForQQAdapter extends AbstractAdapter {
    
        @Override
        public boolean support(Object obj) {
            return obj instanceof LoginForQQAdapter;
        }
    
        @Override
        public String login(String username,  String password) {
            System.out.println("QQ登录");
            if (password == null){
                password = "THIRD_PASSWORD";
                super.regist(username,password);
                System.out.println("QQ号注册成功====");
            }
            return super.loginOld(username, password);
        }
    
    }
    
    // 手机登录,继承抽象适配器类
    public class LoginForTelAdapter extends AbstractAdapter {
    
        @Override
        public boolean support(Object obj) {
            return obj instanceof LoginForTelAdapter;
        }
    
        @Override
        public String login(String username,  String password) {
            System.out.println("手机号登录");
            return super.loginOld(username, password);
        }
    
    }
    
    // 微信登录,继承抽象适配器类
    public class LoginForWechatAdapter extends AbstractAdapter {
    
        @Override
        public boolean support(Object obj) {
            return obj instanceof LoginForWechatAdapter;
        }
    
        @Override
        public String login(String username,  String password) {
            System.out.println("微信登录");
            if (password == null){
                password = "THIRD_PASSWORD";
                super.regist(username,password);
                System.out.println("微信号注册成功====");
            }
            return super.loginOld(username, password);
        }
    
    }
    
    // 第三方登录接口
    public interface PasswordForThird {
        String loginForQQ(String openId);
        String loginForWechat(String openId);
        String loginForTel(String phone, String code);
    }
    
    // 第三方登录实现
    
    public class PasswordForThirdAdapter implements PasswordForThird{
        @Override
        public String loginForQQ(String openId) {
            return processLogin(openId, null, LoginForQQAdapter.class);
        }
    
        @Override
        public String loginForWechat(String openId) {
            return processLogin(openId, null, LoginForWechatAdapter.class);
        }
    
        @Override
        public String loginForTel(String phone, String code) {
            return processLogin(phone, null, LoginForTelAdapter.class);
        }
    
        // 注意这里,通过多态实现适配器切换选择
        private String processLogin(String username, String password, Class<? extends LoginAdapter> clazz) {
            String message = "";
            try {
                LoginAdapter instance = clazz.getDeclaredConstructor().newInstance();
                if (instance.support(instance)){
                    message = instance.login(username, password);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return message;
        }
    }
    
    // 测试类
    public class AdapterTest {
    
        public static void main(String[] args) {
            PasswordForThird adapter = new PasswordForThirdAdapter();
    
            System.out.println(adapter.loginForQQ("lskdjflsdf"));
        }
    }
    
  3. 总结:适配器分为类适配器,对象适配器和接口适配器三类,上面的结构图为最简单的类适配器结构图,而下面的示例代码为一段登录的综合运用的代码示例,只为理解适配器模式.

    优点:提高类的透明性和复用,适配器类和原角色类解偶,提供了程序的扩展性;符合开闭原则;

    缺点:增加了系统的复杂性.

posted @ 2021-04-24 20:40  justKen  阅读(46)  评论(0编辑  收藏  举报