In my system I have a single class with a load of (20?) final
booleans
defining the permissions this user type has.
What is a better way to do this?
I'm sure there's lots of examples about this but I don't know the keywords.
You can take advantage of enums, e.g.:
public enum Permission {
READ, WRITE;
}
public class User {
private final EnumSet<Permission> permissions;
public User(Permission... permissions) {
this.permissions = EnumSet.copyOf(Arrays.asList(permissions));
}
public boolean hasPermission(Permission permission) {
return permissions.contains(permission);
}
//...
}
User user = new User(Permission.READ, Permission.WRITE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With