Search code examples
inheritanceumlclass-diagramclass-design

Class diagram for a room reservation: use of inheritance?


I'm creating a class diagram for a room reservation system. There is an option of creating a regular reservation (for example every Tuesday in March). I´m wondering whether I should design a specific class like this:

enter image description here


Solution

    • Should I design a specific class? Maybe
    • Like this? No

    Whether to use inheritance or not is IMHO a cost/benefit question.

    When implementing inheritance there is usually some cost involved in terms of extra classes, tables, documentation and other results that need to be created after the decision to use inheritance.

    Inheritance is about special and general cases. First you might want to ask what is the difference between the cases - (called a discriminator). In your case it is the type of reservation and you could avoid the inheritance by modelling this type of reservation and implementing the different behavior based on the reservation type. That would lead to a design like:

    Avoiding inheritance

    Avoiding Inheritance

    If the inheritance has a high benefit because there are a lot of extra attributes, relation or operations for each special case then you can apply it.

    In this case there is

    • an extra 1:n relation between regular reservation and reservation (which was not visible in your design but implied by the attribute regular Reservation)

    and there are some extra fields

    • start date
    • end date
    • frequency

    What is missing at this time is common behavior that would make it sensible to apply inheritance. In the image below I added the generalization nevertheless. It would not be needed at this point in time but as soon as there are general operations you would like to apply to any type of reservation:

    Applying inheritance

    Applying inheritance