one, UML in the six relations
in a UML class diagram, the common are the following relation: generalization (Generalization), realization (Realization), association (Association), aggregation (Aggregation), combination (Composition), dependent (Dependency).
1.1, inheritance relationships - generalization (Generalization)
refers to a class (called sub-categories, sub-interfaces) inherit another class (called the parent class, parent Interface) functions, and can increase its own new functional capabilities, inheritance is a class with class or interface with the interface between the most common relationship; in Java using the extends keyword.
[relationship] is a generalization of inheritance, which means that the general and the special relationship, which specifies how the sub-class of the parent class of all special characteristics and behavior. For example: an owl is a bird that has the characteristics of a bird owl in common.
[] with the arrow pointing to the solid line arrowheads, arrows pointing to the parent class.
Description image above has a mouth like bird, wings, feathers and other attributes. Fly, will be called twitter, then there these methods. The Owls have large eyes and the ability to catch mice, which are characteristics of their own.
1.2, to achieve Relations (Realization)
refers to a class interface class that implements the interface (which may be several) function; implementation is the most common classes and the relationship between the interface; in Java implements these relationships clearly identified by the keyword.
[relationship] to achieve a relationship between classes and interfaces, which means that class is the interface to the realization of all the characteristics and behavior.
[] with triangular arrows pointing dashed arrows, arrow pointing interface.
Description above figure IFly is an interface, the interface has time, speed and constant, there is a fly method. FlyImpl inherited this IFly interface, the need to achieve fly method, while achieving classes can also have their own properties and methods.
1.3, dependent (Dependency)
can be simply understood, is a class to another class A uses B, and this relationship is a chance to use, temporary, very weak, but the changes will affect the Class B to A; such a person To cross the river, you need to borrow a boat, and the shipping time is dependent on the relationship between; manifested in the code level, as a parameter for the class B, class A property is a method in use in the method;
[] is a dependency relationship between the use of that one class implementation requires the assistance of another class, so try not to use two-way interdependence.
[code manifestations local variables, method parameters or static method call
】 【arrow and dotted lines with arrows pointing, pointing to the user
Description Bird class has a setFly method, which requires the user to use IFly implementation of the interface, then this relationship is dependent relationship.
1.4, association
He embodies two classes, or classes and interfaces between the semantic level a strong dependency relationship, such as me and my friends; This relationship is stronger than dependence, there is no chance of dependencies, relationship not temporary, usually long-term, but the relationship is generally equal association may be unidirectional, bidirectional; performance in the code level, to be associated with class B to class attributes appear in the associated class A , may also be associated with class A references a type of the associated class B, global variables;
[relationship] is a relationship between the owners, it makes a class knows the properties and methods of another class; such as: teachers and students, the husband and wife can be bidirectional association, it can be unidirectional. The association can have two-way arrows, or no arrow, unidirectional association with an arrow.
[code] member variables reflect
[arrow and arrowhead pointing] with ordinary solid line, pointing to the owner
Description In Bird class has a type of fly IFly attributes need to provide IFly interface implementations. Bird object interface implementation will use IFly complete fly method.
1.5, aggregation (Aggregation)
aggregation relationship is a special case, he embodied the whole and the part, with the relationship, that has-a relationship, this time between the whole and its parts are separable, they can have their own life cycle Some objects can belong to several whole, but also for more general object sharing; such as a computer with the CPU, the relationship between the company and employees; reflected in the code level, and relationships are the same, only from the semantic level to distinguish;
】 【aggregation relationship is the relationship between the whole and the parts, and some may leave a whole separate existence. Such as cars and tires and part of the overall relationship between the tires leave the car can still exist. Aggregation relationship is a relationship is a strong relationship; association and aggregation are syntactically indistinguishable, we must examine the specific logic.
[code] member variables reflect
[] with the arrow pointing to the hollow and solid line diamond, diamond point overall
Description birdChild a baby bird birds have a lot, so self-referential. Birds have many different numbers and colors of feathers, so the reference relationship is 0 ~ *.
1.6, combinations (Composition)
combination is a special case of association, he embodies a kind contains-a relationship, this relationship is stronger than aggregation, also known as a strong aggregation; he also reflects the relationship between the whole and the parts, but this when the whole and its parts are inseparable, integral part of the life cycle also means the end of the life cycle; such as you and your brain; reflected in the code level, and relationships are the same, only from the semantic level to distinguish;
[portfolio] relationship is the relationship between the whole and the parts, but some can not leave a whole separate existence. If the company and the sector is the relationship between the whole and the parts, no company would not exist sector. Combination of a relationship is a relationship, the relationship is even stronger than the aggregation relationship, which requires the aggregation of ordinary objects that represent the whole part of the object is responsible for representing the life cycle.
[code] member variables reflect
[] with the arrow pointing to solid diamond and solid line, diamond point overall
Description a school composed by a number of classes, the class does not exist out of school, and the school is not set up to leave class. Inseparable relationship like this you need to use combinations.
Comprehensive Example
For inheritance relationship did not realize how much these two questions, they are embodied in a class with the class, or classes and interfaces between the vertical relationship; additional four Relations is reflected in a class with class, or classes and interfaces between the reference horizontal relationships, it is more difficult to distinguish between, there are many things in order to prepare the relationship between positioning is difficult, also mentioned earlier, these types of relationships are semantic level, so from the code level, and can not be completely distinguish between the various relationships; But in general, after the performance of the strength of several relations degree were: to achieve generalization => portfolio> aggregation> association> dependency.
没有评论:
发表评论