简体   繁体   English

Java:在框架中定义枚举(抽象枚举的解决方法)

[英]Java : Define an enum in a framework (workaround for abstract enum)

I want to create a global framework, that could be reused in many different systems. 我想创建一个可以在许多不同系统中重用的全局框架。 I would like to include a kind of enum, whose content depends on the project. 我想包含一种枚举,其内容取决于项目。 Concretely, this enum defined different kind of rights with int as the priority (admin=100, advanced user=50, basic user=10 etc.)<- These are just examples, the content of the enum could be totally different for another project. 具体来说,此枚举定义了不同类型的权限,优先级为int (admin = 100,高级用户= 50,基本用户= 10等)<-这些仅是示例,对于另一个项目,枚举的内容可能完全不同。 Since this is not possible to have an "abstract" enum, I have unfortunately no idea how I could implement it. 由于不可能有一个“抽象”的枚举,因此不幸的是我不知道如何实现它。 Can someone give me some advice (Design pattern for instance, or something else)? 有人可以给我一些建议吗(例如设计模式,或其他)?

Some use cases : 一些用例:

if myauthorizationLevel=MyEnum.admin then... 如果myauthorizationLevel = MyEnum.admin,则...

if myauthorizationLevel.value>25 then ... 如果myauthorizationLevel.value> 25,则...

Thanks a lot for your help. 非常感谢你的帮助。

EDIT : According to your answers (all great), I understand what my problem is (Sorry to not have say this before, I couldn't see this then!): I need a function to transform a string in my enum. 编辑:根据您的回答(都很好),我了解我的问题是(很抱歉以前没有说过,我当时看不到!):我需要一个函数来转换枚举中的字符串。 So, how can I do this interface with a "static" function (I know that static is not possible in interfaces, and that would be a problem), and it gives : 因此,如何使用“静态”功能实现此接口(我知道在接口中不可能实现静态,这将是一个问题),它给出了:

public interface IRole
{}

public enum ERole implements IRole
{}

Then the last function in my library would be : 那么我库中的最后一个函数将是:

public function IRole getRole()
{
          return IRole.getValueAsRole(myString);
}       

Sorry again to not have explained this before. 再次抱歉,以前没有对此进行解释。 I hope this is clear. 我希望这很清楚。 This is actually my problem since the beginning. 从一开始,这实际上就是我的问题。

Many thanks 非常感谢

A role interface in your framework (Role.java): 框架中的角色接口(Role.java):

public interface Role {
    int authorizationLevel();
}

A set roles supplied by your framework (Roles.java): 框架(Roles.java)提供的一组角色:

public enum Roles implements Role {
    ADMIN(50),
    POWER_USER(30),
    LIMITED_USER(25),
    GUEST(10);

    private final int authorizationLevel;

    Role(int authorizationLevel) {
        this.authorizationLevel = authorizationLevel;
    }

    @Override
    public int authorizationLevel() {
        return authorizationLevel;
    }
}

A permissions checker in your framework (Authorization.java): 框架中的权限检查器(Authorization.java):

public final class Authorization {
    static boolean isAuthorized(Role role) {
        return Roles.ADMIN.equals(role) ||
            role.authorizationLevel() > 25;
    }
}

A set of roles provided by a client of your framework (ClientRoles.java): 框架的客户端(ClientRoles.java)提供的一组角色:

enum ClientRoles implements Role {
    WEB_DESIGNER(50);

    private final int authorizationLevel;

    @Override
    public int authorizationLevel() {
        return authorizationLevel;
    }
}

A client application (RoleBased MSMain.java): 客户端应用程序(RoleBased MSMain.java):

public final class RoleBasedCMSMain {
    public static void main(String[] args) {
        Role role = ClientRoles.WEB_DESIGNER;
        if (Authorization.isAuthorized(role)) {
            System.out.println("You are authorized!");
        }
    }
}

This is reinventing the wheel though. 不过,这是在重新发明轮子。 It's probably worth looking at JAAS (Java Authentication and Authorization Service) which solves these problems and integrates with existing frameworks. 值得一看的是JAAS(Java身份验证和授权服务)可以解决这些问题并与现有框架集成。

An enum can implement an interface, so you use the interface as an "abstract enum" 枚举可以implement接口,因此您可以将该接口用作“抽象枚举”

interface Role{

}
enum SomeRoles implements Role{
ADMIN
}

So in your external API you can use the interface so people can provide their own implementation of Role and yet they can use whats available in your enum 因此,在您的外部API您可以使用该接口,以便人们可以提供自己的Role实施,但是他们可以使用您enum可用的功能

 void fn(Role role);

 fn(new MyRole()); // provided by user
 fn(SomeRoles.ADMIN); //provided by framework 

It is type-safe and yet extensible 它是type-safeextensible

You could define an interface with the necessary methods like authorizationLevel(). 您可以使用必要的方法(例如authorizationLevel())定义接口。 Since enums can implement interfaces on the project there could be an enum created which implements it. 由于枚举可以在项目上实现接口,因此可以创建一个枚举来实现该接口。 Where such enum should be used, the interface could be used. 在应使用此类枚举的地方,可以使用该接口。 With generics you can even force that enums implementing the interface should be used. 使用泛型,您甚至可以强制枚举应使用实现该接口的枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM