设计模式-适配器模式

适配器UML类图:

Adaptee角色:被适配角色 (UserService)

Adapter角色:适配器角色(LoginAdapter)

Target角色:目标角色(Login3rdTarget)

一、被适配角色

java 复制代码
import java.util.*;

public class UserService {

    public String login(String account, String password) {
        return "Register Success!";
    }

    public boolean checkUserExists(String userName) {
        return true;
    }

    public Object createTicket(String type, String userId, String content, String title) {
        return null;
    }

}

二、适配器角色

java 复制代码
@Component
public class LoginAdapter extends UserService implements LoginTarget{

    @Value("${gitee.state}")
    private String giteeState;

    @Value("${gitee.token.url}")
    private String giteeTokenUrl;

    @Value("${gitee.user.url}")
    private String giteeUserUrl;

    @Value("${gitee.user.prefix}")
    private String giteeUserPrefix;

    @Override
    public String loginByGitee(String code, String state) {
        //进行state判断,state的值是前端与后端商定好的,前端将State传给Gitee平台,Gitee平台回传state给回调接口
        if(!giteeState.equals(state)) {
            throw new UnsupportedOperationException("Invalid state!");
        }

        //请求Gitee平台获取token,并携带code
        String tokenUrl = giteeTokenUrl.concat(code);
        JSONObject tokenResponse = HttpClientUtils.execute(tokenUrl, HttpMethod.POST);
        String token = String.valueOf(tokenResponse.get("access_token"));
        System.out.println(token);
        //请求用户信息,并携带 token
        String userUrl = giteeUserUrl.concat(token);
        JSONObject userInfoResponse = HttpClientUtils.execute(userUrl, HttpMethod.GET);

        //获取用户信息
        String userName = giteeUserPrefix.concat(String.valueOf(userInfoResponse.get("name")));
        String password = userName;

        //"自动注册" 和登录功能,此处体现了方法的 "复用"
        return autoRegisterAndLogin(userName, password);
    }

    private String autoRegisterAndLogin(String userName, String password) {
        //如果第三方账号已经登录过,则直接登录
        if(super.checkUserExists(userName)) {
            return super.login(userName, password);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setUserPassword(password);
        userInfo.setCreateDate(new Date());

        //如果第三方账号是第一次登录,先进行"自动注册"
        super.register(userInfo);
        //自动注册完成后,进行登录
        return super.login(userName, password);
    }

    @Override
    public String loginByWechat(String... params) {
        return null;
    }

    @Override
    public String loginByQQ(String... params) {
        return null;
    }
}

三、目标角色

java 复制代码
public interface LoginTarget {
    public String loginByGitee(String code, String state);
    public String loginByWechat(String ... params);
    public String loginByQQ(String ... params);
}
相关推荐
七月丶2 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞2 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼2 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟3 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder3 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室3 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦4 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo7 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4967 天前
js设计模式 --- 工厂模式
设计模式