Multiple and Dynamic Classification

Multiple and Dynamic Classification

Classification refers to the relationship between an object and its type.

Most methods make certain assumptions about this type of relationshipassumptions that are also present in mainstream OO programming languages. These assumptions were questioned by Jim Odell, who felt that they were too restrictive for conceptual modeling. The assumptions are of single, static classification of objects; Odell suggests using multiple, dynamic classification of objects for conceptual models.

In single classification, an object belongs to a single type, which may inherit from supertypes. In multiple classification, an object may be described by several types that are not necessarily connected by inheritance.

Note that multiple classification is different from multiple inheritance. Multiple inheritance says that a type may have many supertypes, but that a single type must be defined for each object. Multiple classification allows multiple types for an object without defining a specific type for the purpose.

For example, consider a person subtyped as either man or woman, doctor or nurse, patient or not (see Figure 6-4). Multiple classification allows an object to have any of these types assigned to it in any allowable combination, without the need for types to be defined for all the legal combinations.

Figure 6-4. Multiple Classification

If you use multiple classification, you need to be sure that you make it clear which combinations are legal. You do this by labeling a generalization line with a discriminator, which is an indication of the basis of the subtyping. Several subtypes can share the same discriminator. All subtypes with the same discriminator are disjoint; that is, any instance of the supertype may be an instance of only one of the subtypes within that discriminator. A good convention is to have all subclasses that use one discriminator roll up to one triangle, as shown in Figure 6-4. Alternatively, you can have several arrows with the same text label.

A useful constraint is to say that any instance of the superclass must be an instance of one of the subtypes of a group. (The superclass is then abstract.) There's some confusion in the standard at the moment, but many people use the constraint {complete} to show this.

To illustrate, note the following legal combinations of subtypes in the diagram: (Female, Patient, Nurse); (Male, Physiotherapist); (Female, Patient); and (Female, Doctor, Surgeon). Note also that such combinations as (Patient, Doctor) and (Male, Doctor, Nurse) are illegal. The first set is illegal because it doesn't include a type from the {complete} Sex discriminator; the second set is illegal because it contains two types from the Role discriminator. Single classification, by definition, corresponds to a single, unlabeled discriminator.

Another question is whether an object may change its type. For example, when a bank account is overdrawn, it substantially changes its behavior. Specifically, several operations (including "withdraw" and "close") get overridden.

Dynamic classification allows objects to change type within the subtyping structure; static classification does not. With static classification, a separation is made between types and states; dynamic classification combines these notions.

Should you use multiple, dynamic classification? I believe it is useful for conceptual modeling. You can do it with specification modeling, but you have to be comfortable with the techniques for implementing it. The trick is to implement in such a way that it looks the same as subclassing from the interface so that a user of a class cannot tell which implementation is being used. (See Fowler 1997 for some techniques.) However, like most of these things, the choice depends on the circumstances, and you have to use your best judgment. The transformation from a multiple, dynamic interface to a single static implementation may well be more trouble than it is worth.

Figure 6-5 shows an example of using dynamic classification for a person's job, which, of course, can change. This can be appropriate, but the subtypes would need additional behavior, instead of being just labels. In these cases, it is often worth creating a separate class for the job and linking the person to it with an association. I wrote a pattern, called Role Models, on this subject; you can find information about this pattern, and other information that supplements my Analysis Patterns book, on my home page.

Figure 6-5. Dynamic Classification