Derived Associations and Attributes

Derived Associations and Attributes

Derived associations and derived attributes can be calculated from other associations and attributes, respectively, on a class diagram. For example, an age attribute of a Person can be derived if you know that Person's date of birth.

Each perspective brings its own interpretation of derived features on class diagrams. The most critical of these has to do with the specification perspective. From this angle, it is important to realize that derived features indicate a constraint between values, not a statement of what is calculated and what is stored.

Figure 6-8 shows a hierarchical structure of accounts drawn from a specification perspective. The model uses the Composite pattern (see Gamma, Helm, Johnson, and Vlissides 1995).

Figure 6-8. Derived Associations and Attributes

Note the following.

  • Entry objects are attached to Detail Accounts.

  • The balance of an Account is calculated as the sum of Entry amounts.

  • A Summary Account's entries are the entries of its components, determined recursively.

Since Figure 6-8 illustrates a specification model, it does not state that Accounts do not contain fields to hold balances; such a cache may well be present, but it is hidden from the clients of the Account class.

I can illustrate how derived elements indicate constraints with a class named Time Period (see Figure 6-9).

Figure 6-9. Time Period Class

If this is a specification diagram, although it suggests that start and end are stored and that duration is calculated, a programmer can, in fact, implement this class in any fashion that maintains that external behavior. For instance, storing start and duration and calculating end is perfectly acceptable.

On implementation diagrams, derived values are valuable for annotating fields that are used as caches for performance reasons. By marking them and recording the derivation of the cache, it is easier to see explicitly what the cache is doing. I often reinforce this in the code by using the word "cache" on the name (for instance, balanceCache).

On conceptual diagrams, I use derived markers to remind me where these derivations exist and to confirm with the domain experts that the derivations exist. They then correlate with their use in specification diagrams.