Java 两种方式的角色管理

我正在设计一些具有用户和团队的 Java 应用程序:


public abstract class AbstractUser()


public class BasicUser extends AbstractUser()


public class AnotherUser extends AbstractUser()

现在我有包含用户的团队:


public abstract class AbstractTeam() {

  protected List<AbstractUser> members;

}


public class PublicTeam extends AbstractTeam()

现在我需要管理团队,所以每个用户都应该有一个团队状态,例如成员、版主、管理员、所有者等。


于是想到做一些接口:


public Interface TeamRole


public Interface Member extends TeamRole


public Interface Moderator extends TeamRole

并让 AbsractUser 类实现 TeamRole:


public abstract class AbstractUser implements TeamRole

现在我想做的是:让一个控制器来处理删除用户创建的条目:


public boolean deleteEntry(User u, Entry e, requestingUser r) {


}

如何获得干净的表示,以便我可以执行以下操作:


u.getEntries().removeIf(entry -> entry.equals(e));

在不“尝试”if 语句中的所有角色的情况下以一种干净的方式?


更新:这听起来可能不太“好”,但这只是为了描述执行操作时检查权限的问题


MYYA
浏览 248回答 3
3回答

繁花不似锦

首先,充满鳗鱼的气垫船正确地说AbstractUser应该组合TeamRole而不是继承它(因为它是“有”关系而不是“是”关系)。现在到手头的问题。我不确定我是否完全理解您的要求,但是我假设您是在询问如何在AbstractUser不使用iforswitch语句进行检查的情况下检查是否具有执行操作的权限(或权限)。如果是这样,那么这里有一种方法(在我能想到的几种方法中)。创建一个Enum权限级别,每个权限级别都有一个与权限级别直接相关的序数。下面是一个基本的例子:class enum PermissionLevel {&nbsp; &nbsp; MEMBER(0),&nbsp; &nbsp; MODERATOR(1)&nbsp; &nbsp; private final int level;&nbsp; &nbsp; PermissionLevel(int level) {&nbsp; &nbsp; &nbsp; &nbsp; this.level = level;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public int getLevel() {&nbsp; &nbsp; &nbsp; &nbsp; return level;&nbsp; &nbsp; }}现在给TeamRole接口一个返回与该角色关联的权限级别的方法public Interface TeamRole {&nbsp; &nbsp; PermissionLevel getPermissionLevel();&nbsp; &nbsp; ...}现在,检查时,如果一个成员有权所有你需要做的是对的权限级别的整数比较TeamRole的AbstractUser了。if (user.getTeamRole().getPermissionLevel().getLevel() > PermissionLevel.MEMBER.getLevel()) {&nbsp; &nbsp; // User has permission to do this action}

胡说叔叔

我知道已经有了答案,但我想我可以分享一个不同的想法,我们将使用继承来表达角色层次结构和方法签名来约束操作。这是一般的设计思路:用法:public class TeamApplicationService {&nbsp; &nbsp; public void doSomethingSpecial(final String teamId) {&nbsp; &nbsp; &nbsp; &nbsp; final Team team = teamRepository.teamOfId(teamId);&nbsp; &nbsp; &nbsp; &nbsp; final TeamModerator moderator = team.findModerator(currentUserId);&nbsp; &nbsp; &nbsp; &nbsp; team.doSomethingSpecial(moderator);&nbsp; &nbsp; }}public class Team {&nbsp; &nbsp; final Map<String, TeamMember> teamMembers;&nbsp; &nbsp; public Team() {&nbsp; &nbsp; &nbsp; &nbsp; teamMembers = new HashMap<String, TeamMember>();&nbsp; &nbsp; }&nbsp; &nbsp; public void addMember(final TeamMember member) {&nbsp; &nbsp; &nbsp; &nbsp; teamMembers.put(member.userId(), member);&nbsp; &nbsp; }&nbsp; &nbsp; private <T> T findMemberOfRole(final Class<T> clazz, final String userId) {&nbsp; &nbsp; &nbsp; &nbsp; if (!teamMembers.containsKey(userId)) throw couldNotFindMember();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T)clazz.cast(teamMembers.get(userId));&nbsp; &nbsp; &nbsp; &nbsp; } catch (java.lang.ClassCastException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw couldNotFindMember();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private RuntimeException couldNotFindMember() {&nbsp; &nbsp; &nbsp; &nbsp; return new RuntimeException("Could not find member");&nbsp; &nbsp; }&nbsp; &nbsp; public TeamModerator findModerator(final String userId) {&nbsp; &nbsp; &nbsp; &nbsp; return this.<TeamModerator>findMemberOfRole(TeamModerator.class, userId);&nbsp; &nbsp; }&nbsp; &nbsp; public TeamMember findMember(final String userId) {&nbsp; &nbsp; &nbsp; &nbsp; return this.<TeamMember>findMemberOfRole(TeamMember.class, userId);&nbsp; &nbsp; }&nbsp; &nbsp; public void doSomethingSpecial(TeamModerator moderator) {&nbsp; &nbsp; &nbsp; &nbsp; //Moderator only&nbsp; &nbsp; }&nbsp; &nbsp; public void doSomethingGeneral(TeamMember member) {&nbsp; &nbsp; &nbsp; &nbsp; //Any member&nbsp; &nbsp; }}编辑:我不完全确定要求,但这里有一个基于评论的更复杂的例子。

一只名叫tom的猫

TeamRole 成为删除策略(也许重命名?)。可以使用更多的重构,但我认为一般的想法会奏效。如果我们有更多的角色,我们可以添加更多的类并实现它们如何处理删除。public class Entry{}public class User extends AbstractUser{}public abstract class AbstractUser {&nbsp; &nbsp; TeamRole role;&nbsp; &nbsp; List<Entry> entries;&nbsp; &nbsp; //self deletes are always ok&nbsp; &nbsp; public void deleteEntry(Entry e){&nbsp; &nbsp; &nbsp; &nbsp; this.getEntries().removeIf(entry -> entry.equals(e));&nbsp; &nbsp; }&nbsp; &nbsp; //delete depending on requested user role&nbsp; &nbsp; public void deleteEntry(User requestBy, Entry e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; role.delete(this, e, requestBy);//mod or other poeple with ability to delete.&nbsp; &nbsp; }&nbsp; &nbsp; public List<Entry> getEntries() {&nbsp; &nbsp; &nbsp; &nbsp; return entries;&nbsp; &nbsp; }};public class BasicUser extends AbstractUser {};public class AnotherUser extends AbstractUser {};public abstract class AbstractTeam {&nbsp; &nbsp; protected List<AbstractUser> members;}public class PublicTeam extends AbstractTeam {};public interface TeamRole{&nbsp; &nbsp; public void delete(AbstractUser user, Entry entry, User requester);}public class Member implements TeamRole{&nbsp; &nbsp; @Override&nbsp; &nbsp; public void delete(AbstractUser user, Entry entry, User requester) {&nbsp; &nbsp; &nbsp; &nbsp; if(user==requester) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.deleteEntry(entry);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class Moderator implements TeamRole{&nbsp; &nbsp; @Override&nbsp; &nbsp; public void delete(AbstractUser user, Entry entry, User requester) {&nbsp; &nbsp; &nbsp; &nbsp; user.deleteEntry(entry);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java