简体   繁体   English

在Grails或标记中继承域类会做什么?

[英]Inheritance for domain class in Grails or a flag would do?

I am making a website where Doctors and Patient both can login. 我正在建立一个网站,医生和患者都可以登录。

class Doctors {
    String firstName
    String lastName
    String email
    String password
    String hospitalName
    String NPINumber
}

And patients 和病人

class Patients{
    String firstName
    String lastName
    String email
    String password
}

As it is evident that there are lot of overlapping fields which are only used for authetication/login purpose, I was wondering if inheritance is a good idea or should I just create a flag in a single class. 显而易见,有很多重叠的字段仅用于身份验证/登录目的,我想知道继承是一个好主意还是应该在单个类中创建一个标志。

So two options are: 因此,有两个选择:

OPTION-1 选项1

class Users{
    String firstName
    String lastName
    String email
    String password
}

class Doctors extends Users {
    String hospitalName
    String NPINumber
}


class Patients extends Users{
}

OPTION-2 选项2

class Users{
    String firstName
    String lastName
    String email
    String password
    boolean isDoctor
    String NPINumber
    String hospitalName
}

I am not sure which of these designs I should choose so that it is extendable in future! 我不确定应该选择哪种设计,以便将来可以扩展!

Option 1 is more OO for sure. 当然,选项1更面向对象。 However, I would still make a little bit of a change in order to make the design even better. 但是,为了使设计更好,我仍然会做一些更改。 Instead of extending User, why not have a User property which contains some of the common attributes. 为什么不扩展User,而不要拥有包含某些常用属性的User属性。 Extending the User class could make your design more complex. 扩展User类可以使您的设计更加复杂。 Maybe this topic can help you. 也许该主题可以为您提供帮助。

Also, as a suggestion, don't use the classes' names in the plural form. 另外,建议不要将类的名称使用复数形式。 Imagine it's an entity for itself and not a collection of entities. 想象一下,它本身就是一个实体,而不是实体的集合。 Clearly your class Doctor, for example, represents one doctor specifically. 显然,例如您的班级医生专门代表了一位医生。 Not more than one. 不超过一个。 So you should use Doctor, Patient and User . 因此,您应该使用Doctor,Patient和User Hope it helps. 希望能帮助到你。

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

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