开发者社区> 问答> 正文

利用 Lazy 构造通用 User 实体的工厂是如何实现的?

利用 Lazy 构造通用 User 实体的工厂是如何实现的?

展开
收起
游客hmzk6xi5mlygs 2022-05-12 14:51:23 246 0
1 条回答
写回答
取消 提交回答
  • @Component public class UserFactory {

    // 部门服务, rpc 接口 @Resource private DepartmentService departmentService;

    // 主管服务, rpc 接口 @Resource private SupervisorService supervisorService;

    // 权限服务, rpc 接口 @Resource private PermissionService permissionService;

    public User buildUser(long uid) { Lazy< String> departmentLazy = Lazy.of(() -> departmentService.getDepartment(uid)); // 通过部门获得主管 // department -> supervisor Lazy< Long> supervisorLazy = departmentLazy.map( department -> SupervisorService.getSupervisor(department) ); // 通过部门和主管获得权限 // department, supervisor -> permission Lazy< Set< String>> permissionsLazy = departmentLazy.flatMap(department -> supervisorLazy.map( supervisor -> permissionService.getPermissions(department, supervisor) ) );

    User user = new User(); user.setUid(uid); user.setDepartment(departmentLazy); user.setSupervisor(supervisorLazy); user.setPermissions(permissionsLazy); } }

    2022-05-12 17:42:14
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
继承与功能组合 立即下载
开源广进-用Service Catalog构造K8S服务能力 立即下载
低代码开发师(初级)实战教程 立即下载