· Generalization
The generalized relationship is the relationship between the general description of the class element and the specific description. The specific description is based on the general description and is extended. The specific description is exactly the same as the general description. All features, members, and relationships include supplemental information. For example, a mortgage is a specific type of lending. A mortgage maintains the basic features of lending and adds additional features, such as a house that can be used as a collateral for lending. The description is generally called "parent". The specific description is called "sub-loan", "sub-loan", and "mortgage. Generalization is used in class elements (classes, interfaces, data types, use cases, participants, signals, etc.), packages, state machines, and other elements. In a class, the term superclass and subclasses represent the parent and child.
The generalization is represented by an arrow pointing from the child to the parent, and an empty triangle pointing to the parent (as shown in Figure 4-7 ). Multiple general relationships can be represented by a tree composed of arrows. Each branch points to a subclass.
Figure 4-7 generalized Representation
General Purpose
Generalization has two purposes. The first purpose is to define the following conditions: When a variable (such as a parameter or a procedure variable) is declared to carry the value of a given class, the class (or other element) can be used) this is called the alternative principle (proposed by Barbara liskov ). This principle indicates that an instance of the descendant can be used no matter when the ancestor is declared. For example, if a variable is declared to have a loan, a mortgage object is a valid value.
Generalization makes it possible for multi-state operations, that is, the implementation of operations is determined by the classes of the objects they use, rather than by the caller. This is because a parent class can have many sub-classes, and each sub-class can implement different variants defined in the same operation in the class overall set. For example, the calculation of interest on mortgages and car loans is different, and each of them is the calculation of interest deformation in parent loans. A variable is declared to have a parent class, and an object of any subclass can be used, and any of them has its own unique operations. This is especially useful because you can add a new class without changing the existing polymorphism call. For example, a new loan can be newly added, and the existingCodeStill available. A multi-state operation can be declared in the parent class without implementation. Its Child classes need to supplement the implementation of this operation. This incomplete operation is abstract (its name is in italic ).
Another purpose of generalization is to allow the parent to define the added description while sharing the components defined by the ancestor. This is called inheritance. Inheritance is a mechanism by which object descriptions of a class are aggregated from the declared part of the class and its ancestor. The shared part that inherits the description is declared only once and can be shared by many classes, instead of repeatedly declaring and using it in each class. This sharing mechanism reduces the scale of the model. More importantly, it reduces the changes that must be made to update the model and the unexpected pre-and post-definition inconsistency. Inheritance works in a similar way for other components, such as States, signals, and use cases.
· Inheritance
Each generalized element has a set of inheritance features. Includes constraints on any model element. For Class elements, they also include some features (such as attributes, operations, and signal reception) and associated participants. A subclass inherits the inherited features of all its ancestors. Its complete features include inherited features and directly declared features.
For the class element, attributes without the same feature mark will be declared multiple times, whether directly or inherited, otherwise a conflict will occur and the model form is incorrect. In other words, attributes declared by the ancestor cannot be declared again by future generations. If the class interfaces are consistent (with the same parameters, constraints, and meanings), the operation can be declared in multiple classes. The attached statement is redundant. A method can be declared by multiple classes in the hierarchy, and the method attached to the descendant replaces (reloads) the method with the same feature Mark declared in any ancestor. If two or more copies of a method are inherited by a Class (through multiple inheritance of different classes), they will conflict and the model form is incorrect (someProgramming LanguageAllows you to explicitly select one of the methods. We found that it would be simpler and safer to redefine methods in future classes ). The constraint in an element is the combination of the element itself and the constraint of all its ancestors. If they are inconsistent, the model format is incorrect.
In a specific class, each inherited or declared operation must have a defined method, either directly defined or inherited from the ancestor.
· Multi-Inheritance
If a class element has multiple parent classes, it can obtain Inheritance Information (4-8) from each parent class ). Its features (attributes, operations, and signals) are the union of all its parent features. If the same class appears on multiple paths as the parent class, each member of the class has only one copy of it. If the features with the same characteristics are declared by two classes, and these two classes are not inherited from the same ancestor (I .e. independent declaration), the declaration will conflict and the model form is incorrect. Because experience tells our designers to solve this problem on their own, UML does not provide a conflict solution in this case. Languages like Eifel allow conflictProgramThe explicit resolution by designers is safer than implicit conflict resolution principles, which often surprise developers.
Figure 4-8 multi-Inheritance
· Single and multi-category
In the simplest form, an object belongs to only one class. Many object-oriented languages have this restriction. There is no logical necessity for an object to belong to only one class. We only need to observe objects in the real world from multiple perspectives. In a more general form of UML, an object can have one or more classes. The object looks like it belongs to an implicit class, and this class is a subclass of each direct parent class-multiple inheritance can avoid declaring a new class, which improves efficiency.
· Static and dynamic class Elements
In the simplest form, an object cannot change its class after it is created. We once again demonstrate that this restriction is not logically necessary, but is initially designed to make the implementation of object-oriented programming languages easier. In a more general form, an object can dynamically change its class, so that it will get or lose some attributes or associations. If Objects lose them, the information in them will be lost and cannot be restored later, even if the object is changed back to the original class. If this object has properties or associations, they must be initialized when they are changed, just like initializing a new object.
When multiclass classification and dynamic classification are used together, an object can get or lose a class during its life cycle. Dynamic classes are sometimes called roles or types. A common modeling mode is that each object has a unique static inherent class (that is, the class that cannot be changed during the object's life cycle ), add zero or more corner classes that can be added or removed during the life cycle of the object. The inherent class describes the basic features of this object, while the role class describes the temporary features. Although many programming languages do not support multiple dynamic classifications in class declarations, it is still a useful modeling concept and can be mapped to associations.