3 回答
TA贡献1851条经验 获得超4个赞
首先,充满鳗鱼的气垫船正确地说AbstractUser应该组合TeamRole而不是继承它(因为它是“有”关系而不是“是”关系)。
现在到手头的问题。我不确定我是否完全理解您的要求,但是我假设您是在询问如何在AbstractUser不使用iforswitch语句进行检查的情况下检查是否具有执行操作的权限(或权限)。如果是这样,那么这里有一种方法(在我能想到的几种方法中)。创建一个Enum权限级别,每个权限级别都有一个与权限级别直接相关的序数。下面是一个基本的例子:
class enum PermissionLevel {
MEMBER(0),
MODERATOR(1)
private final int level;
PermissionLevel(int level) {
this.level = level;
}
public int getLevel() {
return level;
}
}
现在给TeamRole接口一个返回与该角色关联的权限级别的方法
public Interface TeamRole {
PermissionLevel getPermissionLevel();
...
}
现在,检查时,如果一个成员有权所有你需要做的是对的权限级别的整数比较TeamRole的AbstractUser了。
if (user.getTeamRole().getPermissionLevel().getLevel() > PermissionLevel.MEMBER.getLevel()) {
// User has permission to do this action
}
TA贡献1804条经验 获得超8个赞
我知道已经有了答案,但我想我可以分享一个不同的想法,我们将使用继承来表达角色层次结构和方法签名来约束操作。
这是一般的设计思路:
用法:
public class TeamApplicationService {
public void doSomethingSpecial(final String teamId) {
final Team team = teamRepository.teamOfId(teamId);
final TeamModerator moderator = team.findModerator(currentUserId);
team.doSomethingSpecial(moderator);
}
}
public class Team {
final Map<String, TeamMember> teamMembers;
public Team() {
teamMembers = new HashMap<String, TeamMember>();
}
public void addMember(final TeamMember member) {
teamMembers.put(member.userId(), member);
}
private <T> T findMemberOfRole(final Class<T> clazz, final String userId) {
if (!teamMembers.containsKey(userId)) throw couldNotFindMember();
try {
return (T)clazz.cast(teamMembers.get(userId));
} catch (java.lang.ClassCastException e) {
throw couldNotFindMember();
}
}
private RuntimeException couldNotFindMember() {
return new RuntimeException("Could not find member");
}
public TeamModerator findModerator(final String userId) {
return this.<TeamModerator>findMemberOfRole(TeamModerator.class, userId);
}
public TeamMember findMember(final String userId) {
return this.<TeamMember>findMemberOfRole(TeamMember.class, userId);
}
public void doSomethingSpecial(TeamModerator moderator) {
//Moderator only
}
public void doSomethingGeneral(TeamMember member) {
//Any member
}
}
编辑:我不完全确定要求,但这里有一个基于评论的更复杂的例子。
TA贡献1906条经验 获得超3个赞
TeamRole 成为删除策略(也许重命名?)。可以使用更多的重构,但我认为一般的想法会奏效。如果我们有更多的角色,我们可以添加更多的类并实现它们如何处理删除。
public class Entry{
}
public class User extends AbstractUser{
}
public abstract class AbstractUser {
TeamRole role;
List<Entry> entries;
//self deletes are always ok
public void deleteEntry(Entry e){
this.getEntries().removeIf(entry -> entry.equals(e));
}
//delete depending on requested user role
public void deleteEntry(User requestBy, Entry e){
role.delete(this, e, requestBy);//mod or other poeple with ability to delete.
}
public List<Entry> getEntries() {
return entries;
}
};
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 void delete(AbstractUser user, Entry entry, User requester);
}
public class Member implements TeamRole{
@Override
public void delete(AbstractUser user, Entry entry, User requester) {
if(user==requester) {
user.deleteEntry(entry);
}
}
}
public class Moderator implements TeamRole{
@Override
public void delete(AbstractUser user, Entry entry, User requester) {
user.deleteEntry(entry);
}
}
添加回答
举报