具有不同类型用户的 Java 应用程序:我应该使用层次结构吗?

发布于 2025-01-17 10:43:34 字数 350 浏览 2 评论 0原文

我正在构建一个小型应用程序来学习具有不同类型用户的Java。起初,我认为这是一个层次结构,用户是超级阶级和其他三个(客户,管理员和工作人员)子类。另外,这三种用户类型具有不同的方法和GUI,但是它们都有用户和密码。

我的问题是,我有一个名为“应用程序”的类,以及返回用户对象的方法登录,我不知道该对象是不使用instanceof的admin,worker还是客户。同样,所有方法都在每个子类中,同样,我不想使用实例。我有另一个想法是在用户类中制作所有方法,以及我将检查的角色枚举,以查看用户是否有许可。但是,如果我需要为此进行枚举,我看不到拥有子类的意义。

有什么解决方案吗?

注意:这是一个小应用程序,没有数据库或在线连接,只有一个大学项目。

I'm building a small application to learn Java that has different types of user. At first, I thought that It was a hierarchy, where user is the superclass and the other three (customer, admin and worker) subclasses. Also, this three user types have different methods and GUIs, but they all have user and password.

My problem is that I have a class called Application and the method login that returns an user object and I don't know how to see if this object is an admin, worker or customer without using instanceof. Also, all the methods are in each subclass because they don't have functionality in common, except the login (that is in another class), then if I have an object of type user I would need to cast it to another subclass, but, again, I don't want to use instanceof. Another idea that I had is to make all the methods in user class and an role enum that I would check to see If the user has permission. But, If I need to do an enum for this, I don't see the point in having subclasses.

Is there any solution to this?

Note: It's a small app, without databases or online connection, only a college project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2025-01-24 10:43:34

从我的所有经验来看,为用户提供多个类是一个不好的主意:

  1. 不同的实例意味着不同的持久性(例如从加载并保存到数据库或文件或流)。
  2. 在某一时刻,有些用户会担任其他角色。处理该过渡的不同类别将是最糟糕的。

事实证明,另一点更难实施:当前角色的一个枚举参考。尽管这是一个很好的解决方案,但请考虑特殊的权限和角色混合(set< myroleenum>set< myroleclass>)。

  • 在这里,最好的方法是能够赋予用户多个角色。
  • 角色可以是班级的枚举或实例,具体取决于您想实现的动态。
    • 枚举可以(内省/反射:应该!)仅在编译时更改(表示重新编程,重新编译,重新发行,重新启动您的应用程序),
    • 虽然您可以在运行时创建正常类的新实例(尽管您也需要集成某种管理和持久性)。
  • 请记住不要通过其序数来保存角色!更改班级中的枚举顺序将改变他们的单个序数!
    • 最好通过唯一的ID(例如名称不更改名称)保存角色
  • 并且鉴于角色,您可以将所有“ Isresponsible Forfor(item)”方法实现到您的会话管理器中,也可以在用户类中实现或进入另一个专注于角色处理的班级。

FATIT:

  • 此(用户在迄今为止,我已经实施了最新项目的列表/一组枚举角色),这是我最新的最新项目的最好的,故障的(编译时间安全)模型。

    • 使用user.set< myroleenum>的最大缺点是,您必须重新编译+重新编译整个代码以扮演其他角色。但是其他角色通常总是带有新功能,因此必须重新编译/部署该代码...
  • 如果您的项目打算由成千上万的用户和一组管理员使用,请选择user.set< myroleclass>实现。

    • 随着这个大小,该项目可能会有动态的类加载,扩展枚举和动态类加载不能很好地混合。
    • 但这意味着要在Myroleclass
    • 的实例,持久性和管理中进行许多其他工作

From all my experience, it's quite a bad idea to have multiple classes for the User:

  1. Different instancing means different persistence (like loading from and saving to a database or file or stream).
  2. At one point or the other, some users will get other roles. Different classes will be worst to handle that transition.

Another point which has proven to make implementation harder: one enum reference for the current role. Although this is quite a good solution, think about special permissions and a mixture of roles (Set<MyRoleEnum> or Set<MyRoleClass>).

  • Here, the best approach is to be able to give the user multiple roles.
  • Roles can be enums or instances of a class, depending on how dynamic you wanna implement it.
    • Enums can (with introspection/reflection: SHOULD!!!) only be changed at compile time (means reprogram, recompile, redeploy, restart your app),
    • while you can create new instances of normal classes during runtime (though you then need to integrate some kind of administration and persistence too).
  • Remember to NOT save roles by their ordinal number! Changing the order of enums in their class will change their individual ordinal number!
    • It's better to save roles by a unique ID (like the name, provided names don't change)
  • And given the roles, you can either implement all the 'isResponsibleFor(item)' methods into your Session Manager, or into the User class, or into another class that focuses on role handling.

Facit:

  • This (user has a list/set of enum roles), so far, has proven the best, fail-safe (compile-time safe) model I have implemented up to date, for smaller projects.

    • The big Downside of using User.Set<MyRoleEnum> is that you have to recompile+redeploy the whole code for an additional role. But additional roles usually always come with new features, so the code would have to be re-compiled/deployed anyway...
  • If your project is meant to be used by thousands of users and an army of admins, go for the User.Set<MyRoleClass> implementation.

    • With that size, the project will probably have dynamic class loading, and extending enums and dynamic class loading do not mix well.
    • But this means a lot of additional work to put into instancing, persistence and administration of the MyRoleClass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文