1

Can requirements be expressed in class-diagrams?

For example:

A student can enroll in a class. There are different classes. Some classes have dependencies, e.g. you can't go to Spanish III before you went to Spanish II and Spanish I.

How to model that?

jkbx
  • 137
  • 3
  • [Sharing your research helps everyone](https://softwareengineering.meta.stackexchange.com/questions/6559/why-is-research-important). Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see [ask] – gnat Mar 24 '20 at 09:10
  • Don't be scared by comments. The above is from a moderator and generally a valid statement (it's not an answer). You'll get used to certain habits at SO over time... –  Mar 24 '20 at 11:00
  • @qwerty_so, to be clear, gnat is not a moderator. He’s more of a glitch in the system that’s useful enough that the real mods choose to leave him in place rather than “fix the bug” by banning him. – David Arno Mar 24 '20 at 20:19
  • 1
    @DavidArno Well to us lower ones that does not make a difference ;-) I saw hin in so many posts that I just assumed him to be a mod... –  Mar 24 '20 at 21:19
  • 1
    Usually requirements are expressed as user stories. Sometimes the problem is clear enough to start writing classes but we usually want to keep that conceptual at the beginning. (Look up CRC cards). Class Responsibility Collaborator cards are a way to think about what classes you may need before writing the details of their implementations. – John Douma Mar 25 '20 at 02:19
  • Thanks, that hint to CRC was very useful and actually helped me! – jkbx Mar 28 '20 at 17:08

2 Answers2

3

No, most requirements can not be fully expressed in a class diagram.

A class diagram shows the static structure of how classes are related to each other, but many requirements also have a dynamic aspect to them. However, requirements do drive the structure of the class diagram.

For example, to be able to fulfil the requirement that a Student must have followed the Class "Spanish I" before being allowed to enroll in "Spanish II", the class diagram must show an optional relation of Class with itself to record the possible prerequisites for a class.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
1

Requirements can be expressed either via requirement elements you can create on your own with a profile (there are already a couple of profiles offering that kind of element). However, more quickly you can just add constraints derived from requirements and plaster them in your class diagram - linked up to where they shall belong. A constraint can be represented as a {note with text in curly brackets}. Eventually you can stereotype that with <<pre-condition>>, <<post-condition>> or <<invariant>>.

With respect to the requirements you listed: they are related to runtime and are a better fit for use cases. The above is more for things like "student names may not exceed 20 chars or the like.