• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/11

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

11 Cards in this Set

  • Front
  • Back

Figure 24 shows the class Account with an attribute called accountNo. Write an appropriate invariant that limits the values of this attribute to a number composed of a 6-digit sort code (to identify the branch) followed by an 8-digit account number.

We are not told anything about the range of either branch codes or identifiers, but we do know that a valid account number has 14 digits as follows:



{accountNo is a 14-digit number, with a 6-digit number at the beginning for the sort code followed by an 8-digit identifier}

When is an invariant on a class true?

The invariant on a class must be true for every object of that class from the time that object is created to the time it is deleted.

What are the risks involved when you try to record all the possible constraints on a model?

The first risk relates to the complexity of the resultant model. If too many constraints are recorded on a model, that model will become difficult to read or comprehend. For each case you should decide whether or not a given constraint adds value to your particular model. It may be more appropriately recorded in the glossary.



The second risk relates to the potential increase in the number and complexity of any dependencies that would arise for each additional constraint, especially those among two or more model elements.

What is the { xor } notation used for?

What is the { xor } notation used for?

The special {xor} notation is used to specify that an instance of the base class must participate in exactly one of the associations grouped together by the {xor} constraint. Exactly one of the associations must always be active.



xor is a Boolean operator that gives true as a result only if its two operands are one true and the other false.

In a model that contains the classes Adult and Child, where each Child has a mother and father that are instances of Adult, what constraints might you impose on relationships between the classes? Express the constraints in English.

Every Child must have a mother and father that are instances of Adult. The father cannot be the mother. A father must be male and a mother must be female. Both adults must be older than the child.

In the UK, it is a legal requirement that both parties to a marriage are at least 16. Should this be modelled as an invariant?

Almost certainly not. A model is meant to express what the case is about rather than what it ought to be about. So unless the domain expert agrees that illegal marriages need not be represented, the model should allow them.

How would you model the constraint in a hotel system that every bill must be paid with either a debit or a credit card? How would you extend your model if cash were to be allowed?

Figure 47 shows one way is to use the {xor} notation. However this can relate only two associations. If you need to express a three-way constraint in order to allow for the addition of cash payments, for example, you will have to abandon the graph...

Figure 47 shows one way is to use the {xor} notation. However this can relate only two associations. If you need to express a three-way constraint in order to allow for the addition of cash payments, for example, you will have to abandon the graphical notation of {xor} and instead write a textual constraint. Alternatively you could use generalisation to create an abstract PaymentMethod class with an association to the class Bill, following the examples in Section 5. Then each payment method, such as debit or credit card or cash, would become a specialisation of the parent, abstract classPaymentMethod.

The model in Figure 51 expresses the fact that a train must be associated with two employees: a driver and a guard. By considering the loop of associations identify a problem with the model, and suggest a constraint that solves the problem.


 

The model in Figure 51 expresses the fact that a train must be associated with two employees: a driver and a guard. By considering the loop of associations identify a problem with the model, and suggest a constraint that solves the problem.


You need to capture the fact that a given employee cannot be both the driver and the guard for a particular train. You can formulate this as a constraint on any class round the loop.


For example, the following constraint can be located in the class Train:



context Train inv:


self.driver <> self.guard

The model in Figure 52 shows that a person takes out a mortgage to buy a house. A person must offer that house as security against the mortgage. A person can take out more mortgages, each secured by a house. By considering the loop of association...

The model in Figure 52 shows that a person takes out a mortgage to buy a house. A person must offer that house as security against the mortgage. A person can take out more mortgages, each secured by a house. By considering the loop of associations, identify a problem with the model and suggest a suitable constraint using OCL.


The model would allow one person to have a mortgage that uses someone else’s home as security. You need to capture the fact that a house’s owner is the same person who offered that house as security for a given mortgage. When there are multiplicities in the loop that can be greater than one, you need to take care in writing the invariant. In one direction around the loop an individual house has only one owner. In the opposite direction each house is security for only one mortgage, and that mortgage is associated with only one person. This makes it easy to place an appropriate constraint on either the House class or the Mortgage class.


For the House class:


context House inv:


self.owner = self.mortgage.person



Similarly for the Mortgage class:


context Mortgage inv:


self.person = self.security.owner



However expressing an equivalent constraint on the class Person is more problematic because you would have to find the intersection of two sets: the houses owned by a particular person and the houses secured by mortgages taken out by the same person. We suggest that you use English to formulate a suitable constraint in such circumstances. You could place the constraint as text inside a note and attach it to the appropriate class.

Suppose that there is a class Person in a model, and you have identified the attributes


title


firstName


lastName


gender


houseNumber


streetName


city


postcode


all of which are represented as strings. Write an appropriate invariant that will constrain the attribute values to exclude invalid values.

You might constrain some fields to have appropriate capitals and constrain others to contain only values from a fixed set as follows:



{title must be spaces, or one of ‘Mr’, ‘Mrs’ or ‘Ms’


firstName and lastName are written in lower case, but upper case is applied either to the first character of a word or after a space


sex must be ‘M’ or ‘F’ or an empty string


houseNumber must contain only digits, forming a number greater than zero, optionally followed by a letter


streetName and city are written in lower case, but upper case is applied to both the first character of a word and any character after punctuation or spaces


postcode may contain upper-case letters and digits, and a single space}



The invariants are certainly all things that must remain true, but they may not be the whole truth. For example, the constraint given above is necessary for a postcode to be valid but it is not sufficient: a string could conform to it but still not be a postcode that actually exists. However the constraint will always be true, if the system is implemented correctly.


Unfortunately the invariant for title is probably incorrect. There are other commonly approved titles such as ‘Dr’ and ‘Sir’.

Figure 53 shows a fragment of a class model for a chain of video libraries. It includes an attribute called location for the class VideoLibrary to show where each branch is located. The class Member has been given two attributes: name and...

Figure 53 shows a fragment of a class model for a chain of video libraries. It includes an attribute called location for the class VideoLibrary to show where each branch is located. The class Member has been given two attributes: name and address. Identify a problem with the loop of associations in this model. How could an object diagram help you identify the problem?



The loop in the associations for the video library’s class model is similar to that for the hotel’s class model that you saw in Figure 48. That is, there are two ways of starting with a member and ending with a library. You can ask the member...

The loop in the associations for the video library’s class model is similar to that for the hotel’s class model that you saw in Figure 48. That is, there are two ways of starting with a member and ending with a library. You can ask the member either which library he or she has borrowed the stock copy from, or which library owns the stock copy that the member is borrowing. Both routes must yield the same library.


In Section 2 you saw that an object diagram can be useful for determining whether a class model is correct and sufficiently constrained, because an object diagram represents a particular configuration at a particular moment in time. For example, you could show a particular valid configuration of video library objects as in Figure 54. In another object diagram, you might (try to) show an invalid configuration. If you can show an invalid configuration, then you might want to add constraints or change the model to prevent it.