Client 接口
public interface LiveTecherClient {
/**
* 获取直播老师信息
*
* @param id 直播老师id
* @return 老师信息
*/
LiveTeacherInfo getTeacher(long id);
/**
* 批量获取直播老师信息
*
* @param ids 直播老师id 列表
* @return 直播老师id to LiveTeacherInfo
*/
Map<Long, LiveTeacherInfo> getIdToTeacher(List<Long> ids);
/**
* admin 用户是否是直播老师
*
* @param adminName admin 用户名称
* @return true: admin 用户是直播老师
*/
boolean isTeacher(String adminName);
}
涉及的数据结构:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LiveTeacherInfo {
private long id; // 直播老师 ID
private String nickName; // 直播老师 名称
private String adminName; // 老师的 admin 帐号用户名
}
Fake Client 实现
public class FakeLiveTecherClient implements LiveTecherClient {
private volatile AtomicLong idGen = new AtomicLong();
private volatile List<LiveTeacherInfo> liveTeacherInfos = new CopyOnWriteArrayList<>();
public LiveTeacherInfo createLiveTeacher() {
return createLiveTeacher("adminName");
}
public LiveTeacherInfo createLiveTeacher(String adminName) {
LiveTeacherInfo liveTeacherInfo = LiveTeacherInfo.builder()
.id(idGen.incrementAndGet())
.nickName("Colin")
.adminName(adminName)
.build();
liveTeacherInfos.add(liveTeacherInfo);
return liveTeacherInfo;
}
@Override
public LiveTeacherInfo getTeacher(long id) {
return liveTeacherInfos.stream()
.filter(v -> v.getId() == id)
.findFirst()
.orElse(null);
}
@Override
public Map<Long, LiveTeacherInfo> getIdToTeacher(List<Long> ids) {
return liveTeacherInfos.stream()
.filter(v -> ids.contains(v.getId()))
.collect(Collectors.toMap(LiveTeacherInfo::getId, Function.identity()));
}
@Override
public boolean isTeacher(String adminName) {
return liveTeacherInfos.stream()
.anyMatch(v -> v.getAdminName().equals(adminName));
}
}