my goal is to convert this class diagram into Java code.
How should I approach this, given that I want the constraints to hold at all times? It creates a chicken-egg problem where the first Course or Student created will always be alone in the world, thus violating the minimal multiplicity constraint of either "teaches" or "takes".
Any suggestions?
Thanks

enter image description here

有帮助吗?

解决方案

I'm of the opinion that we need to model our systems somewhat with the possibilities in mind that business rules & constraints can and will change.

Thus, some constraints are hard model constraints — the system simply won't work properly without them — but others are soft business constraints, which we should consider either dynamically configurable or case-by-case overridable.

I consider the use of constraints other than 0, 1, and * (e.g. 2, 3, 30) as an indicator as to whether constraint is a hard constraint that needs to be enforced within the model for correctness or a soft constraint that can be dynamically configured and/or application-overridden.

Even if the business constraint comes from legal requirements (e.g. in child care there must be at least 1 adult in the room for every 10 children under 4), the laws can change, over time for one, and can vary by district as well.


We can find similar issues (business rules as configuration or soft constraints vs. hard model constraints/requirements) even with the more basic cardinalities.

It might seem reasonable, and be a documented business rule, that a professor has one and only one office.  So, this suggests setting up the model with 1:1 cardinality with these entities.  Then we run into a situation where we have an exceptional professor who is a member of two different departments and she has two offices.

This exceptional situation could be handled in stride in a model with 1:N relationship, while a model with 1:1 would break requiring ugly workarounds — like duplicating the identity and account (userid/password) for the professor.

其他提示

First of all your class diagram doesn't look quite right. You can't make 2 associations with opposing cardinalities between 2 classes. At least I've never seen one.

What you want to achieve based on the lower association, a course teaches 0...30 students and a students has 1...* courses.

The Student class has a list of its courses and the course class has a list of its students. The constructor of students has a parameter for a course, so that you can only create a students attending a course.

许可以下: CC-BY-SA归因
scroll top