6.2.6 Relationships between links
You have already seen an invariant involving links of the associations , and , connecting the classes , and .
A similar invariant arises in connection with the associations , and . The requirements state:
Each patient… is under the care of a single team of doctors; the consultant who heads that team is responsible for the patient.
See the second invariant at the beginning of Subsection 6.1.
The relevant fragment of the class diagram is shown in Figure 28.
The diagram represents the facts that a patient is cared for by a single team and is the responsibility of a single consultant, but there is nothing in the diagram to indicate that the consultant who is responsible for the patient must head the team that cares for that patient. This is clearly important, and we need to include it in the text that forms part of our model.
Remember that the association names are needed here to avoid ambiguity.
We can state the invariant as follows:
The object linked to any object via is linked via to the object to which that object is linked.
You can see from Figure 28 that the three associations involved in this invariant form a loop, as was the case with the invariant involving the , and associations at the beginning of Subsection 6.1. Invariants are often (but not always) required when a number of associations in a class diagram form a loop.
In the discussion following Activity 19 we concluded that an invariant was required to reflect the constraint that the consultant doctor in a team must be the consultant that heads the team.
(a) Which associations are involved in this invariant?
(b) Write down the invariant required.
(a) The associations involved are and .
(b) The invariant can be written as follows:
The object linked to a object via is linked to the same object via .
The invariant which was the subject of Activity 21 also involves a loop as shown in Figure 29. However, although there are three classes shown in this diagram (, and ), the loop is between only two classes ( and ). This is because inherits the association from (since it is a specialisation of ), and consequently is in a loop with via the and associations.
Figure 29 The and classes and the associations between them
Look again at the description of the Goat Breeding System given in Activity 10 and the class diagram given in the discussion of the exercise.
(a) Identify an extract from the description which gives rise to one or more invariants that cannot be deduced from the class diagram.
(b) State the invariant(s).
(a) The invariant(s) can be identified from the following extracts from the description.
A billy-goat mates several times during the current season but only with nanny-goats that are in the same herdas the billy-goat. A mating can produce several offspring, which – of course – are kids and are in the same herdas their parents.
In other words if a billy-goat and a nanny-goat mate, they and their offspring must be in the same herd.
(b) Here is a single invariant covering the relationships between all three kinds of goat:
All the objects linked to each object are linked to the same object.
There are a number of other ways of expressing this invariant, for example:
If is any object, then the and objects linked to , and any objects linked to , are linked to the same object.
Alternatively the invariant could be formulated as two separate invariants, each involving two of the associations , and , together with the association .
Note that because generalises , and , all their instances are also instances of and so are linked to instances of via .
Some of the invariants we have identified above could have been avoided by choosing an alternative way of modelling the system domains concerned. For example, representing the fact that each team of doctors includes both a consultant doctor and a junior doctor could have been achieved by replacing the association between and with two separate associations, one between and , and one between and . We could even have introduced a separate class for grade 1 junior doctors and connected it to with a third association. However, this alternative model would have introduced more complexity into our class diagram, and would still have required some invariants, so would not have provided any advantage.
It is an understanding of what is significant in the system domain that should inform decisions as to which classes and associations are most appropriate. However, as we have already said, the construction of a conceptual model is not an exact science. The best advice at this stage of the modelling process is to choose the simplest option that accurately reflects the situation in the real-world system domain; any appropriate changes or improvements to the ensuing model of the system structure can be considered later in the development.
Having specified invariants for the Hospital System, we include these in the text which, together with the class diagram, constitutes our model. A consolidated set of the invariants for the Hospital System is given and can be found in the PDF document below.
Click on the link below to view invariants for the hospital system.
(a) Identify an excerpt from the requirements document which gives rise to an invariant.
(b) State this invariant.
(a) ‘Each booklet is written by a number of tutors … one of whom must be the tutor who is taking the session that uses the booklet.’
(b) The object linked to any object is linked to the object linked to that object.
As usual, there are other, equally correct, ways of expressing this invariant.
Consider the Election Campaign System described in Activity 13. Suppose that the conceptual model for the system contained the following invariant:
Each object linked to a object is also linked to two or more objects.
Write down the real-world constraint that this invariant would represent.
A local activist who speaks at a public meeting must have visited at least two community groups.
In this section, you have learnt what is meant by an invariant, and how to identify and express them. In the process, a list of the invariants for the Hospital System has been drawn up. In the next, short section you will explore some of the implications of invariants for other aspects of the model.