I l@ve RuBoard |
6.5 Generalization and SpecializationAll of the instances of a class must share common characteristics and common behavior. But we often face things that are somewhat common yet somewhat different. 6.5.1 The Concept of Generalization and SpecializationIn Section 5.5: Checking Classes and Attributes, we presented a Product as an example of a uniformity problem. The online store sells more than just books; it also sells videotapes, CDs, DVDs, and computer software. All these products have a sale price and are identified by a universal product code (UPC). Books have an ISBN number; CDs, videotapes, and DVDs have a running time; computer software titles have version numbers. We noted a uniformity problem that made a single Product class undesirable and promised to show how to correct the problem. An alternative to the single Product class is to have separate classes for each type of product, as shown in Figure 6.11. But while this is a more precise attribution, it isn't quite satisfying either. Attributes (and behaviors) that ought to be common are spread across many classes. Figure 6.11. Separate Product ClassesThe concept of generalization allows us to have the best of both worlds when confronted by this type of problem. We can have both a single Product class and individual classes for BookProduct, RecordingProduct, and SoftwareProduct. Figure 6.12 shows how these products may be partitioned into subsets, one for each kind of product. Figure 6.12. Partitioning of Products into SubsetsFigure 6.13 models the Product class as a generalization of BookProduct, RecordingProduct, and SoftwareProduct. This means that the superclass Product contains attributes and associations applicable to all of the subclasses BookProduct, RecordingProduct, and SoftwareProduct. Likewise, BookProduct is a specialization of the Product class. It contains additional attributes and associations that are relevant only to books. Figure 6.13. Product Generalization and SpecializationDefinition: Definition: Definition: Each superclass and each subclass is a class like any other. It may have attributes and participate in associations. It must have a class description and descriptions for each attribute and association in which it participates.
With the preceding in mind, we may now (finally!) define an object. Definition: 6.5.2 Mutual Exclusion and its ImplicationsExecutable UML prescribes a strict mutual exclusion principle in defining generalizations: Each instance of a superclass must be one and only one of its superclasses, and each instance of a subclass must be an instance of the superclass. Definition: In UML terms, all Executable UML superclasses are tagged {abstract} and all generalizations are tagged {disjoint, complete}. This means:
This approach supports the semantic modeling properties of Executable UML, and abstraction from things in the domain: the several products. Consequently, each product is a member of the superset and exactly one subset. The set partitioning is disjoint and complete as shown by the tag {disjoint, complete}. 6.5.3 Repeated SpecializationA class can be specialized repeatedly, as shown in Figure 6.14. Bank accounts can be subclassed into checking and savings accounts. A checking account can be further subclassed into regular and interest-bearing accounts. Figure 6.14. Repeated SpecializationIn cases of repeated specialization, all the non-leaf classes are abstract; hence the class CheckingAccount is abstract even as it is also a subclass. 6.5.4 Multiple GeneralizationConsider now the hierarchy of Figure 6.15, in which a single subclass is the subclass of multiple superclasses. In this model, the class InterestCheckingAccount is a subclass of InterestBearingAccount and CheckingAccount. This is perfectly legal. Figure 6.16 illustrates how the accounts are organized into {disjoint, complete} subsets. Figure 6.15. Multiple GeneralizationFigure 6.16. Illustration of Multiple GeneralizationHowever, when building these kinds of structures, watch out for "diamond generalization," as illustrated in Figure 6.17, wherein the same instance purports to be a subclass of two superclasses that have the same parent. Figure 6.17. Improper Multiple GeneralizationHere we have made the InterestBearingAccount a subclass of an Account. (After all, the InterestBearingAccount is surely some kind of account.) The result of this innocuous change is that an InterestCheckingAccount has a parent CheckingAccount that is a subclass of Account, and a parent InterestBearingAccount that is a different subclass of Account. The mutual exclusion rule forbids this. This situation is quite rare, but it is subtle and can cause a great deal of confusion. 6.5.5 Compound GeneralizationIn Figure 6.13 we subclassed Product into Book, Recording, and Software. In addition, some of each kind of Product are not actually kept in stock, but are special-ordered from the Publisher whenever ordered by a Customer. This subclassing is separate and distinct from whether the Product is a Book, Recording, or Software product. Figure 6.18 shows how we can represent this with a distinct generalization-specialization relationship. Figure 6.18. Compound Generalization
We call this a compound generalization since the superclass is a disjoint, complete generalization in more than one hierarchy. Each generalization is shown as a separate relationship with its own triangle and collection of subclasses |
I l@ve RuBoard |
No comments:
Post a Comment